Entering the Windows Programming World-----window registration and Creation

Source: Internet
Author: User

1 window register and create 1.1WIN32 window program creation steps

1. Definition of WinMain entry function

2. Definition of WindowProc function

3. Registration window class

RegisterClass, RegisterClassEx

4. Create a window

CreateWindow, CreateWindowEx

HWND CreateWindow (

lpctstrlpclassname,// Pointer to the name of the registered window class

lpctstrlpwindowname,// Pointer to window name

dworddwstyle,// the style of the window

intx,// of the window position X coordinates

inty,// of the window position Y coordinates

intnwidth,// width of window

intnheight,// Height of window

hwndhwndparent,// handle to parent window

hmenuhmenu,// Window menu handle

hinstancehinstance,// application Use handle

lpvoidlpparam// Application Data area handle

);

Parameters:

Lpclassname

a pointer to a string ending with 0, or to a previous registerclass or RegisterClassEx The pointer to the atom created by the bar. This atom must be the low of the secondary parameter, and the high must be 0. If it is a string, it specifies the name of the window class. The class name can be any name registeredwith registerclass or registerclassex ,providedthat Themodule that registers, the class is also the module thatcreates the window. The class name can also be any predefined system class name.

Lpwindowname

A pointer to a string ending in 0 that specifies the window name. If the style of the window specifies a title bar, it will be displayed on the title bar. When you create a control using CreateWindow, this parameter specifies the text of the control. when you create a static control with the Ss_icon style , this parameter specifies the name or identifier of the icon. To specify an identifier, use the "#num" syntax.

Dwstyle

Specifies the style of the window to be created. Can be a combination of window styles, plus the style of the controls.

X

Specifies the initial horizontal position of the window. Foroverlappedor pop-up windows,xThe parameter is the initialxThe coordinates are equivalent to the upper-left corner of the window, on the screen coordinate system. Fora ChildWindow, X is the x-coordinate of the Upper-left corner ofthe window relative tothe upper-left corner of the Parent window ' Sclientarea.if the parameter value is set toCw_usedefault, the system selects the default location for the upper-left corner of the window and ignoresyparameters. Cw_usedefaultonly foroverlappedwindow is valid, if it is specified in a popup window or from a window, thexand theyparameter is set to zero.

Y

Specifies the vertical position of the window's initialization. For overlapping windows, or pop-up windows,yparameter is the upper-left corner of the window that is initially in screen coordinates .ycoordinates. For child windows,yis the upper-left corner of the initial child window associated with the upper-left corner of the parent window client area .ycoordinates. ForListBoxControl,yis related to the upper-left corner of the parent window client areaListBoxThe client area is initially in the upper -left corner of theycoordinates. If you usews_visiblestyle Creates aoverlappedwindow andxparameter is set toCw_usedefault, the system ignoresyparameters.

Nwidth

Specifies the width of the window in device units. Foroverlappedwindow,nwidthparameters can be either the width of the window in screen coordinates or theCw_usedefault.ifnwidthis aCw_usedefault, the system selects a default width and height, which is the default width from the initialxcoordinates to the right edge of the screen, the default height fromycoordinates to the top of the icon area. Cw_usedefaultonly foroverlappedwindow is available if a child window or popup window is setCw_usedefault, younwidthand thenheightis set to0.

Nheight

Specifies the height of the window in device units. For the overlapped window,nheight is the height of the window in screen coordinates. If nwidth is set to Cw_usedefault, the system ignores nheight.

hWndParent

A handle to the parent window or owner window of the created window. To create a child window or a window that is owned by all, provide a valid window handle. This parameter is optional for pop-up windows.

HMenu

menu handle, or specify a window-style child window identifier. For the overlapped window or pop-up window,hMenu identifies the menu to use for the window. Can be NULL If the menu of the class is used. For a child window,hMenu Specifies the identifier of the window from which an integer is used to notify the parent window of the event of the dialog box control. The program determines the identity of the child window, which must be unique for all child windows of the same parent window.

HInstance

windows95/98/me: The instance handle of the module that is related to this window.

WINDOWSNT/2000/XP: this value is ignored.

Lpparam

[In]pointer to a value to being passed to the window through Thecreatestruct structurepassed in the Lpparam parameter the Wm_create message. If a program creates a client window with a Multi-document interface by calling CreateWindow. Lpparam must point to a clientcreatestruct structure.


5. Display and refresh of Windows

ShowWindow, UpdateWindow

6. Message Processing

GetMessage, DispatchMessage

7. Window exit

Wm_destroy,

1.2 Registration of Windows 1.2.1 Categories of window classes

1. system global window class, such as button, text edit box (edited), etc.

2. The application's global window class. A window class that can be used in all modules of an EXE, DLL, and so on in an application.

