VC++ Fundamentals

18,977 views

Published on

Published in: Technology
12 Comments
39 Likes
Statistics
Notes
No Downloads
Views
Total views
18,977
On SlideShare
0
From Embeds
0
Number of Embeds
79
Actions
Shares
0
Downloads
0
Comments
12
Likes
39
Embeds 0
No embeds

No notes for slide

VC++ Fundamentals

  1. 1. WINDOWS PROGRAMMING USING VC++
  2. 2. Contents <ul><li>Windows Architecture </li></ul><ul><li>SDK </li></ul><ul><li>MFC Fundamentals </li></ul><ul><li>Device Context </li></ul><ul><li>Document-View Architecture </li></ul><ul><li>Dialog Boxes </li></ul><ul><li>Property sheets and splitter windows </li></ul><ul><li>Database Connectivity </li></ul><ul><li>Networking </li></ul><ul><li>Multithreading </li></ul><ul><li>Exception Handling </li></ul>
  3. 3. References <ul><li>MFC C++ classes </li></ul><ul><li>by Shirley Wodkte </li></ul><ul><li>VC++ Programming </li></ul><ul><li>by David Kruglensky </li></ul><ul><li>VC++ Programming </li></ul><ul><li>by yaswanth kanetkar </li></ul><ul><li>MSDN library </li></ul>
  4. 4. WINDOWS ARCHITECTURE
  5. 5. Understanding Win32 Architecture <ul><ul><li>Processes </li></ul></ul><ul><ul><li>The Virtual Memory System </li></ul></ul><ul><ul><li>Threads and Multitasking </li></ul></ul><ul><ul><li>Threads, Messages, and Message Queues </li></ul></ul><ul><ul><li>Event-Driven Programming </li></ul></ul>
  6. 6. PROCESS <ul><li>A process: </li></ul><ul><ul><li>Is a running instance of an executable program. </li></ul></ul><ul><ul><li>Has a private address space in memory. </li></ul></ul><ul><ul><li>Contains system resources, such as files, pipes, communications ports. </li></ul></ul><ul><ul><li>Has at least one thread, or path of execution. </li></ul></ul>
  7. 7. Virtual Memory System <ul><li>Two types of memory </li></ul><ul><ul><li>Physical memory </li></ul></ul><ul><ul><li>Virtual memory </li></ul></ul><ul><li>Virtual memory vs. Physical memory </li></ul><ul><li>Benefits of using a virtual memory system </li></ul>
  8. 8. THREADS <ul><li>What are threads? </li></ul><ul><ul><li>A path of execution within a process </li></ul></ul><ul><ul><li>Can be considered a task that the application must perform </li></ul></ul><ul><ul><li>Processes can contain many threads </li></ul></ul><ul><ul><li>Multithreading gives the appearance of multitasking </li></ul></ul><ul><ul><li>Multithreading uses context switching </li></ul></ul>
  9. 9. THREAD PROCESSING
  10. 10. THREADS & MESSAGES <ul><li>How a window gets messages from hardware </li></ul><ul><ul><li>Each (UI) thread has an input queue for processing messages. </li></ul></ul><ul><ul><li>The queue operates asynchronously from other queues. </li></ul></ul><ul><ul><li>The queue receives messages from the system's raw input queue. </li></ul></ul><ul><ul><li>Raw input queue routes messages from hardware to the appropriate window's input queue. </li></ul></ul>
  11. 11. EVENT DRIVEN PROGRAMMING <ul><li>Event-driven programs start and wait for user input </li></ul><ul><li>Contrast to procedural programming </li></ul>
  12. 12. Anatomy of a Windows based Application <ul><li>Elements of a Windows-Based Application </li></ul><ul><li>How an Application Is Started </li></ul><ul><li>How a Window Processes Messages </li></ul>
  13. 13. Windows Program
  14. 14. Elements of Windows based Program <ul><li>Code </li></ul><ul><li>User interface resources </li></ul><ul><li>Library modules </li></ul>
  15. 15. How the Application is Started <ul><li>Operating system creates a new process and thread </li></ul><ul><li>Code is loaded into memory </li></ul><ul><li>Dynamic link libraries, if used, are loaded </li></ul><ul><li>Data space is allocated and mapped into virtual memory </li></ul><ul><li>Application begins execution by starting the thread </li></ul>
  16. 16. DOS Vs Windows <ul><li>In Dos based programs events are sent directly to the program hence suitable for single tasking system. </li></ul><ul><li>Device dependence </li></ul><ul><li>In Windows programs all events are sent to the Windows OS which in turn distributes these messages to appropriate applications </li></ul><ul><li>Device Independence </li></ul>
  17. 17. SDK
  18. 18. Objectives <ul><li>Understand the structure of a windows program. </li></ul><ul><li>To appreciate the mechanism by which messages are handled in Windows </li></ul><ul><li>Understand resources </li></ul><ul><li>Write the first windows program using </li></ul><ul><li>WIN 32APIs </li></ul>
  19. 19. Windows Program API <ul><li>To program in windows we need to use APIs </li></ul><ul><li>API is a standardize collection of function calls,data structures, and messages that represent an interface to Windows OS services. </li></ul><ul><li>It is written in C Programming language.It exists as a set of macros and type definitions, in addition to data structures and callable routines. </li></ul>
  20. 20. WIN32API <ul><li>WIN 32 </li></ul><ul><li>API </li></ul>WIN 32 Program WINDOWS OS WIN 32 API is a collection of Functions and datastructures to interface to windows oS. Available in KERNEL32.DLL, GDI32.DLL, USER32.DLL
  21. 21. WINDOWS PROGRAM <ul><li>program 1 </li></ul>Mainprogram With Messageloop WINDOW Procedure Event message Queue Systemqueue program1 program2 WINDOWS OS
  22. 22. How a Windows processes the messages <ul><li>Operating system calls the application's WinMain function </li></ul><ul><li>Application creates and registers a window containing a window procedure </li></ul><ul><li>Application runs a message loop, retrieving messages from the message queue </li></ul>
  23. 23. Message Processing
  24. 24. WinMain( ) <ul><li>WinMain function required to do three things </li></ul><ul><ul><li>Register a window class </li></ul></ul><ul><ul><li>Create and display the window </li></ul></ul><ul><ul><li>Process messages </li></ul></ul>
  25. 25. Hungarian notation <ul><li>Convention of naming identifiers in windows programs. </li></ul><ul><li>names precede with letters describing datatype. </li></ul><ul><li>first letter of every word is capitalized </li></ul><ul><li>n-short int </li></ul><ul><li>H-handle </li></ul><ul><li>p-pointer </li></ul><ul><li>fn-function </li></ul><ul><li>lpsz.-long pointer tozero terminated string </li></ul><ul><li>Eg:lpfnWndProc. </li></ul>
  26. 26. First Program <ul><li>#include<windows.h> </li></ul><ul><li>int far PASCAL WinMain(HANDLE hInstance,HANDLE hPrevInstance,LPSTR lpszCmdline, int nCmdShow) </li></ul><ul><li>{ </li></ul><ul><li>MessageBox(0,”Hello”, ”First program”, MB_OK); </li></ul><ul><li>Return 0; </li></ul><ul><li>} </li></ul>
  27. 27. Structure of WNDCLASS <ul><li>UINT style; </li></ul><ul><li>WNDPROC lpfnWndProc; </li></ul><ul><li>int cbClsExtra; </li></ul><ul><li>int cbWndExtra; </li></ul><ul><li>HANDLE hInstance; </li></ul><ul><li>HICON hIcon; </li></ul><ul><li>HCURSOR hCursor; </li></ul><ul><li>HBRUSH hbrBackground; </li></ul><ul><li>LPCTSTR lpszMenuName; </li></ul><ul><li>LPCTSTR lpszClassName; </li></ul>
  28. 28. Concept of HANDLE <ul><li>An unsigned integer assigned by system to an entity like a window, instance of a program and other objects. </li></ul><ul><li>Translate into memory pointers through handle tables maintained by windows. </li></ul>HANDLE MEMORY HANDLE TABLE
  29. 29. Window Functions <ul><li>HWND CreateWindow(LPSTR classname,LPSTR windowname, DWORD style,int x,int y,int width,int height, HWND parent,HMENU menu, HANDLE hist,LPSTR lparam); </li></ul>
  30. 30. Window Functions <ul><li>BOOL ShowWindow(HWND h, int nCmdShow) </li></ul><ul><li>SW_HIDE </li></ul><ul><li>SW_MINIMIZE </li></ul><ul><li>SW_RESTORE </li></ul><ul><li>SW_SHOW </li></ul><ul><li>SW-SHOWMAXIMIZED </li></ul><ul><li>SW_SHOWMINIMIZED </li></ul>
  31. 31. Window Functions <ul><li>Messageloop: </li></ul><ul><li>While(GetMessage(&msg,0,0,0)) </li></ul><ul><li>{ </li></ul><ul><li>TranslateMessage(&msg); </li></ul><ul><li>DispatchMessage(&msg); </li></ul><ul><li>} </li></ul><ul><li>Loop is brokenif WM_QUIT is retieved </li></ul>
  32. 32. Window functions <ul><li>Window procedure is made up of a switch statement which takes action depending on the incoming message. </li></ul><ul><li>switch(message) </li></ul><ul><li>{ </li></ul><ul><li>Case WM_PAINT:…. </li></ul><ul><li>Case WM_COMMAND:…. </li></ul><ul><li>.Default: </li></ul><ul><li>return DefWindowProc(hWnd,msg,wParam,lParam); </li></ul><ul><li>} </li></ul>
  33. 33. DAY 2
  34. 34. Resources <ul><li>Apart from framework all other objects in a window are treated as resources. </li></ul><ul><li>A separate .rc file holds the description of the resource. </li></ul><ul><li>Resource compiler which is required to compile .rc files is built into VC++ IDE and called automatically when a project has resources included </li></ul>
  35. 35. Resources <ul><li>Simplified diagram for the compilation </li></ul><ul><li>Process </li></ul><ul><li>source </li></ul><ul><li>compiler </li></ul><ul><li>linker </li></ul><ul><li>resource </li></ul><ul><li>compiler </li></ul>.res .obj .cpp .exe .rc
  36. 36. Menus <ul><li>Menus are resources added to the project </li></ul><ul><li>Each menu item is associated with a unique ID </li></ul><ul><li>Menu items can be either –pop-up(no id) or </li></ul><ul><li>-nonpopup(with ID) </li></ul><ul><li>Invoking any menu item sends a WM_COMMAND message to the container window </li></ul>
  37. 37. Menus <ul><li>WM_COMMAND holds the menu item which caused it in the WPARAM variable </li></ul><ul><li>Programmer needs to map this to a particular functionality by checking the WPARAM variable </li></ul><ul><li>The process of mapping a message to a functionality is called messagemapping. </li></ul>
  38. 38. Messages <ul><li>Messages along with the message IDs send other relevant information to the windows procedure in WPARAM and LPARAM </li></ul><ul><li>WM_LBUTTONDOWN..stores the x and y coordinates in LPARAM variables as LOWORD and HIWORD and information about any special key pressed simultaneously in WPARAM variable. </li></ul>
  39. 40. MFC Fundamentals
  40. 41. Objectives <ul><li>Appreciate the benefits of MFC </li></ul><ul><li>Know the naming conventions of MFC </li></ul><ul><li>Understand the class hirarchy of MFC classes </li></ul><ul><li>Write programs using Windows/application Architecture </li></ul><ul><li>Understand messaging support of MFC classes </li></ul><ul><li>Study the messageboxes </li></ul>
  41. 42. Object Oriented Programming <ul><li>Class </li></ul><ul><li>Object </li></ul><ul><li>Encapsulation </li></ul><ul><li>Abstraction </li></ul><ul><li>Inheritance </li></ul><ul><li>Polymorphism </li></ul>
  42. 43. MFC fundamentals <ul><li>Microsoft Foundation Library is a system of C++ programming language framework for creating Microsoft Windows based applications </li></ul><ul><li>A set of classes help develop applications easier and quicker </li></ul><ul><li>A frame work is a collection of C++ classes that go beyond a standard C++ library </li></ul><ul><li>MFC is built on top of API </li></ul><ul><li>Many names and conventions familiar to MS Windows SDK programmers are present in MFC </li></ul>
  43. 44. Benefits of MFC <ul><li>Object Oriented </li></ul><ul><li>Encapsulate and classifies API calls into logical classes thus making API easier to handle </li></ul><ul><li>Adv.Prg.tools….Appwizard,class wizard etc. </li></ul><ul><li>Easy to add adv.features such as OLE and ODBC </li></ul><ul><li>Use C++ features such as Exception Handling </li></ul><ul><li>Better portability. </li></ul>
  44. 45. MFC naming Conventions <ul><li>Class names </li></ul><ul><li>C stands for class </li></ul><ul><li>Follow Hungarian notation </li></ul><ul><li>member functions and datamembers </li></ul><ul><li>Capitalize beginning letters </li></ul><ul><li>UpdateAllViews() </li></ul><ul><li>m_ for data members </li></ul><ul><li>m_pModify </li></ul><ul><li>Get and set prefixes on member functions </li></ul>
  45. 46. MFC naming conventions <ul><li>Prefixes for global names: AFX, Afx,afx </li></ul><ul><li>Global identifiers: AFX </li></ul><ul><li>AFX_MSG_MAP </li></ul><ul><li>Global functions </li></ul><ul><li>Afx ::AfxGetApp() </li></ul><ul><li>Global variables afx </li></ul><ul><li>afxDump </li></ul>
  46. 47. Class Hierarchy <ul><li>MFC maintains a class hierarchy,Cobject on the top </li></ul><ul><li>Most classes are derived from Cobject </li></ul><ul><li>It provides basic functionality like </li></ul><ul><li>- Serialization support </li></ul><ul><li>Run time class Information </li></ul><ul><li>Operators like new ,delete </li></ul><ul><li>Functions like Serialize() etc </li></ul><ul><li>Two imp classes for creating application : </li></ul><ul><li>CFrameWnd </li></ul><ul><li>CWinApp </li></ul>
  47. 48. MFC Classes <ul><li>Cobject… </li></ul><ul><li>Application classes </li></ul><ul><li>Window support classes </li></ul><ul><li>File services </li></ul><ul><li>Graphical drawing </li></ul><ul><li>Arrays,lists, maps </li></ul><ul><li>Internet services </li></ul><ul><li>Networking </li></ul><ul><li>Exceptions </li></ul>
  48. 49. Classes not derives from Cobject <ul><li>CTime </li></ul><ul><li>CPoint </li></ul><ul><li>CSize </li></ul><ul><li>CArchive </li></ul><ul><li>CString </li></ul>
  49. 50. Class Hierarchy <ul><li>Cobject </li></ul><ul><li>CCmdTarget </li></ul><ul><li>CWnd </li></ul><ul><li>Cwnd is the base calss from which all windows are derived </li></ul>
  50. 51. Class Hierarchy <ul><li>CObject </li></ul><ul><ul><li>CCmdTarget </li></ul></ul><ul><ul><ul><ul><li>CWinThread </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Cwinapp </li></ul></ul></ul></ul><ul><ul><ul><ul><li>CWinThread supports thread based multitasking </li></ul></ul></ul></ul><ul><ul><ul><ul><li>CWinApp controls start up, initialization, execution and shutdown of program </li></ul></ul></ul></ul>
  51. 52. Creating a window using MFc <ul><li>CFrameWnd is commonly used class derived from CWnd to create SDI applictions </li></ul><ul><li>Each application will have one object derived from CWinApp .When this object is instantiated the application starts running </li></ul><ul><li>MFC requires C++ compiler as it is hierarchy of C++ classes </li></ul><ul><li>afxwin.h </li></ul>
  52. 53. Creating a window using MFC <ul><li>To create an MFC program </li></ul><ul><li>Derive a window from CFrameWnd class </li></ul><ul><li>Override the constructor of the class </li></ul><ul><li>Derive an application class from CWinApp </li></ul><ul><li>Define a messagemap </li></ul><ul><li>Override CWinApp InitInstancre() </li></ul><ul><li>Create an instance of your application class </li></ul>
  53. 54. Creating a window <ul><li>Within the CFrameWnd constructor, which constructs the CWnd object </li></ul><ul><li>call Create(), which creates the windows </li></ul><ul><li>Virtual BOOL Create( </li></ul><ul><li>LPCTSTR lpszClassName, </li></ul><ul><li>LPCTSTR windowname) </li></ul>
  54. 55. CWinApp class <ul><li>InitInstance() creates an instance of the frame </li></ul><ul><li>m_pMainWnd member variable that holds the pointer to the frame. </li></ul><ul><li>And ShowWindow(style) is called </li></ul><ul><li>Override ExitInstance() to do a clean up. </li></ul>
  55. 56. MFC program <ul><li>#include<afxwin.h> </li></ul><ul><li>Class MyWindow:public CFrameWnd </li></ul><ul><li>{public: MyWindow() </li></ul><ul><li>{Create(NULL,”Example Window”);} </li></ul><ul><li>}; </li></ul><ul><li>Class MyApp:public CWinApp </li></ul><ul><li>{Public: BOOL InitInstance() </li></ul><ul><li>{m_pMainWnd=new MyWindow; </li></ul><ul><li>m_pMainwnd->ShowWindow(SW_SHOW); </li></ul><ul><li>return TRUE;} </li></ul><ul><li>}; MyApp appobj; </li></ul>
  56. 57. Messages <ul><li>Applications written for Microsoft Windows are “message driven.” </li></ul><ul><li>In responsible to events such as mouse clicks, Keystrokes, window movements,and so on, Windows sends messages to the proper window. </li></ul><ul><li>MFC encapsulates the message-handling mechanism </li></ul>
  57. 58. Common messages and handlers <ul><li>A message map is a table that maps windows messages to message handlers </li></ul><ul><li>When a message is received by the window procedure the window procedure performs a look up in the message map. </li></ul><ul><li>Depending on the message map it executes the connected handler </li></ul>
  58. 59. Message maps <ul><li>To respond to a message our program must perform these three steps </li></ul><ul><li>The message map macro corresponding to the message map must be added </li></ul><ul><li>The prototype for the message handler must be added </li></ul><ul><li>Implement the message handler associated with the message </li></ul>
  59. 60. Message map <ul><li>Any window that will process messages must include DECLARE_MESSAGE_MAP() </li></ul><ul><li>It is a macro which allows the class to respond to messages </li></ul><ul><li>Use BEGIN_MESSAGE_MAP( ) macro, </li></ul><ul><li>macro entries for each of the message handler functions and the END_MESSAGE_MAP( ) macro </li></ul>
  60. 61. WM_COMMAND message <ul><li>It is sent to the application whenever user makes menu selection </li></ul><ul><li>Along with the message , Message ID that is associated determines the item selected. </li></ul><ul><li>ON_COMMAND(ID,Handler name) is the macro used for mapping </li></ul><ul><li>Handler name is a function name to be executed for the selection </li></ul><ul><li>The handler functions do not have any parameters </li></ul>
  61. 62. Message maps <ul><li>void OnMyMessage(); </li></ul><ul><li>void OnExit(); </li></ul><ul><li>DECLARE_MESSAGE_MAP() </li></ul><ul><li>};//end of CFrameWnd derived class </li></ul><ul><li>BEGIN_MESSAGE_MAP(MyWindow, </li></ul><ul><li>CFrameWnd)//message maps </li></ul><ul><li>ON_COMMAND(ID_MYMESSAGE, </li></ul><ul><li>OnMyMessage) </li></ul><ul><li>ON_COMMAND(ID_EXIT,OnExit) </li></ul><ul><li>END_MESSAGE_MAP() </li></ul>
  62. 63. Message Boxes <ul><li>A Message box displays a message to the user and waits for an acknowledgement </li></ul><ul><li>The purpose of a message box is to inform the user that some event has taken place. </li></ul><ul><li>Prototype: </li></ul><ul><li>int CWnd::MessageBox( </li></ul><ul><li>LPCSTR lpsztext, </li></ul><ul><li>LPCSTR lpsz title= NULL, </li></ul><ul><li>UINT mbtype=MB_OK); </li></ul>
  63. 64. MessageBoxes <ul><li>Here the lpsztext parameter is a pointer to a string that will appear inside the message box </li></ul><ul><li>lpsztitle is used as caption for the box </li></ul><ul><li>The value of mbtype determines the exact nature of the message box </li></ul>
  64. 65. Message box styles <ul><li>MB_OK </li></ul><ul><li>MB_YESNO </li></ul><ul><li>MB_YESNOCANCEL </li></ul><ul><li>MB_RETRYCANCEL </li></ul><ul><li>MB_OKCANCEL </li></ul><ul><li>MB_ABORTRETRYIGNORE </li></ul>
  65. 66. Messagebox icons <ul><li>MB_ICONINFORMATION </li></ul><ul><li>MB_ICONEXCLAMATION </li></ul><ul><li>MB_ICONSTOP </li></ul><ul><li>MB_ICONQUESTION </li></ul>
  66. 67. Possible return values <ul><li>Return values specify the possible outcome of the function </li></ul><ul><li>It return s an integer depending on the user selection on the message box </li></ul><ul><li>It is 0 if there is not enough memory to create a message box </li></ul>
  67. 68. Return values of MessageBox() <ul><li>IDOK </li></ul><ul><li>IDNO </li></ul><ul><li>IDCANCEL </li></ul><ul><li>IDYES </li></ul><ul><li>IDABORT </li></ul><ul><li>IDIGNORE </li></ul><ul><li>IDRETRY </li></ul>
  68. 69. Device Context
  69. 70. Objectives <ul><li>Understand the need for device independence </li></ul><ul><li>Study the CDC class </li></ul><ul><li>Write programs to use the client area </li></ul><ul><li>Understand graphical device independence </li></ul><ul><li>Use GDI objects </li></ul><ul><li>Solutions for the paint problem </li></ul><ul><li>Use virtual window concept to handle paint problem </li></ul>
  70. 71. Device Context <ul><li>DC s are logical constructs used to maintain the integrity of the output </li></ul><ul><li>A Dc is represented by a data structure </li></ul><ul><li>It provides a link between a window based application a device driver an output device such as monitor or printer </li></ul><ul><li>Maintains current info about how to draw or paint to window such as colors, brush or pen pattern, pen width </li></ul><ul><li>Maintains clipping region for a window </li></ul>
  71. 72. CDC class <ul><li>It defines a hierarchy of device context types </li></ul><ul><li>The CDC class encapsulates both the functionality of a window DC and many of the associated graphic routines </li></ul><ul><li>To use a CDC object construct it and then call its member functions </li></ul>
  72. 73. <ul><li>We have several classes derived from CDC </li></ul><ul><li>CWindowDC manages a display context associated with an entire window. </li></ul><ul><li>CClientDC manages a display context associated with the client window. </li></ul><ul><li>CPaintDC manages a display context associated with the invalid portion of the client window. It is used when handling a WM_PAINT MESSAGE </li></ul>The CDC Class
  73. 74. Client area <ul><li>To access the client area of DC , </li></ul><ul><li>it would be created as CClientDC dc(this); </li></ul><ul><li>This DC is used for carrying operations on the client area </li></ul><ul><li>Avaliable functions are </li></ul><ul><li>TextOut(x,y,”string”,string length); </li></ul><ul><li>DrawText(const Cstring &str, </li></ul><ul><li>LPRECT lpRect, </li></ul><ul><li>UINT nFormat); </li></ul><ul><li>nFormat---DT_CENTRE,DT_LEFT etc </li></ul>
  74. 75. Client area <ul><li>COLORREF SetTextColor(COLORREF crClr) </li></ul><ul><li>-COLORREF takes the color value RGB(nr,ng,nb) </li></ul><ul><li>COLORREF SetBkColor(COLORREF crColor) </li></ul><ul><li>MoveTo(x,y) </li></ul><ul><li>LineTo(x,y) </li></ul><ul><li>Ellipse(int x1,int y1,int x2,int y2); </li></ul><ul><li>Rectangle(int x1,int y1,int x2,int y2); </li></ul>
  75. 76. GDI Objects <ul><li>MFC provides graphic object classes equivalent to the drawing tools in Windows like brush,pen etc. </li></ul><ul><li>The available classes and the equivalent windows GDI Handle types are </li></ul><ul><li>CPen…HPEN </li></ul><ul><li>CBrush .HBrush </li></ul><ul><li>CFont..HFONT </li></ul><ul><li>CBitmap …HBITMAP </li></ul>
  76. 77. Using GDI objects <ul><li>Create the object with required attributes </li></ul><ul><li>Select into DC </li></ul><ul><li>The new details in DC are new GDI settings </li></ul><ul><li>CPen *mypen; </li></ul><ul><li>mypen->CretaePen </li></ul><ul><li>(PS_SOLID,5,RGB(255,0,0)); </li></ul><ul><li>CClientDc dc(this); </li></ul><ul><li>dc.SelectObject(mypen); </li></ul><ul><li>dc.LineTo(500,500); </li></ul><ul><li>Similarly a brush can be created using CreateSolidBrush(COLORREF); </li></ul>
  77. 78. Using Fonts <ul><li>CFont..allows to create fonts and select them into DC </li></ul><ul><li>BOOL CreateFont() is used </li></ul><ul><li>Select it into DC </li></ul><ul><li>The text put in the DC will have newly created font </li></ul>
  78. 79. Bitmap <ul><li>Is a graphic image </li></ul><ul><li>Contains bit by bit representation of the image </li></ul><ul><li>Bitmaps are encapsulated by CBitmap class </li></ul><ul><li>We must use an image editor to create bitmap </li></ul>
  79. 80. Displaying bitmap <ul><li>We must create a bitmap object and then load the bitmap resource into this object </li></ul><ul><li>Obtain DC so that our program can output to the window </li></ul><ul><li>Obtain an equivalent memory DC that will hold the bitmap until it is displayed </li></ul><ul><li>CreateCompatibleDC() </li></ul>
  80. 81. Displaying a bitmap <ul><li>Select the bitmap into the memory DC </li></ul><ul><li>SelectObject() </li></ul><ul><li>Copy the bitmap from memory DC to the window DC </li></ul><ul><li>Bitblt() </li></ul>
  81. 82. Load bmp from resource <ul><li>Void CMyWnd::OnBmp() </li></ul><ul><li>{ </li></ul><ul><li>Cbitmap bmp; </li></ul><ul><li>CDC memdc; </li></ul><ul><li>CClientDc dc(this); </li></ul><ul><li>Bmp.LoadBitmap(IDB_BITMAP1); </li></ul><ul><li>memdc,.createCompatibleDC(&dc); </li></ul><ul><li>Memdc.SelectObject(bmp); </li></ul><ul><li>Dc.Bitblt(0,0,1000,1000,&memdc,0,0,SRCCOPY) </li></ul><ul><li>} </li></ul>
  82. 83. The caret <ul><li>A flashing line,blocjk,or bitmap that marks the location of the insertion point in windows client area </li></ul><ul><li>CreateSolidCaret creates a solid block for the system caret and gerts ownership of thecaret </li></ul><ul><li>GetCaretPos() retrieves clentcoordinates of the carets current position </li></ul><ul><li>StCaretPos() moves the caret to the specified position </li></ul><ul><li>HuideCaret() Hies the caret by removing it from the dis[lay screen </li></ul><ul><li>Showcarety() </li></ul><ul><li>shows the caret on the display screen.once shown it starts flashing automatically </li></ul>
  83. 84. WM_PAINT message <ul><li>Window sdoes not keep a rcpord of what a window client are contains </li></ul><ul><li>The contents are ased when the window is resized or overlap[ped </li></ul><ul><li>It is our program’s task to maintain the contents oftthe window </li></ul><ul><li>Toaccomplisht this we use WM_PAINT message which is automatoically senty to our window when oit is resized or overlapped and exposed agsin. </li></ul>
  84. 85. WM_PAINT messagfe <ul><li>OnPaint() is the handler </li></ul><ul><li>Macro,,.ON_WM_PAINT </li></ul><ul><li>DC is obtained by creating an object of type CPaintDC not CClientDC </li></ul><ul><li>Which calls BeginPaint() through constructor and EndPaint through destructor </li></ul>
  85. 86. Solving the repaint problem <ul><li>When we run the program, display some text and then overlay wondoiw with another the content I the client area is lost as the background is repainted </li></ul><ul><li>There are 3 basic methids to solve this problem </li></ul>
  86. 87. Solving the repaint problme <ul><li>Regenerate the output if that output is created by some computational method </li></ul><ul><li>Store a record of diaply events and replay thise events </li></ul><ul><li>Maintain a virtual woindow and copy the contents of the viryual window each time a WM_PAINT message is received </li></ul>
  87. 88. Virual Window theory <ul><li>First a memory DC is created that is compatible with the windowDC </li></ul><ul><li>A bitmap compatible with the WindowDC is created and selected ito it </li></ul><ul><li>Background is made same as r hat of windowDC </li></ul><ul><li>All the output is written to the memoryDC </li></ul>
  88. 89. Virtual Window Theory <ul><li>Each time WM_PAINT message is received the contentrs of memortDC ar copied into the physicalDC causing the output to be displayed on thescreen </li></ul><ul><li>Whenever the physical wiindow is covered and then uncovered a WM_PAINT will be sent tyo our program causing the contents of th veirual window to b copiec to physical window restoring it. </li></ul>
  89. 90. Virtual window –functions used <ul><li>BOOL CreateCompatibleDC(CDC *pdc,int nWidth,int nHeight) </li></ul><ul><li>Initializes a bitmap that is compatible with the device specified by pDC </li></ul><ul><li>Width and height are the dimensions of the bitmap </li></ul>
  90. 91. Virtual window –functions used <ul><li>BOOl PatBlt(int x,int y,int width,int height,DWORD dxRop) </li></ul><ul><li>Some valyues for dxROP </li></ul><ul><li>PATCOPY..copies pattern to destination bitmap </li></ul><ul><li>BLACKNESS turns all output to black </li></ul><ul><li>WHITENESS turns……………..white </li></ul>
  91. 92. Virtual window code <ul><li>CDC memDC ; </li></ul><ul><li>Cbitmap bmp; </li></ul><ul><li>CclientDC DC(this); </li></ul><ul><li>memDC.CreateCOmptileDC(DC); </li></ul><ul><li>Bmp.CreateCOmpatibleBitmap(DC,nWidth,height); </li></ul><ul><li>Memdc.selectobject(bmp) </li></ul><ul><li>Memdc.patblt(x,y,nwidth,nheight,WHITE_NESS);each opration on dc has to bedone on memdcas well </li></ul><ul><li>On receiving WM_PAINT copy memdc ontom DC </li></ul><ul><li>Dc.bitblt(x,y,nwidth,nheigt,&memdc,xsrc,ysrc,PATCOPY); </li></ul>
  92. 93. CHAPTER4 <ul><li>APPLICATION FRAMEWORK </li></ul>
  93. 94. objectives <ul><li>To understand the docview architecture </li></ul><ul><li>Know about callses involved </li></ul><ul><li>Using appwizard </li></ul><ul><li>Understand classwoizard </li></ul><ul><li>Appreciate framework support for serialization and painting </li></ul><ul><li>Use metafiles to handle paint problem </li></ul>
  94. 95. Application Framework <ul><li>Is an integrated collction of object oriented software components that offers all that is needed for a generc application </li></ul><ul><li>Taken together thae calsse in MFc library make up an application framework..the frame work on which you build an application forwindows </li></ul>
  95. 96. Application framework <ul><li>Five classes represent the architectuaral ccomponts of anMFc app;lication </li></ul><ul><li>Cdoc </li></ul><ul><li>CFrameW </li></ul><ul><li>CWinAp </li></ul><ul><li>Cview </li></ul><ul><li>Template (single/multi)which ties all the calsses together </li></ul><ul><li>The applacation generated using this approach is called as Doc/View architecture </li></ul>
  96. 97. Doc-View Arch// <ul><li>Data is separated from the presentation </li></ul>
  97. 98. Doc/view <ul><li>Document refers to any or allforms of data </li></ul><ul><li>View refers to the physical representation of data </li></ul><ul><li>Mapping of document to view is oneto many </li></ul>
  98. 99. Framework <ul><li>Application aclss represents the application as a whole ans manages instance inityializationn,message loop and instance clean up </li></ul><ul><li>Document template class..binds together the frame,view,doc and a set of application resources </li></ul>
  99. 100. Frame work <ul><li>Framewindow class represents the borders of the window </li></ul><ul><li>The view window class rpresents the client area of the application </li></ul>
  100. 101. Framework <ul><li>Frameworkdoc view clases automate the storage of documents tpo disk filrs known as serialization The paint process is automated by use of Ondraw() method These two functions ar overloaded to achieve the desired functionality. </li></ul>
  101. 102. Framework <ul><li>DECLARE_DYNCREATE mnacro enavles objects Cobject derived classes to be created dynamically at run time </li></ul><ul><li>The framework uses this ability to cretae new objects dynamically </li></ul>
  102. 103. Doc/View code <ul><li>BOOL CsampeApp::InitInstance() </li></ul><ul><li>{ </li></ul><ul><li>SetRegistryKey(_T(“local appwizard genrated applications)); </li></ul><ul><li>CsingleDocTemplate *pDocTemplate; </li></ul><ul><li>pDocTemplate;=new CSingleDocTemplate( </li></ul><ul><li>IDR_MENU!, </li></ul><ul><li>RUNTIME_CLASS(CSampleDoc), </li></ul><ul><li>RUNTIME_CLASS(CmainFrame) </li></ul><ul><li>RUNTIME_CLASS(CSampleView)); </li></ul><ul><li>AddDocTemplate(pDocTemplate); </li></ul>
  103. 104. DocvIew code <ul><li>//parse command line for standared shell commands,Dde,fileopen </li></ul><ul><li>CCommandLineInfo cmdinfo; </li></ul><ul><li>ParseCommandLine(cmdinfo); </li></ul><ul><li>//dispatch commnds specifuied on the //commndline </li></ul><ul><li>If(!ProcessShellCommand(cmdinfo)) </li></ul><ul><li>Return FA:LSE; </li></ul><ul><li>//the one and only window has been initialized ,so show and update it </li></ul><ul><li>M_pmainWnd->showwindow(SW_SHOW); </li></ul><ul><li>mpMainWnd->Updatewindow(); </li></ul><ul><li>Return TRUE; </li></ul>
  104. 105. Using VC++ wizrds <ul><li>VC++ supports wizards to create different tupes of applications </li></ul><ul><li>Applications of MFC exe or DLL </li></ul><ul><li>Active-x applications etc, </li></ul>
  105. 106. Appwizard <ul><li>It creates the framework for a window basred C++ application that uses the MFC library which incorporats the doc.view architecture </li></ul><ul><li>It can create only new application </li></ul><ul><li>It cannot modify the strucure of an existing application </li></ul>
  106. 107. Appwizard <ul><li>The files generated by appwizard include :source files,header files,resource files,module definition file a project file </li></ul><ul><li>A projct created buy appwizard provides a varietynof a functionality </li></ul>
  107. 108. The project workspace <ul><li>Provides info about our project </li></ul><ul><li>It offers a method for navigation among the components of our project </li></ul><ul><li>It offers 4 project viewing options </li></ul><ul><li>Fileview </li></ul><ul><li>Classview </li></ul><ul><li>Resource view </li></ul><ul><li>Info view </li></ul>
  108. 109. classwizard <ul><li>It allows us to create new classes ,add,delete, and edit the message handlers in our application classes </li></ul><ul><li>It wtites prototupes,the function bodies and the code to link the windows messgae to the function </li></ul><ul><li>The accelerator key combination for classwizaed is ctrl+w </li></ul>
  109. 110. classwizard <ul><li>It contains the foll.property pages </li></ul><ul><li>Messagemaps </li></ul><ul><li>Membervariables </li></ul><ul><li>Automation </li></ul><ul><li>Activez events </li></ul><ul><li>classinfo </li></ul>
  110. 111. Serialization <ul><li>Serializations is the process of storing (writing)and loading (reading)objects and data.Most commonlyobjects are written from memory to the harddisk ansd viceversa </li></ul><ul><li>Cdocument contyains the info(data &objects) to be seroialized </li></ul><ul><li>Carchive provides a context for serialization </li></ul>
  111. 112. Serialization <ul><li>Cfile object is attached to the Carchive </li></ul><ul><li>A Carchive object can be used for storing and loading the data </li></ul><ul><li>The file dialog autyomatically invokes the serialize function </li></ul>
  112. 113. Serialization <ul><li>Two member functions oif Carchive are </li></ul><ul><li>IsLoading(0 </li></ul><ul><li>Isstoring() </li></ul><ul><li>Carchive overloads the << and >> operators for the built in datatypes and the class types defined </li></ul><ul><li>Carchive is implicitly created by the framework from the file duialogbox </li></ul>
  113. 114. Ondraw() <ul><li>OnDraw() is a virtual member function of the Cview class that the application framework calls everytime the view window needs to be repainted </li></ul><ul><li>A window needs repainting if the user resizes the window or reveals a previously hidden oartb of window or if the applicatyion cahanges the window’s data </li></ul><ul><li>If the user resizes the window or reveals a hidden area the application framework calls Ondraw(0 </li></ul>
  114. 115. OnDraw() member function <ul><li>If a function in our program xchanges the data, it must inform windows of the change to update the view by calling the view’s inherited Invalidate() or nvalidateRect() member function </li></ul><ul><li>Tjis call to Invalidate triggers a later call to ondraw() </li></ul><ul><li>OnDraw uses CPaintDc instead of cClientDc for painting </li></ul><ul><li>All functions applkied to CClierntDC apply to cPaintDc with few exceptions. </li></ul>
  115. 116. Invalid Rectangle <ul><li>An invalid region is a collection of polygonal areas that will require painting </li></ul><ul><li>The invalid rectangle for a window is the minimum rectangular area that encompasses the invalid region </li></ul><ul><li>Associatec functions </li></ul><ul><li>Invalidate()//invalidates the entire client area </li></ul><ul><li>Void CWnd::InvalidatERect(LPCRECT lpRect,BOOL berase TRUE) </li></ul><ul><li>//invalidates the client area in the specified rect </li></ul><ul><li>//berase specifies whether the background has to be erased </li></ul>
  116. 117. Rubberbanding <ul><li>A technique of showing the trail of an image as the mouse is dragged to draw </li></ul><ul><li>Achieved by drawing the previous line with the inverse so that it is erased and drawing the new line with the inverse color of the background in the WM_MOUSEMOVE event </li></ul><ul><li>SetROp2(ndrawMode)-used to set the drawing mode </li></ul><ul><li>Values of Ndrawmode </li></ul><ul><li>R2_BLACK,R2_WHITE,R2_NOP,R2_NOT </li></ul>
  117. 118. rubberbanding <ul><li>OnMouseMove() </li></ul><ul><li>{ </li></ul><ul><li>cClientDc dc(this); </li></ul><ul><li>Dc.SetRop2(R2_NOT); </li></ul><ul><li>Dc.MoveTo(startx,starty); </li></ul><ul><li>Dc.LineTo(prevx,prevy); </li></ul><ul><li>Dc.MoveTo(startx,starty); </li></ul><ul><li>Dc.lineto(curx,cury); </li></ul><ul><li>Prevx=cux; prevy=cury;} </li></ul>
  118. 119. Metafiles <ul><li>Another answer to painting problem </li></ul><ul><li>A method to create disk based image files. </li></ul><ul><li>.wmf files store graphic object using a macro like logic ie stores statements like lineto,ellipse along with drawing attributes which can be played back to displaty the image </li></ul><ul><li>Occupy very less disk place </li></ul><ul><li>Used to store complicated drawings </li></ul><ul><li>Tend to be slow if the picture is large </li></ul>
  119. 120. Cview class derivatives <ul><li>Appwizard generates an application deriving its view from the Cview class </li></ul><ul><li>MFC supports derived classes from Cview which give particular functionality to the view </li></ul><ul><li>CeditView: notepad functionality </li></ul><ul><li>CFormView: allows cpontrols to be placed on the view thus the view behaves like a dlg box </li></ul>
  120. 121. Dialogboxes
  121. 122. Objectives <ul><li>To apprecviate the need for dlgboxes </li></ul><ul><li>Study types of dlgboxes </li></ul><ul><li>Write programs to cvreate modal and modeless dlgboxes </li></ul><ul><li>Understnand the data excvhane mechanisms bet. Contyrols and class variables </li></ul><ul><li>Write code to individully manipulate the control propertuies </li></ul><ul><li>Use propertuy sheets oin applications </li></ul>
  122. 123. dialogboxes <ul><li>A dialogbox is awindow that provides more flexible means by which thwe user can interact with the application </li></ul><ul><li>A dlg box interacts with the user throughj one or more controls </li></ul><ul><li>A control is a specific type of input or output window </li></ul>
  123. 124. Dialogboxes <ul><li>Windows support several basic controls includeing pushbuttons,checkboxes,radiobuttons </li></ul><ul><li>List boxes,combo boxes,scroll bars and static controls </li></ul><ul><li>There are two types of dialog boxes </li></ul><ul><li>Modal: cannot be bypassed and interacted with the application which created it </li></ul><ul><li>Modeless..can be bypassed and interact with the application </li></ul>
  124. 125. Dialogbox classification <ul><li>System </li></ul><ul><li>modal(Filedialog,font dialog) </li></ul><ul><li>modeless(find&replace,spellcheck) </li></ul><ul><li>User defined </li></ul><ul><li>modal </li></ul><ul><li>Modeless </li></ul><ul><li>Dialogboxes are creatyed as resources </li></ul><ul><li>Managed by a class derived from CDialog </li></ul>
  125. 126. Creating dialogboxes <ul><li>Programmer need to create an association between ID if dialog resource and the class used to manage the dialog </li></ul><ul><li>Class wizard allows us to add new classes to the application using the dialog resource </li></ul><ul><li>As the base </li></ul><ul><li>Cdialog members: </li></ul><ul><li>DoModal()..creates modal dialogbox </li></ul><ul><li>Create() creates modeless dialogbox </li></ul><ul><li>Create(UINT nIDtemplate,Cwnd *pParentWnd=NULL); </li></ul><ul><li>OnInitDialog()..when the dialog is being initialized </li></ul>
  126. 127. Creating a Dialogclass <ul><li>Add a dialog resource using the Insert-resource-dialog option </li></ul><ul><li>Add the required controls from the toolbar </li></ul><ul><li>Keeping the dialog open,invoke the class wizard </li></ul><ul><li>Wizard will prompt asking for creating a new Dialog </li></ul>
  127. 128. Creating a Dialog class <ul><li>Give a name to the Dialog calls to be associated with the dialog resource </li></ul><ul><li>Make sure the base class isv Cdialog </li></ul><ul><li>And the id of your dialog box is selected </li></ul>
  128. 129. Dialog Data Exchange <ul><li>The exchange of data between dialog controls and the program is through member variables which are mappedc onto each of the control by class wizard </li></ul><ul><li>TRUE </li></ul><ul><li>FALSE </li></ul><ul><li>This mechanism is called DDX-Dialog Data Exchange </li></ul><ul><li>Acieved by the function DodataExchange() </li></ul>Dialogbox Member variables
  129. 130. 0

×