005 Windows Messaging Mechanism

Source: Internet
Author: User

#windows消息机制

Create a new first WIN32 application

VS2015 New Project Win32 application

//Win32WindowDemo.cpp: Defines the entry point for the application. //#include"stdafx.h"#include"Win32WindowDemo.h"#defineMax_loadstring 100//Global Variables:HINSTANCE HInst;//Current InstanceWCHAR sztitle[max_loadstring];//title bar TextWCHAR szwindowclass[max_loadstring];//main window class name//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);
Program Entry point

#define CALLBACK __stdcall vs default is __stdcall
#define WINAPI __stdcall vs default is __stdcall
#define WINAPIV __cdecl Other versions with the compiler is __cdecl
#define Apientry WINAPI

Stack-"Pass parameters

Stack frame-"Flat stack"

__stdcall is a predetermined function call, and the function calling convention mainly constrains two things.

1. Parameter Pass Order

2. The call stack is cleaned by WHO (calling function or called function)

Common function Calling Conventions: stdcall cdecl fastcall thiscall naked Call

intapientry wWinMain (_in_ hinstance hinstance, //int type wWinMain twinmain WinMain different encoding modes ASCII Unciode  _in_opt_ hinstance hprevinstance, _in_ lpwstr lpcmdline, _in_ intnCmdShow)
            
            //hinstance hinstance Kernel Object Instance object the operating system tells us
            HINSTANCE hprevinstance Start my this process who the operating system tells us
lpCmdLine receive parameters run as the user tells us
nCmdShow start the way user high speed our {unreferenced_parameter (hprevinstance); Unreferenced_parameter (lpCmdLine);

Two macro definitions
//TODO: Place the code here. //Initializing global StringsLoadstringw (HInstance, Ids_app_title, SzTitle, max_loadstring); Loadstringw (HInstance, Idc_win32windowdemo, Szwindowclass, max_loadstring);

// Loadstringw to load a string
//ids the string defined in the Resource table one is the title of the program a window class name
//A way to diversify internationally
MyRegisterClass (HINSTANCE); //Register window class to system //To perform an application initialization:    if(!InitInstance (HINSTANCE, nCmdShow)) {        returnFALSE; } haccel hacceltable=loadaccelerators (hinstance, Makeintresource (Idc_win32windowdemo));    MSG msg; //main message loop:
Get message
//&msg Structural Body
typedef struct tagmsg{
HWND HWND Window Instance
UINT message; Window Messages
WPARAM WPARAM Value Added
LPARAM LPARAM; Added value
DWORD time; Message Time
Point PS; Mouse position
}
while(GetMessage (&msg, nullptr,0,0)) { if(! TranslateAccelerator (Msg.hwnd, hacceltable, &msg)) {TranslateMessage (&msg); DispatchMessage (&msg); } } return(int) Msg.wparam;}////function: MyRegisterClass ()////Purpose: Registers the window class. //ATOM MyRegisterClass (hinstance hinstance) {WNDCLASSEXW Wcex; Wcex.cbsize=sizeof(Wndclassex); Wcex.style= Cs_hredraw |Cs_vredraw; //Window style Wcex.lpfnwndproc=WndProc; //Window class Wcex.cbclsextra=0; //Reserved space for added value, this program is useless to this domain! Wcex.cbwndextra=0; //Reserved space for added value, this program is useless to this domain! wcex.hinstance=hinstance; //instance handle, one of the parameters of the main function! Wcex.hicon=LoadIcon (hinstance, Makeintresource (Idi_win32windowdemo)); //Load icon function, in which you can load your own icons! Wcex.hcursor=loadcursor (nullptr, Idc_arrow); //Load cursor function, in which you can load your own cursor! Wcex.hbrbackground= (Hbrush) (color_window+1); //Initialize the window's paint brush wcex.lpszmenuname=Makeintresourcew (Idc_win32windowdemo); //Window menu wcex.lpszclassname=Szwindowclass; //Program name! WCEX.HICONSM=LoadIcon (Wcex.hinstance, Makeintresource (Idi_small)); returnREGISTERCLASSEXW (&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 the main program window.  //BOOL InitInstance (hinstance hinstance, int  ncmdshow) {hInst = hinstance; // storing an instance handle in a global variable hwnd hwnd = Createwindoww (szwindowclass, //The name of the application in the operating system registry
SzTitle,
//Application title bar name
Ws_overlappedwindow,
//Window styleCw_usedefault,//window display upper left corner x coordinate
0,
the y-coordinate of the upper-left corner when the window is displayed
Cw_usedefault,
//window display, lower right corner x coordinate
0 ,
//The lower-right corner of the window display y-coordinate
nullptr,
//Parent window handle, this program has no
nullptr,
//Menu handle
HINSTANCE,
//handle Program Instance handle if (! HWND) { return FALSE;          } ShowWindow (HWnd, ncmdshow); //Display window UpdateWindow (hWnd); return TRUE;}

////functions: WndProc (HWND, UINT, WPARAM, LPARAM)////Purpose: Processes the message of the main window. ////wm_command-Processing Application Menu//WM_PAINT-Draw the main window//Wm_destroy-Sends an exit message and returns////
#消息是已整数形式传送的LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
HWND instance
Message Queue
Value
//Value Added
{    Switch(message) { Casewm_command: {intWmid =LoWord (WParam); //Analysis Menu Selection:            Switch(wmid) { CaseIdm_about:dialogbox (HInst, Makeintresource (Idd_aboutbox), hWnd, about);  Break;  CaseIdm_exit:destroywindow (HWND);  Break; default:                returnDefWindowProc (hWnd, message, WParam, LParam); }        }         Break;  Casewm_paint: {paintstruct ps; HDC HDC= BeginPaint (HWnd, &PS); //TODO: Add any drawing code here that uses HDC ...EndPaint (HWnd, &PS); }         Break;  CaseWm_destroy:postquitmessage (0);  Break; default:        returnDefWindowProc (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) { CaseWm_initdialog:return(INT_PTR) TRUE;  CaseWm_command:if(LoWord (wParam) = = IDOK | | LoWord (wParam) = =IDCANCEL)            {EndDialog (hdlg, LoWord (WParam)); return(INT_PTR) TRUE; }         Break; }    return(INT_PTR) FALSE;}

#MFC Win32

Because the Win32 development program is cumbersome and complex

Messaging system

MFC is actually wrapping the message and method into a class

The functions in MFC are not the same as the parameters of the Win32 API function.

MSDN MFC is the Chinese Win32 API is in English

    

005 Windows Messaging Mechanism

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.