Reference Xinxin Sun "VC + + in-depth detailed"
Steps to create a WIN32 program:
1. Write WinMain function
2. Design window class (WNDCLASS)
3. Registration window class
4. Create a window
5. Display and update the window (ShowWindow (hwnd,sw_shownormal); UpdateWindow (HWND);)
6. Message loop (constantly fetching and responding to messages from message queues)
7, window procedure function (processing the message sent to the window)
The test code is as follows (compiled by VS2010):
#include <stdafx.h> #include <windows.h> #include <stdio.h>lresult CALLBACK winproc (HWND hwnd,uint Umsg,wparam wparam,lparam LPARAM); int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpcmdline, int n ShowCmd) {//Design a window class wndclass Wndcls;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 = Winproc;wndcls.lpszclassname = _t ("window"); wndcls.lpszmenuname = Null;wndcls.style = Cs_hredraw | Cs_vredraw; RegisterClass (&WNDCLS);//Create a window that defines a variable to hold the handle returned after the successful creation of the window hwnd Hwnd;hwnd = CreateWindow (_t ("window"), _t ("window"), ws_overlappedwindow,0,0,600,400,
Null,null,hinstance,null);//Display and Refresh Window ShowWindow (hwnd,sw_shownormal); UpdateWindow (HWND);//define message structure body, start message loop msg msg;while (GetMessage (&msg,null,0,0)) {TranslateMessage (&msg);D Ispatchmessage (&msg);} return Msg.wparam;} Window procedure function lresult CALLBACK winproc (HWND hwnd,uint umsg,wparam wparam,lparam LPARAM) {switch (umsg) {case Wm_char:char szchar[20];sprintf_s (Szchar, "You pressed%d", wParam); MessageBoxA (Hwnd,szchar, "char", 0), Break;case wm_lbuttondown:messagebox (hwnd,_t ("Mouse clicked"), _t ("message"), 0) ; HDC HDC;HDC = GetDC (hwnd); TextOut (hdc,0,100,_t ("wm_lbuttondown message"), strlen ("wm_lbuttondown message")); ReleaseDC (hwnd, HDC); break;case WM_PAINT:HDC Paint_hdc; Paintstruct PS; PAINT_HDC = BeginPaint (HWND,&PS); TextOut (paint_hdc,0,0,_t ("WM_PAINT message"), strlen ("WM_PAINT message")); EndPaint (HWND,&PS); Break;case wm_close:if (Idyes = MessageBox (hwnd,_t ("Is it really over? "), _t (" message "), Mb_yesno)) {DestroyWindow (HWND);} Break;case wm_destroy:postquitmessage (0); Break;default:return DefWindowProc (Hwnd,umsg,wparam,lparam);} RetuRN 0;}
1. For the following statement:
Char szchar[20];
sprintf_s (Szchar, "You pressed%d", wParam);
MessageBox (Hwnd,szchar, "char", 0);
Compile error: Cannot convert parameter 2 from "Char [20]" to "LPCWSTR"
This is because the project uses the Unicode character set, which can be converted using the _t (""), but _t ("") does not work on the variable.
Changes the MessageBox to MessageBoxA, and the argument is a short character.
Run