Pacman developing-win32 's "HelloWorld" &myregisterclass () function

Source: Internet
Author: User

#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 paint hereFigure Code ... 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;}


This is a simple Hello World program.

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);}

MyRegisterClass is a very simple function that sets the value of the main window class required by the program.

InitInstance passes two parameters to MyRegisterClass to set the window class, HINSTANCE is the instance passed to InitInstance by WinMain, HInstance stores the current instance of the running program, InitInstance will copy its value into a global variable. The second parameter is a value created in InitInstance with the char * type, and is initialized to the window class name (in this case, "Hello World").

The

Yregisterclass defines a new variable WC, which is the WNDCLASS type. Each member of this structure is defined sequentially in MyRegisterClass, so it is not necessary to list this structure. The

window style wc.style is set to Cs_hredraw | Cs_vredraw. The pipe symbol (|) Represents a method of bit merging. The Cs_hredraw value allows the program window to be completely redrawn when moving or resizing changes the width. Similarly, the Cs_vredraw makes the window highly adjustable and completely redrawn. The

Wc.lpfnwinproc is not a simple variable, but rather a long pointer to a callback function. This is very important, and if you do not set this value, the message cannot be passed to the program window (HWND). When the Windows message matches the value of this hwnd, the callback window procedure is automatically called. This is true for all messages, including user input and window repainting. Any button press, screen refresh, or other event occurrence will undergo this callback process. This function can be any name, such as Bigbadgamewindowproc, as long as its return value is LRESULT callback and the parameters are correct. The

structure variables Wc.cbclsextra and Wc.cbwndextra should be set to zero most of the time. These variables are only used to add extra memory space to the window process, and we really don't need to use them. The

Wc.hinstance is set to the HINSTANCE parameter value passed to MyRegisterClass. The

Wc.hicon and Wc.hcursor,loadicon functions are typically used to mount an icon image from a resource, and the Makeintresource macro returns a string value for the resource identifier. The

Wc.hbrbackground is set to a brush handle that is used to draw the background of the program window. The default is to use the Backup object White_brush. It can be a bitmap image, a custom brush, or any color. The

Wc.lpszmenuname is set to the name of the program menu, which is also a resource.

The Wc.lpszclassname is set to the Szwindowclass parameter value passed to MyRegisterClass. It assigns a specific class name to the window, and is used together with the HWND in message processing.

MyRegisterClass calls the Regsiterclassex function. The WNDCLASS variable that sets the window details is passed to this function by the WC. If the return value is zero, the value is passed back to InitInstance if the window is successfully registered with Windows.


@ Mayuko

Pacman developing-win32 's "HelloWorld" &myregisterclass () function

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.