To create the main window:
Create a main window This code is often used, save it first.
#include"stdafx.h"hinstance g_hinstance=0;//receive application instance handle//main Window processing functionLRESULT CALLBACK WndProc (HWND hwnd,uint nmsg,wparam wparam,lparam LPARAM) {Switch(nmsg) { CaseWm_destroy:postquitmessage (0); Break; } returnDefWindowProc (Hwnd,nmsg,wparam,lparam);}//Register window classBOOL Register (LPSTR lpclassname,wndproc WNDPROC) {wndclassex WCE={0}; Wce.cbsize=sizeof(WCE); Wce.cbclsextra=0; Wce.cbwndextra=0; Wce.hbrbackground= (Hbrush) (color_window+1); Wce.hcursor=NULL; Wce.hicon=NULL; WCE.HICONSM=NULL; Wce.hinstance=g_hinstance; Wce.lpfnwndproc=WndProc; Wce.lpszclassname=Lpclassname; Wce.lpszmenuname=NULL; Wce.style= Cs_hredraw |Cs_vredraw; ATOM Natom= RegisterClassEx (&WCE); if(natom==0) { returnFALSE; } returnTRUE;}//Creating the main windowhwnd Createmain (LPSTR lpclassname,lpstr lpwndname) {HWND hwnd= CreateWindowEx (0, Lpclassname,lpwndname,ws_overlappedwindow,cw_usedefault,cw_usedefault,cw_usedefault,cw_usedefault,null, Null,g_hinstance,null); returnhWnd;}//Display windowvoidDisplay (HWND hwnd) {ShowWindow (hwnd,sw_show); UpdateWindow (hWnd);}//message LoopsvoidMessage () {MSG nmsg={0}; while(GetMessage (&nmsg,null,0,0) {translatemessage (&nmsg); DispatchMessage (&nmsg); }}intapientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCm Dline,intncmdshow) {g_hinstance=hinstance; //Registration Window if(! Register ("Main", WndProc)) {MessageBox (NULL,"Registration Failed","Error", MB_OK); return 0; } //Create WindowHWND hwnd = Createmain ("Main","Window"); Display (HWND); Message (); return 0;}
Creating a child window
To set the parent window handle when creating
Create style to add ws_child| Ws_visible
Additional data Buffers for window and window classes
Role
When registering a window, you can set the size of both data memory space.
int Cbclsextra; Additional data for the window class buffer size 4 in multiples of general (in bytes)
The additional data buffer size of the INT cbwndextra;//window
can provide the window class and the window to store their own data space
To buffer data:
DWORD Setclasslong (
HWND hwnd, //window handle int nIndex, //Byte index number LONG dwnewlong //new value);
Read buffer
DWORD Getclasslong (
HWND hwnd, int nIndex);//return value receive read data
Additional data buffers for Windows
int Cbwndextra; Multiple (in bytes) of General 4
LONG SetWindowLong (
HWND hwnd, //window handle int nIndex,//Byte index number LONG Dwnewlong//Deposit data );
LONG GetWindowLong (
HWND hwnd,//window handle int nIndex//Byte index number );//return value receive read data
The difference between a window class additional data buffer and a window-attached data buffer
The additional data buffer for the window class is the buffer that is shared by all windows that are created based on the window class-allocating space when the static member definition in the class is defined
Window attach data buffer is the window's own private buffer-equivalent to the ordinary members of the class, when created to allocate space
Enclosed complete code: read-write window class and window-attached functions
#include"stdafx.h"#include"stdio.h"hinstance g_hinstance=0;//receive application instance handle//main Window processing functionLRESULT CALLBACK WndProc (HWND hwnd,uint nmsg,wparam wparam,lparam LPARAM) {Switch(nmsg) { CaseWm_destroy:postquitmessage (0); Break; } returnDefWindowProc (Hwnd,nmsg,wparam,lparam);}//Register window classBOOL Register (LPSTR lpclassname,wndproc WNDPROC) {wndclassex WCE={0}; Wce.cbsize=sizeof(WCE); Wce.cbclsextra= $; Wce.cbwndextra= $; Wce.hbrbackground= (Hbrush) (color_window+1); Wce.hcursor=NULL; Wce.hicon=NULL; WCE.HICONSM=NULL; Wce.hinstance=g_hinstance; Wce.lpfnwndproc=WndProc; Wce.lpszclassname=Lpclassname; Wce.lpszmenuname=NULL; Wce.style= Cs_hredraw |Cs_vredraw; ATOM Natom= RegisterClassEx (&WCE); if(natom==0) { returnFALSE; } returnTRUE;}//Creating the main windowhwnd Createmain (LPSTR lpclassname,lpstr lpwndname) {HWND hwnd= CreateWindowEx (0, Lpclassname,lpwndname,ws_overlappedwindow,cw_usedefault,cw_usedefault,cw_usedefault,cw_usedefault,null, Null,g_hinstance,null); returnhWnd;}//Creating child windowsHWND createchild (LPSTR lpclassname,lpstr lpwndname,hwnd hparent) {returnCreateWindowEx (0, lpclassname,lpwndname,ws_child| ws_visible| Ws_overlappedwindow, -, -, $, $, hparent,null,g_hinstance,null);}//Display windowvoidDisplay (HWND hwnd) {ShowWindow (hwnd,sw_show); UpdateWindow (hWnd);}//message LoopsvoidMessage () {MSG nmsg={0}; while(GetMessage (&nmsg,null,0,0) {translatemessage (&nmsg); DispatchMessage (&nmsg); }}//Read BuffervoidGetextra (HWND hwnd) {DWORD Nclassextra= Getclasslong (HWnd,196);//additional data buffers for Read window classesDWORD Nwndextra = GetWindowLong (HWnd,0);//additional data buffers for read WindowsCHAR sztext[ the] = {0}; sprintf (Sztext,"window class:%d, window:%d", Nclassextra,nwndextra); MessageBox (Null,sztext,"Infor", MB_OK);}//Deposit BuffervoidSetextra (HWND hwnd) {Setclasslong (hwnd,196, -);//additional data buffers for write-window classesSetWindowLong (HWnd,0, -);//additional data buffers for write Windows}intapientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCm Dline,intncmdshow) {g_hinstance=hinstance; //Registration Window if(! Register ("Main", WndProc)) {MessageBox (NULL,"Registration Failed","Error", MB_OK); return 0; } //Create WindowHWND hwnd = Createmain ("Main","Window"); Register (" Child", DefWindowProc); HWND HChild1= Createchild (" Child","C1", hWnd); HWND hChild2= Createchild (" Child","C2", hWnd); Setextra (HCHILD1); Getextra (HCHILD2); Display (HWND); MoveWindow (HChild1,310, -, $, $, TRUE); MoveWindow (HChild2,510, -, $, $, TRUE); Message (); return 0;}
Windows core Programming 05-Creation of window classes