VS2013 wrote the first Win32 program-hello World.
Through this program:
· Familiar with how to create a Window under MFC
· Learn how Windows programs work.
Cough, that's serious, recently our dorm is working on a small game written in Visual C + +.
The operating platform we use is Visual Stdio 2013.
I thought writing a Hello world would be as simple as C + +, but after a preliminary look at how the Windows program works, creating a form becomes a problem between us.
Demo.cpp: Defines the entry point for the application. #include "stdafx.h" #include "resource.h" #define max_loadstring 100//global variable: hinstance hinst;//current instance Tchar sztitle[max_ loadstring];//title bar Text tchar szwindowclass[max_loadstring];//main window class name Char *showtext;//the forward declaration of functions contained in this code module: Atommyregisterclass (hinstance hinstance); Boolinitinstance (HINSTANCE, int.); LRESULT Callbackwndproc (hwnd, UINT, WPARAM, LPARAM); Int_ptr Callbackabout (hwnd, UINT, WPARAM, LPARAM); INT apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpCmdLine, int ncmdshow) {unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine); TODO: Place the code here. MSG msg; Haccel hacceltable;//Initialization of global string LOADSTRING (HInstance, Ids_app_title, SzTitle, max_loadstring); LoadString (HInstance, Idc_consoleapplication6, Szwindowclass, max_loadstring); MyRegisterClass (HINSTANCE);//Execution of application initialization: if (! InitInstance (HINSTANCE, nCmdShow)) {return FALSE;} hacceltable = Loadaccelerators (hinstance, MakeintRESOURCE (IDC_CONSOLEAPPLICATION6));//main message loop: while (GetMessage (&msg, NULL, 0, 0)) {if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg);D ispatchmessage (&msg);}} return (int) Msg.wparam;} Function: MyRegisterClass ()////Purpose: Registers the window class. ATOM MyRegisterClass (hinstance hinstance) {wndclassex wcex;wcex.cbsize = sizeof (wndclassex); wcex.style= CS_HREDRAW | cs_vredraw;wcex.lpfnwndproc= wndproc;wcex.cbclsextra= 0;wcex.cbwndextra= 0;wcex.hinstance= hInstance;wcex.hIcon= LoadIcon (HInstance, Makeintresource (Idi_consoleapplication6)); wcex.hcursor= loadcursor (NULL, IDC_ARROW); Wcex.hbrbackground= (Hbrush) (color_window+1); wcex.lpszmenuname= makeintresource (IDC_CONSOLEAPPLICATION6); Wcex.lpszclassname= szwindowclass;wcex.hiconsm= LoadIcon (wcex.hinstance, Makeintresource (IDI_SMALL)); return RegisterClassEx (&WCEX);} Function: InitInstance (hinstance, int)////Purpose: Save the instance handle and create the main window////Note:////in this function, we save the instance handle in the global variable and//create and display Display the main program window. BOOL InitInstance (HINStance hinstance, int ncmdshow) {HWND hwnd; HInst = hinstance; Store instance handles in global variables hWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, 0, Cw_usedefault, 0, NULL, NULL, HINSTANCE, NULL); if (!hwnd) {return FALSE; } ShowWindow (HWnd, ncmdshow); UpdateWindow (HWND); return TRUE;} Functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Handles the message of the main window. wm_command-processing Application Menu//wm_paint-Draw main window//wm_destroy-send exit message and return////lresult CALLBACK WndProc (HWND hwnd, UINT message , WPARAM WPARAM, LPARAM LPARAM) {int wmid, wmevent; Paintstruct PS; HDC hdc;switch (message) {case wm_lbuttondown:showtext = "Hello world!"; I Nvalidaterect (HWnd, NULL, 1), Case wm_command:wmid = LoWord (wParam); wmevent = HiWord (WParam);//Analysis Menu selection: switch (wmid) {CA Se idm_about:dialogbox (hInst, Makeintresource (Idd_aboutbox), hwnd, about); Break;case Idm_exit:destroywindow (HWND); Break;default:return DefWindowProc (hWnd, message, WParam, LParam);} Break;case WM_PAINT:HDC = BeginPaint (HWnd, &PS);//TODO: Add any drawing code here ... TextOut (HDC, Showtext, 12); EndPaint (hwnd, &PS); Break;case wm_destroy:postquitmessage (0); Break;default:return DefWindowProc (hwnd, message, WParam, LParam);} return 0;} The message handler for the About box. INT_PTR CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {unreferenced_parameter (LPARAM); switch ( Message) {case Wm_initdialog:return (INT_PTR) true;case wm_command:if (LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL) {EndDialog (hdlg, LoWord (WParam)); return (INT_PTR) TRUE;} break;} Return (INT_PTR) FALSE;}
The function of this code is to click the left mouse button to display the Hello World in the form (50,50).
Below, according to your own understanding, try to explain the following sections of code:
int Apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpcmdline, int nCmdShow)
The first parameter, HINSTANCE, represents the handle to the instance that the program is currently running, which is a numeric value. When the program runs under Windows, it uniquely identifies the running instance (note that only the running instance of the program has an instance handle). An application can run multiple instances, each running an instance, and the system assigns a handle value to the instance and passes the HINSTANCE parameter to the WinMain function.
The second parameter, hPrevInstance, represents the handle to the previous instance of the current instance. By looking at MSDN we can see that in the WIN32 environment, this parameter is always null, that is, in the WIN32 environment, this parameter no longer works.
The third parameter, lpCmdLine, is a null-terminated string that specifies the command-line arguments to be passed to the application. For example: There is a sunxin.txt file under D, and when we double-click the file with the mouse, the Notepad program (notepad.exe) is started, and the system will D:\ Sunxin.txt is passed as a command-line argument to the Notepad program's WinMain function, and the Notepad program displays the file's contents in a window after it obtains the full pathname of the file.
The fourth parameter ncmdshow specifies how the program's window should be displayed, such as Maximize, minimize, hide, and so on. The value of this parameter is specified by the caller of the program, and the application usually does not need to ignore the value of this parameter.
@ Mayuko
Pacman developing-win32 's "HelloWorld" &winmain () function