. We know that the entry function in the C language is the main () function, so it is also the main () function when writing Windows applications? Of course not. We use the WinMain () function, but we did not see this function when writing the MFC program. In fact, this is Microsoft's attempt to help us quickly write a Windows-based application, we can also encapsulate it. Here, we try to write a WinMain () function by ourselves. Now, let's go to the topic! First, we need to include some libraries.
#include <windows.h>#include <stdio.h>
Also, we should learn to use msdn to quickly find the information we need.
Here, windows is a message-based application. Program First, we should compile a message response function, that is, a callback function.
LRESULT CALLBACK WinMYProc( HWND hWnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter);
Next is WinMain.
Int WINAPI WinMain (HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // command line int nCmdShow // show state) {WNDCLASS wndcls; // register wndcls. cbClsExtra = 0; wndcls. cbWndExtra = 0; wndcls. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); // wndcls for window painter. hCursor = LoadCursor (NULL, IDC_ARROW); // wndcls. hIcon = LoadIcon (NULL, IDI_WINLOGO );/ /Icon wndcls. hInstance = hInstance; // window instance wndcls. lpfnWndProc = WinMYProc; // Window Function wndcls. lpszClassName = "BEYOND"; // window class name wndcls. lpszMenuName = NULL; // menu name (empty here) wndcls. style = CS_HREDRAW | CS_VREDRAW; // window style (horizontal and vertical redrawing) ReGISterClass (& wndcls); // registration window // after registration, the following window is created and displayed: HWND hWnd; hWnd = CreateWindow ("BEYOND", lovebeyond ", WS_OVERLAPPEDWINDOW, 600,400, NULL, NULL, hInstance, NULL); ShowWindow (hwnd, SW_SHOWNORMAL); // display UpdateWindow (HWnd); // update // next, the message loops through MSG msg; while (GetMessage (& msg, NULL,) // when it is not WM_QUIT, continue message loop {TranslateMessage (& msg); // used to translate message DispatchMessage (& msg); // send message} return msg. wParam; // return a parameter} // message response function lresult callback WinMYProc (HWND hWnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam // second message parameter) {switch (uMsg) {case WM_CHAR: // Keyboard message char szChar [20 ]; Sprintf (szChar, "char is % d", wParam); MessageBox (hWnd, szChar, "char", 0); break; case WM_LBUTTONDOWN: // left-click MessageBox (hWnd, "mouse clicked", "message", 0); break; case WM_PAINT: // redraw HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint (hWnd, & ps); TextOut (hDC, "lovebeyond", strlen ("lovebeyond"); EndPaint (hwnd, & ps); break; case WM_CLOSE: // close the message if (IDYES = MessageBox (hWnd, "Do you want to end? "," Message ", MB_YESNO) {DestroyWindow (hWnd);} break; case WM_DESTROY: // destroy window message PostQuitMessage (0); break; default: // return DefWindowProc (hWnd, uMsg, wParam, lParam);} return 0 ;}
By now, when all programs are finished, readers can try to write similar programs and add more response functions. This ends. I hope to help you understand the windows program.