Win32 programming (3) window registration and Creation

Source: Internet
Author: User

One-window registration and Creation

1. Create a Win32 window Program

1.1 Definition of the winmain entry function
1.2 windowproc Function Definition
1.3 registration window class
Registerclass/registerclassex
1.4 create dialog box
Createwindow/createdomainwex
1.5 display and refresh the window
Showwindow/updatewindow
1.6 Message Processing
Getmessage/dispatchmessage
1.7 exit from the window
Wm_destroy/postquitmessage

2 window Registration

2.1 Classification of window classes
2.1.1 global window class, such as button ("button "),
Text editing box ("edit.
2.1.2 Global window class of the application. It can be used in an application
Window classes used in all modules, such as EXE and DLL, in the program.
2.1.3 local window class. Which can only be used in this module
Window class.

2.2 registration of window classes
2.2.1 global window class for use without registration.
Use the createwindow function and specify
The window type name has been defined by the system.

2.2.2 global window class of the application, which must be implemented by code
Register. You need to add the cs_globalclass definition during registration.
Implementation Method:
Wndclass WC = {0 };
WC. Style = cs_globalclass | cs_hredraw ..;
....
Registerclass (& WC );

2.2.3 local window class, not added cs_globalclass definition.
Register with registerclass and registerclassex
Typedef struct _ wndclassex {
Uint cbsize; // the size of the struct.
Uint style;
Wndproc lpfnwndproc;
Int cbclsextra;
Int cbwndextra;
Handle hinstance;
Hicon;
Hcursor;
Hbrush hbrbackground;
Lpctstr lpszmenuname;
Lpctstr lpszclassname;
Hicon hiconsm; // small icon
} Wndclassex;
2.2.4 window style
Cs_hredraw window horizontal change, re-draw the window.
Cs_vredraw changes vertically and redraws the window.
The cs_dbclick window can receive double-click messages.
Cs_globalclass creates the global window class of the application.
Cs_bytealignwindow alignwindow
Cs_bytealignclient client window alignclient, which is aligned in multiples of 8
Cs_classdc all windows of this type use the same DC (device description table, drawing use)
Cs_owndc each window has its own DC
Cs_parentdc uses the DC of the parent window
Cs_savebits is used to save the window interface with bitmap, which can be improved
Window Interface refresh Performance
Cs_noclose disable the command.

2.2.5 additional data of the window class cbclsextra
Add your own information to the data information of the window class.
The memory size of cbclsextra used to add information
Setclasslong saves information to the memory
Getclasslong extracts information from memory
DWORD setclasslong (
Hwnd, // window handle
Int nindex, // Index Number of the value
Long dwnewlong // Value
);
DWORD getclasslong (
Hwnd, // window handle
Int nindex, // Index Number of the value
);
The length of cbclsextra is generally a multiple of 4 bytes.
2.2.6 add data in the window cbwndextra
Add your own information to the data information in the window.
Cbwndextra memory size used to add information
Setwindowlong saves information to the memory
Getwindowlong extracts information from memory

2.3 window functions
Registerclass/registerclassex Registration
Unregisterclass logout
Getclassinfo/getclassinfoex
Getclassname: get the window class name of the window
Set and obtain additional data of the getclasslong/setclasslong window class
Set and obtain additional data in the getwindowlong/setwindowlong window

2.4 how to locate the window class when creating a window

2.4.1 find the window class of the application. If you find,
Run 2.4.2. Otherwise, run 2.4.3.
2.4.2 if a window class with the same name is found
Hisntance handle. If they are equal, use
Window class information creation window, if different, continue
Search and execute 2.4.3.
2.4.3 If a window class with the same name is not found
Search in the global window class of the application. If you find,
Run 2.4.4. Otherwise, run 2.4.5.
2.4.4 if the global window class is the same, use
.
2.4.5 search in the global window class of the system. If found, create
Window. Otherwise, an error is returned.
 
3. Window Creation

3.1 create a function in the window
Createwindow/createdomainwex
Hwnd createmediawex (
DWORD dwexstyle, // extended window style (new)
Maid, // pointer to registered Class Name
Lptstr lpwindowname, // pointer to window name
DWORD dwstyle, // window style
Int X, // horizontal position of window
Int y, // vertical position of window
Int nwidth, // window width
Int nheight, // window height
Hwnd hwndparent, // handle to parent or owner window
Hmenu, // handle to menu, or child-window identifier
Hinstance, // handle to application instance
Lpvoid lpparam); // pointer to window-creation data
3.2 window style and extended Style
 
Window style: The style defined by ws_xxxx.
Basic style.
Extended style: The style defined by ws_ex_xxxxx, which is a window
Extended style, such as toolwindow.
You can use the basic window style in createwindow to expand
You must use createmediawex to set the window style.

Ws_overlapped window, stacked window
Ws_popup window, pop-up window
Ws_child window, subwindow

3.3 Parent and Child windows
3.3.1 When createwindow is used, specify the parent window
3.3.2 add ws_child to the window style
3.3.3 setparent and getparent can be used
Function to set and obtain the parent window of a specified window.

Others: movewindow moves the window.

3.4 create an MDI window
3.4.1 create the Main Window
Register the main window type and create the main window.
Hwnd = createdomainwex (0,
Pszclassname, "mainwnd ",
Ws_overlappedwindow, cw_usedefault,
Cw_usedefault, cw_usedefault,
Cw_usedefault, null, null, g_hinst,
Null );
3.4.2 mdiclient window
1. Add the clientcreatestruct structure as the additional data.
Clientcreatestruct cs = {0 };
CS. idfirstchild = 1000; // ID of the first child window of MDI
2. The "mdiclient" window type is used during creation.
3. Its parent window is the main window.
Hwnd = createdomainwex (0,
"Mdiclient", "mainwnd ",
Ws_child | ws_visible, cw_usedefault,
Cw_usedefault, cw_usedefault,
Cw_usedefault, hparent, null, g_hinst,
& CS );
3.4.3 create an MDI subwindow
1. register the main window type and create a subwindow.
2. Its parent window is the mdiclient window.
3. added the ws_ex_mdichild style.
Hwnd =
Createmediawex (ws_ex_mdichild,
Pszclassname, "childwnd ",
Ws_child | ws_visible, cw_usedefault,
Cw_usedefault, cw_usedefault,
Cw_usedefault, hparent, null,
G_hinst, null );

// Winmdi. CPP: defines the entry point for the application. // # include "stdafx. H "hinstance g_hinst = NULL; hwnd g_hmdiclient = NULL; // The lresult callback mainproc (hwnd, uint nmsg, wparam, lparam) {Switch (nmsg) {Case wm_destroy: postquitmessage (0); Return 0;} return defframeproc (hwnd, g_hmdiclient, nmsg, wparam, lparam );} // The lresult callback childproc (hwnd, uint nmsg, wparam, lparam) {return defmdichildproc (hwnd, nmsg, wparam, lparam );} // window registration function bool registerwnd (lpstr pszclassname, wndproc proc, int nbrush) {wndclassex wce = {0}; wce. cbsize = sizeof (wce); wce. style = cs_hredraw | cs_vredraw; wce. cbclsextra = 0; wce. cbwndextra = 0; wce. lpfnwndproc = proc; wce. hinstance = g_hinst; wce. hcursor = NULL; wce. hicon = NULL; wce. hbrbackground = hbrush (nbrush); wce. lpszclassname = pszclassname; wce. lpszmenuname = NULL; wce. hiconsm = NULL; atom natom = registerclassex (& wce); If (natom = 0) {return false;} return true;} // display window void displaywnd (hwnd) {showwindow (hwnd, sw_show); updatewindow (hwnd) ;}// message loop void message () {MSG = {0}; while (getmessage (& MSG, null, 0, 0) {dispatchmessage (& MSG) ;}// create the hwnd createmainwnd (lpstr pszclassname) {hwnd = createdomainwex (0, pszclassname, "mainwnd", ws_overlappedwindow, cw_usedefault, null, null, g_hinst, null); Return hwnd ;}// create mdiclient window hwnd createmdiclient (hwnd hparent) {clientcreatestruct cs = {0 }; CS. idfirstchild = 1000; hwnd = create1_wex (0, "weight", "mainwnd", ws_child | ws_visible, cw_usedefault, hparent, null, g_hinst, & CS); Return hwnd;} // create an MDI sub-window hwnd createchildwnd (lpstr pszclassname, hwnd hparent) {hwnd = create1_wex (ws_ex_mdichild, pszclassname, "childwnd ", ws_child | struct, cw_usedefault, hparent, null, g_hinst, null); Return hwnd;} int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {// register the main registerwnd ("mainwnd", mainproc, color_btnface + 1); // register the subwindow registerwnd ("childwnd", childproc, color_window ); // create the MDI Main Window hwnd hmain = createmainwnd ("mainwnd"); // create the mdiclient window g_hmdiclient = createmdiclient (hmain); movewindow (g_hmdiclient, 0, 0,500,500, true ); // create the MDI subwindow createchildwnd ("childwnd", g_hmdiclient ); // display and Message Processing displaywnd (hmain); 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.