Windows application:
WinMain function (entry function):
1. Design window class, register window class; wndclass
2. Create Windows, display and update windows;
3. Message loops;
Window procedure function (callback function): WindowProc
Ps:dos Program entry function for the Main,windows program entry is WinMain, which is called directly by the system.
Windows program run Process
#include <windows.h> #include <stdio.h>lresult CALLBACK winsunproc (//Window procedure function (callback function) declares HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM); int WINAPI WinMain (//winmain function, Windows application portal HInstance hinstance, HINST Ance hprevinstance, LPSTR lpcmdline, int ncmdshow) {wndclass wndcls; Instantiate a window class object and set individual properties Wndcls.cbclsextra = 0;wndcls.cbwndextra = 0;wndcls.hbrbackground = (hbrush) getstockobject (BLACK_ BRUSH); wndcls.hcursor = LoadCursor (null,idc_cross); Wndcls.hicon = LoadIcon (null,idi_error); wndcls.hinstance = Hinstance;wndcls.lpfnwndproc = Winsunproc;wndcls.lpszclassname = "Hepengtao"; wndcls.lpszmenuname = NULL;wndcls.style = Cs_hredraw | Cs_vredraw; RegisterClass (&WNDCLS); Register window class hwnd Hwnd;hwnd = CreateWindow ("Hepengtao", "window caption", Ws_overlappedwindow,//Create window 0,0,600,400,null,null,hinstance , NULL); ShowWindow (Hwnd,sw_shownormal); Display window UpdateWindow (HWND); Update window MSG msg;while (GetMessage (&msg,null,0,0))//Remove message from Message queue, return 0 when Wm_quit message is obtained{TranslateMessage (&msg);//When the keyboard key is pressed, the system gets wm_keydown and Wm_keyup message, which is transformed into WM_CHAR message dispatchmessage (&MSG); Pass the resulting message to the callback function}return 0;} LRESULT CALLBACK Winsunproc (//Define window procedure function (callback function) HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {switch (umsg) {case WM_CHAR://keyboard pressed letter, send WM_CHAR message CHAR szchar[20];sprintf (Szchar, "char is%d", wParam); MessageBox (Hwnd,szchar, "popup title", 0); Break;case wm_lbuttondown://left mouse button pressed, send WM_LBUTTONDOWN message MessageBox (hwnd, "mouse Clicked "," Pop-up window title ", 0); HDC HDC;HDC = GetDC (hwnd); TextOut (hdc,0,50, "I am Hepengtao.", strlen ("I am Hepengtao."); ReleaseDC (HWND,HDC); Break;case wm_paint://Window redraw, send WM_PAINT message hdc hdc; Paintstruct PS;HDC = BeginPaint (HWND,&PS); TextOut (hdc,0,0, "hello,world!", strlen ("hello,world!")); EndPaint (HWND,&PS); Break;case wm_close://Close button pressed, send Wm_close message if (Idyes = = MessageBox (hwnd), "OK to close the window?" "," popup title ", Mb_yesno)) {DestroyWindow (HWND);//Destroy window and send wm_destory message}break;case Wm_destroy://execute Destorywindow () function, send Wm_destory message postquitmessage (0); //Program request exit, send Wm_quit message Break;default:return DefWindowProc (Hwnd,umsg,wparam,lparam); The user does not care about the message, which is handled by the operating system by default}return 0;}
Windows program run Process