3, the local window class. A window class that can be used only in this module.

1.2.2 Implementation of window class registration

1, the System Global window class, does not need to register the direct use

Using the CreateWindow function, specify the window type name that the system has already defined in classname.

Create a button or edit window below

/*file:botton.cpp *auth:sjin *date:20140618 *mail: [email protected] *//* window creation Exercise */#include <windows.h># Include <winnt.h>hinstance g_hinst = NULL; /* Window handler function */lresult CALLBACK WndProc (HWND hwnd,uint nmsg,wparam wparam,lparam iparam) {return DefWindowProc (hwnd,nmsg, Wparam,iparam);} /* Create Button*/hwnd Createbutton () {/*button */#if 0HWND HWND = CreateWindow ("button", "My first BUTTOn test", Ws_ Overlappedwindow,0,0,100,150,null,null,g_hinst,null); #elseHWND hWnd = CreateWindow ("EDIT", "My first button test", Ws_ Overlappedwindow,0,0,100,150,null,null,g_hinst,null); #endifreturn hWnd;}  /* Register */bool Registerwnd (LPSTR pszclassname) {wndclassex hWnd = {'};hwnd.cbsize ' = sizeof (HWND); Hwnd.style = Cs_vredraw | Cs_hredraw;hwnd.lpfnwndproc = Wndproc;hwnd.cbclsextra = 0;hwnd.cbwndextra = 0;hwnd.hcursor = NULL;hWnd.hIcon = NULL;hWnd . Hbrbackground = Hbrush (color_btnface + 1); hwnd.lpszclassname = Pszclassname;hwnd.hinstance = G_hInst; ATOM Natom = RegisterClassEx (&hwnd); if (0 = = Natom) {REturn FALSE;} return TRUE;} /* Display window */void Displaybutton (HWND hwnd) {ShowWindow (hwnd,sw_show); UpdateWindow (HWND);} The void Message () {MSG msg = {0};while (GetMessage (&msg,null,0,0)) {/* text box can be entered */translatemessage (&msg);D Ispatchmessage (&msg);}} /* Entry function */int WINAPI WinMain (hinstance hinst,hinstance hprevinst,lpstr pszcmcline,int nshowcmd) {g_hinst = HInst; HWND hwnd = Createbutton (); Registerwnd ("");D Isplaybutton (HWND); Message (); return 0;}


2, the Application Global window class, need to implement the registration code, in the registration need to increase the cs_clobalclass (can be used in each module) the implementation of the definition:

Wndclass WC = {'} ';

Wc.style = Cs_clobalclass |cs_hrearaw;

RegisterClass (&WC);

The following example is basically the same

3, local window class, not add Cs_clobalclass definition

Use RegisterClass, registerclassex to register

/*file:winreg.cpp *auth:sjin *date:20140619 *mail: [email protected] *//* window creation Exercise */#include <windows.h># Include <winnt.h>hinstance g_hinst = NULL; /* Window handler function */lresult CALLBACK WndProc (HWND hwnd,uint nmsg,wparam wparam,lparam iparam) {return DefWindowProc (hwnd,nmsg, Wparam,iparam);} /* Create Window*/hwnd CreateWindow (LPSTR pszclassname) {hwnd hwnd = CreateWindow (Pszclassname, "My first Wondow test", Ws_ Overlappedwindow,0,0,100,150,null,null,g_hinst,null); return hWnd;}  /* Register */bool Registerwnd (LPSTR pszclassname) {wndclassex hWnd = {'};hwnd.cbsize ' = sizeof (HWND); Hwnd.style = Cs_vredraw | Cs_hredraw;hwnd.lpfnwndproc = Wndproc;hwnd.cbclsextra = 0;hwnd.cbwndextra = 0;hwnd.hcursor = NULL;hWnd.hIcon = NULL;hWnd . Hbrbackground = Hbrush (color_btnface + 1); hwnd.lpszclassname = Pszclassname;hwnd.hinstance = G_hInst; ATOM Natom = RegisterClassEx (&hwnd); if (0 = = Natom) {return FALSE;} return TRUE;} /* Display window */void Displaywnd (HWND hwnd) {ShowWindow (hwnd,sw_show); UpdateWindow (HWND);} void MessaThe GE () {MSG msg = {0};while (GetMessage (&msg,null,0,0)) {/* * text box can be entered */translatemessage (&msg);D ispatchmessage ( &msg);}} /* Entry function */int WINAPI WinMain (hinstance hinst,hinstance hprevinst,lpstr pszcmcline,int nshowcmd) {g_hinst = HInst; Registerwnd ("Mywin"); HWND hwnd = CreateWindow ("Mywin");D Isplaywnd (HWND); Message (); return 0;}


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.