Hello-win program Getting Started Win32 basic window frame full parsing

Source: Internet
Author: User

Create a Win32 program in a vc++6.0 environment

Select the third then you can run to see Hello World.

Program parsing:

#define MAX_LOADSTRING 100 \ \ Defines the maximum word length </span>hinstance hinst;\\ The handle of the current process resource TCHAR sztitle[max_loadstring]; The caption displayed above \ \ window Tchar szwindowclass[max_loadstring];  \ \ window-defined name

In fact, the first line in front of the handle is equivalent to the ID number, each running a process requires the system to assign a code to mark it. TCHAR is a double-byte character type and char is a single byte.


This is the main program of the Win32 program equivalent to main:

int Apientry WinMain (hinstance hinstance,  //the handle of the current instance (flag) Windows is a multitasking operating system, a program can run multiple instances, in order to differentiate it Hinsta                     NCE hPrevInstance,  //handle of previous instance LPSTR lpCmdLine,//long string pointer data type, passed to WinMain command int nCmdShow)//Specifies how the window is displayed, and the application often ignores the value {. MSG msg;  //message data type Haccel hacceltable;  //stores the handle of the Keyboard accelerator key table, the keyboard accelerator key is used by the application shortcut keys such as Word under alt+f find LOADSTRING (hinstance, Ids_app_title, SzTitle, max_loadstring); Load the contents of the Ids_app_title into the string sztitle LOADSTRING (hinstance, Idc_test, Szwindowclass, max_loadstring); Load the contents of the idc_test into the string Szwindowclass MyRegisterClass (HINSTANCE);  //Registration window if (! InitInstance (HINSTANCE, nCmdShow))  //Startup window, failure program exit         {          & Nbsp;return false;  }  hacceltable = Loadaccelerators (hinstance, (LPCTSTR) IDC_TEST); Load the shortcut key table, which can be seen in the Resource VC view while (GetMessage (&msg, NULL, 0, 0))//message loop, if get exit message return 0{          & Nbsp;if (! TranslateAccelerator (msg.hWND, Hacceltable, &msg))//If it is not a shortcut, it is sent directly to the window process    {       translatemessage ( &AMP;MSG);  //translation Messages        dispatchmessage (&msg);  //Send Message to window procedure            }}return Msg.wparam;  //return Exit}

Window Registration Function:

ATOM MyRegisterClass (hinstance hinstance)//atom for unsigned short type {wndclassex Wcex; Defines a window class wcex.cbsize = sizeof (wndclassex); Window Size wcex.style= Cs_hredraw | Cs_vredraw;  Window style, with the | symbol can be selected at the same time, the specific type look at the following figure wcex.lpfnwndproc= (WNDPROC) WNDPROC;  window procedure function, below is defined wcex.cbclsextra= 0;  Additional bytes, typically 0wcex.cbwndextra= 0;  Additional bytes, generally 0wcex.hinstance= hinstance;  Instance handle wcex.hicon= LoadIcon (hinstance, (LPCTSTR) idi_test);  Window icon, can be changed in the Resource View wcex.hcursor= loadcursor (NULL, Idc_arrow);  The style of the cursor in the window wcex.hbrbackground= (hbrush) (color_window+1);  Window Paint Brush Color wcex.lpszmenuname= (LPCSTR) idc_test;  Menu Resource Name, you can change wcex.lpszclassname= Szwindowclass in the resource view;   Window class name wcex.hiconsm= LoadIcon (wcex.hinstance, (LPCTSTR) idi_small);  Window Small icon return registerclassex (&WCEX); Registry

Window style:


Startup window:

BOOL InitInstance (hinstance hinstance, int ncmdshow)  //Startup Window function, the second parameter is the startup type {   HWND hwnd; Defines the window handle (identity)   hInst = hinstance;//Store instance handle in our global variable, translated by setting the handle of the current instance of the record to the instance   hWnd = creat Ewindow (Szwindowclass, SzTitle, Ws_overlappedwindow,//Create window      cw_usedefault, 0, cw_usedefault, 0, NULL, NULL, HINSTANCE, NULL);   if (!hwnd)//Determine if the creation was successful   {      return FALSE;   }   ShowWindow (HWnd, ncmdshow);  Display window   UpdateWindow (hWnd); Refresh window   return TRUE;}


Window procedure:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)  //back down the System API window procedure function, followed by two parameters for message {  &N Bsp                          ,         &NB Sp                          ,         &NB Sp    //additional information int wmid, wmevent; Paintstruct PS;  //saved the window drawing information hdc hdc;  //the handle of the element in the window TCHAR szhello[max_loadstring];  //defines a double-byte string LOADSTRING (HInst, Ids_hello, Szhello, max_loadstring);  //the character in the Resource table to the Szhello variable switch (message)  //Select message {case WM_COMMAND: The message obtained when a command is selected in the  //menu Wmid = LoWord (wParam ); Wmevent = HiWord (WParam); Parse the menu Selections:switch (wmid) {case idm_about:  //About DialogBox (HInst, (LPCTSTR) Idd_aboutbox, HWnd, (DL GPROC);  //jump out of the new graphics window Break;case idm_exit:  //Exit DestroyWindow (HWND);  //Destroy Window Break;default:return DefWindowProc (hWnd, MessagE, WParam, LParam);} Break;case WM_PAINT:  //draw, the message can receive HDC = BeginPaint (hWnd, &ps) after the window is stretched, minimized, and/or todo:add any drawing code here ... RECT RT;  //defines a rectangle variable getclientrect (hWnd, &AMP;RT);  //assigns the window dimensions to the rectangle variable DrawText (hdc, Szhello, strlen (Szhello), &rt, Dt_center);  //print Szhello content on Windows                        //When you need to format in a specific location Output can be used: TextOut (hdc,1,1,buff,wsprintf (Buff,text ("Lalla%i"), AA), and I for int type                        //First to define TCHAR type output buffer array buff                & nbsp      //If forced redraw is required, InvalidateRect (hwnd,null,true);                        EndPaint (hWnd, &ps);  //finishes drawing Break;case wm_destroy:postquitmessage (0); Break;default:return defwindowproc (hWnd, message, WParam,   LParam); } return 0;} </span>
About:

LRESULT CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM)//menu click About Pop-up small window {switch (message)//select message {CA  SE wm_initdialog://Add content to initialize here, default does not add return TRUE; Normal exit Case WM_COMMAND://Button Selected if (LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL)//If the point "OK" or "Cancel" {EndDialog (hdlg, LoWord (WParam));  End return TRUE; Normal exit}break;}    return FALSE; Abnormal exit}</span>




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hello-win program Getting Started Win32 basic window frame full parsing

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.