#include
//Windows.h header file contains most Windows API functions and data structures
// Declaration Window Processing Function LRESULT CALLBACK WNDPROC (HWND, UINT, WPARAM, LPARAM);
/ / WinMain function is responsible for creating a form and creation message loop: int WinApi WinMain (Hinstance Hinstance, // Application Current instance handle Hinstance HPREVINSTANCE, / / application Other instance handle, often nullpstr iplmdline, // pointing to program line parameters Pointer int nshowcmd) // Identify the application to start execution window display mode {hwnd hwnd; // window handle MSG Message; // message name WNDCLASS WNDCLASS; / / Define window structure
Char * lpszclassname = "wnd"; // Window class name char * lpsztitle = "wndframe"; // window title
// The following are the members to pay value Wndclass Wndclass.style = 0; Wndclass.lpfnWndProc = WndProc; // the specified window message handler Wndclass.cbClsExtra = 0; Wndclass.cbWndExtra = 0; Wndclass.hInstance = hInstance; Wndclass.hIcon = LoadIcon (NULL, IDI_ASTERISK); Wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM); Wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); Wndclass.lpszMenuName = NULL; Wndclass.lpszClassName = lpszClassName;
IF (! registerclass (& wndclass)) // Try to register the window {messagebeep (0); // Ring RETURN FALSE;}
// call the CreateWindow () function to create a window hwnd = CreateWindow (lpszClassName, lpszTitle, WS_VSCROLL | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, SW_SHOWMAXIMIZED); // display window UpdateWindow (hwnd); // refresh the screen while (GetMessage (& Message, NULL, 0,0)) // setup message loop {TranslateMessage (& Message); DispatchMessage (& Message);} return Message.wParam;} // window function LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {char * szMsg = "Hello"; switch (message) {case WM_DESTROY: PostQuitMessage (0); break; Case WM_LButtondown: MessageBox (Hwnd, "You Click LEFT button", SZMSG, MB_OK) BREAK; Case WM_RBUTTONDOWN: MessageBox (hwnd, "You hit right-click!", szmsg, mb_ok;
DEFAULT: RETURN DEFWINDOWPROC (HWND, Message, WPARAM, LPARAM);} Return (0);} Appendit: The handle is the unique identifier in the system in the system, the common handle is: hwnd window handle HDC device Environment handle Hinstance Current instance handle HBitmap bit HCURSOR cursor handle HICON icon handle HFONT font handle HMENU menu handle HBrush brush handle
Definition of the MSG structure: typedef struct tagmsg {// msghWnd hwnd; uint message; wparam wparam; lparam lparam; dword time; point; Pt;} MSG; MSG structure contains message information in the message queue of the thread. Members: hwnd: The handle of the window to which the window of the received message is identified. Message: Specifies the message number. WPARAM: Specifies additional information of the message. The specific meaning is related to the value of Message members. LPARAM: Specifies additional information for messages. The specific meaning is related to the value of Message members. Time: Specifies the time to make a message. PT: Specifies the screen coordinates of the message when the message is issued.
The definition of the WndClass data structure: typef tagwndclass {uint style; // window class style, generally 0WndProc LPfnWndProc; // Pointer INT CBCLSEXTRA, pointing to window functions INT CBCLSEXTRA; / / INT CDWndextra; / / Assign bytes after the window class instance INSTANCE HINSTANCE; // Application Example Handle Hicon Hicon; // Window Class Icon HcURSOR HCURSOR; // Window Cursor HBrush HBRBACKGROUND; // Window Class Background Brush LPCTSTR LPSZMENUNAME; // Window class menu resource name lpctstr lpszclassname; // window class name} WNDCLASS;
CreateWindow () function prototype: hwnd createwindow (lpctstr lpszclassname, // window class name LPCTSTR LPSZTILE, / / Title DWORD DWSTYLE, // Window Style INT X, // Coordinate XINT Y, / / Coordinate Y Int Nwidth, // Wide INT Nweight, // High HWND HWNDPARENT, / / Parent Window Handle Hinstance Hinstance, // Application Handle LPVOID LPPARAM) // CreateSTRECT Structure Pass the value of the window