Windows API: In addition to coordinating application execution, allocating memory, and managing resources, Windows Multi-job system... In addition, it is also a large service center that calls various services of this Service Center (each service is a function ), it can help applications enable Windows, depict graphics, and use peripheral devices. Because these function services are intended for applications, they are called application programming interfaces, API functions. Win32 API is the Application Programming Interface of Microsoft Windows 32-bit platform. Any application executed in a Windows operating environment can call the Windows API. MFC: MFC is a combination of win api and C ++. It is the programming language interface for Windows applications provided by Microsoft and is a software programming specification, but not a programming language, it allows users to use a variety of third parties (for example, I am a party, Microsoft is a party, Borland is a third party) programming language for the development of Windows applications, so that these developed applications can run in windows, such as VB, VC ++, Java, the dehpi programming language functions are all derived from APIs in nature. Therefore, all applications developed using them can work in the message mechanism and drawing of windows, and comply with the internal implementation of windows as an operating system, this is actually a necessity. If Microsoft does not provide APIs, there will be no work on win programming in this world, and Microsoft's products will quickly change from fashion to garbage, as mentioned above, MFC is Microsoft's dedicated C ++ encapsulation for API functions. This combination allows users to use Microsoft's professional C ++ It is easy to develop win applications using sdks. Because MFC encapsulates APIs, Microsoft has done a lot of work, it hides a lot of details of program developers compiling software with C ++ & MFC in win, such as application message processing and device environment plotting, this kind of combination is for convenience and has to pay a certain price (this is Microsoft's style of work). As a result, MFC has caused a certain degree of redundancy and detour in class encapsulation, but this is acceptable ..Let's look at a program. This program creates a white window and outputs "Hello, world!" at 10 and 10 !" . Windows API implementation:
C code # include <windows. h> /* Declare the window process */ Lresult callback windowprocedure (hwnd, uint, wparam, lparam );/* Window class name */ Tchar szclassname [] = text ("windowsapp "); /* Entry function */ Int winapi winmain ( Hinstance, // instance handle Hinstance hprevinstance, // The previous instance. It is always null in Win32. Lpstr lpcmdline, // command line parameters Int ncmdshow // display command parameters in the window ) { Hwnd;/* window handle */ MSG messages;/* message structure variable */ Wndclass wincl;/* window class structure variable */ /* Fill in the window class structure variable */ Wincl. hinstance = hinstance; Wincl. lpszclassname = szclassname; Wincl. lpfnwndproc = windowprocedure;/* set the window process */ Wincl. Style = cs_dblclks;/* window style */ /* Set the default icon cursor */ Wincl. hicon = loadicon (Null, idi_application ); Wincl. hcursor = loadcursor (Null, idc_arrow ); Wincl. lpszmenuname = NULL;/* menu */ Wincl. cbclsextra = 0;/* Number of extra window data bytes */ Wincl. cbwndextra = 0;/* additional data bytes in the window */ /* Default window background */ Wincl. hbrbackground = (Hbrush) getstockobject (white_brush ); /* Registration window class */ If (! Registerclass (& wincl )) Return 0; /* Create a window */ Hwnd = createwindow ( Szclassname,/* class name */ Text ("Win32 API"),/* Title text */ Ws_overlappedwindow, /* Window style */ Cw_usedefault,/* x coordinate */ Cw_usedefault,/* Y coordinate */ Cw_usedefault,/* width PX */ Cw_usedefault,/* Height PX */ Hwnd_desktop,/* parent window handle */ Null,/* menu */ Hinstance,/* program instance handle */ Null/* Window Creation parameters */ ); /* Display the window by parameters */ Showwindow (hwnd, ncmdshow ); /* Enter the message loop until getmessage () returns 0. That is, wm_quit message is sent */ While (getmessage (& messages, null, 0, 0 )) { /* Translate a virtual key message into a character message */ Translatemessage (& messages ); /* Send a message to the window */ Dispatchmessage (& messages ); } Return (INT) messages. wparam; } /* Implementation of the window process */ Lresult callback windowprocedure (hwnd, uint message, wparam, lparam) { Switch (Message) { Case wm_paint: { Paintstruct pS; HDC = beginpaint (hwnd, & PS ); Tchar SZ [] = text ("Hello, world! "); Textout (HDC, 10, 10, SZ, lstrlen (sz )); Endpaint (hwnd, & PS ); } Break; Case wm_destroy: Postquitmessage (0);/* Send wm_quit message to the Message Queue */ Break; Default:/* messages that are not interest are sent to defwindowproc () for processing */ Return defwindowproc (hwnd, message, wparam, lparam ); } Return 0; } MFC implementation:
C ++ Code # include <afxwin. h> /* Custom window */ Class cmywnd: Public cframewnd { Public: Cmywnd (); Protected: Void onpaint (); /* Declare message ing */ Declare_message_map () }; Cmywnd: cmywnd () { /* Create a window */ Create (null, text ("MFC frame window ")); }/* Redraw function */ Void cmywnd: onpaint () { Cpaintdc DC (this ); DC. textout (10, 10, text ("Hello, world! ")); } /* Message ing */ Begin_message_map (cmywnd, cwnd) On_wm_paint () End_message_map () /* Define the application class */ Class cmyapp: Public cwinapp { Public: Bool initinstance (); }; /* Unique application instance */ Cmyapp theapp; Bool cmyapp: initinstance () { M_pmainwnd = new cmywnd (); M_pmainwnd-> showwindow (m_ncmdshow ); M_pmainwnd-> updatewindow (); /* Return true to enter the message loop */ Return true; }
Obviously, using MFC can significantly reduce the amount of written code. But because of this, you will feel confused when learning. You will find that the winmain function is invisible in MFC and the window process is not found. Actually, they are encapsulated in the MFC class. The implementation of Windows APIS is clear, though not difficult, but cumbersome. There are few written code for MFC implementation, but the process is not clear. Although it seems simple, it is hard to learn .. The difficulty of MFC is here .. |