Framework4.5,vs A detailed explanation of the C + + Win32 application that is automatically generated by the system __c++

Source: Internet
Author: User
Tags win32

Under VS2013, Framework4.5 created the Win32 application, which was a little different from the previous one, and was rarely mentioned in many books, and I sorted it out and explained every code,

Hope to be helpful to students who first learn Win32 application.

#include "stdafx.h" #include "FirstWin32.h" #define MAX_LOADSTRING 100//global variable: hinstance hinst; Current instance TCHAR sztitle[max_loadstring];
The title bar text TCHAR is a wchar_t macro that represents a wide character, and can be written in Chinese, using Unicode encoding. TCHAR szwindowclass[max_loadstring];
Main window class name TCHAR szmytest[max_loadstring];
The forward declaration of the function contained in this code module: ATOM myregisterclass (hinstance hinstance);
BOOL InitInstance (hinstance, int);
Lresult CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);


INT_PTR CALLBACK About (HWND, UINT, WPARAM, LPARAM);
#define APIENTRY WINAPI and #define WINAPI __stdcall indicates that this is the system callback function//_in_ the value of the parameter must be given before the call function is passed, and the handle of the application has been identified before the system calls main.
_in_opt_ the value of the parameter must be given and cannot be modified before the call function is passed, and the handle of the previous application cannot be modified to avoid an error.
                     HINSTANCE instance handle, the handle of the current application process, is the operating system assigned to you, there can only be one in the current program, if you open multiple programs there are multiple int apientry _twinmain (_in_ hinstance hinstance,
                     _in_opt_ hinstance hprevinstance,//on an instance handle _in_ LPTSTR lpCmdLine,
_in_ int ncmdshow) {//unreferenced_parameter (unused variable) tells the compiler that this variable is not used and is not reported as a warning. Unreferenced_parameter (hprevinstance);      


  Unreferenced_parameter (lpCmdLine);
TODO: Place the code here.
MSG msg;
Haccel hacceltable;//Shortcut Table//Initialize global string gets string information from the. rc resource file LoadString (hinstance, Ids_app_title, SzTitle, max_loadstring);
LoadString (HInstance, Idc_firstwin32, Szwindowclass, max_loadstring);
LoadString (HInstance, MYTEST, Szmytest, max_loadstring); MyRegisterClass (HINSTANCE),//Registration//Execution Application initialization: if (! InitInstance (HINSTANCE, nCmdShow))//Initialize and display window {return FALSE;}//Get the Accelerator information table configured by the user in the. rc resource file//make int resource, Converts a macro definition of a numeric type to a string, #define IDC_FIRSTWIN32 109 into a string 109 hacceltable = Loadaccelerators (hinstance, Makeintresource (idc_

FIRSTWIN32)); Main message loop: while (GetMessage (&msg, NULL, 0, 0))//keep getting messages, but just get messages that may belong to you, such as clicking in a control or pressing the keyboard {// TranslateAccelerator translates shortcut messages, if the message exists in the accelerator table, converts the type of message to WM_COMMAND or Wm_syscommand//If the shortcut key is not present in the accelerator table, it is treated as a non-shortcut key message , this only requires the processing of the shortcut key message if (!
TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {translatemessage (&msg);//if keyboard messages are converted to character information DispatchMessage (&msg)//Send the message to the system, the system will automatically call LPFNWNDPROC function} return (int) Msg.wparam;
////function: MyRegisterClass ()////Purpose: Register window class.


The difference between the ATOM MyRegisterClass (hinstance hinstance) {wndclassex wcex;//and Wndclass is the addition of cbsize indicating their size and hiconsm small icons.


wcex.cbsize = sizeof (wndclassex); Wcex.style = Cs_hredraw |
cs_vredraw;//wcex.lpfnwndproc= WndProc;
Wcex.cbclsextra= 0;//added wcex.cbwndextra= 0;
Wcex.hinstance= hinstance; The second parameter is the path to the icon, and a picture with an ID of Idi_firstwin32 is added to the resource file of the. RC, and there will be an automatic macro definition in Resource.h Wcex.hicon = LoadIcon (HINSTANCE,
Makeintresource (idi_firstwin32); the icon in the taskbar wcex.hcursor = LoadCursor (NULL, Idc_arrow) when minimized;//mouse cursor shape
Wcex.hbrbackground= (Hbrush) (color_window+1);
Wcex.lpszmenuname= Makeintresource (IDC_FIRSTWIN32); Wcex.lpszclassname= szwindowclass;//Registration must ensure that the window class name lpszClassName has not been registered, otherwise register a new window failure WCEX.HICONSM = LoadIcon (
Wcex.hinstance, Makeintresource (Idi_small));/Add return RegisterClassEx (&WCEX);


   BOOL InitInstance (hinstance hinstance, int ncmdshow) {HWND hwnd; Hinst = hinstance; Store an instance handle in a global variable//szwindowclass this character arrayis the window name of the Register window Wcex.lpszclassname=szwindowclass; HWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault, 0, cw_usedefault, 0, NULL, NULL, HIN


   Stance, NULL);
   if (!hwnd) {return FALSE;
   } ShowWindow (HWnd, ncmdshow);


   UpdateWindow (HWND);
return TRUE; This method is run when the message is DispatchMessage processed LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {int
Wmid, Wmevent;
Paintstruct PS;


HDC HDC; Switch (message) {Case wm_command:wmid = LoWord (wParam); wmevent = HiWord (WParam);//Analysis Menu selection: switch (wmid) {case I
Dm_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://when the window's size or content changes, the window needs to be redrawn, and the message is sent.
HDC = BeginPaint (hWnd, &ps);
TODO: Add any drawing code here ...
EndPaint (HWnd, &ps);
Break
Case Wm_destroy:postquitmessage (0);
Break Default:return DefwindowproC (hWnd, message, WParam, lParam);//For unwanted messages, you must use DefWindowProc to the system to process, otherwise, the message has been waiting to be processed, the program card dead do not move} 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 (Me


Ssage) {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;
Return (INT_PTR) FALSE; }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.