Creating a child window

Creating a child window
Steps for creating the child window:1. Register a new window class for the child window with
the RegisterClass( ) function.
2. Create the child window using the createWindow()
function with the WS_CHILD window style.
3. Write a separate window message processing function
for the child window, similar to WndProc() for the main
program window.
4. Declare the child window’s message processing function
in the Exports section of the program’s module
defination file.
The child window class is created with the help of
following programs lines:WNDCLASS childclass;
childclass.lpfnWndProc= ChildProc;
Childclass.hIcon=NULL;
Childclass.hCursor=LoadCursor(NULL,IDC_CROSS);
Childclass.hbrBackground=GetStockObject(LTGRAY_BRUSH);
Childclass.lpszMenuName=NULL;
Wndclass.lpszClassName=“newchildclass”;
/* register the child window class*/
if(!RegisterClass(&childclass)){
MessageBox(hWnd,”could not register child class”,
“Message”,MB_OK);
return (0);
}
• The message processing function for this
window class is set to “childProc”. This is the
name if a seprate function that will process
message for the child window.
• The ChildProc() function has the same format
as WndProc(), but only processes messages
sent to child window created from this new
window class.
• When a child window’s class is registered any
number of child window can be created based
on this class.
For creating the child window
hChild=CreateWindow(“newchildclass”, childWindow”,
WS_CHILD|WS_BORDER|WS_CAPTION,10,30,200,150,
hWnd, NULL,hInstance,NULL);
ShowWindow(hChild,SW_SHOWNORMAL);
Code for creating the child window
#include<windows.h>
int _stdcall WinMain(HINSTANCE h, HINSTANCE hprv, char *n,
int i)
{ HWND hwnd;
// window’s handle
MSG msg;
// message structure data
WNDCLASS wndclass;
if(!hprev)
{ wndclass.Style =CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndproc=WndProc;
wndclass.cbWndExtra=0;
wndclass.cbClsExtra=0;
wndclass.hInstance=h;
wndclass.hIcon=LoadIcon(hInstance, “myicon”);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclasss.hbrBackground=GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=“parentMenu”;
wndclass.lpszClassName=“myclass”;
/* register the parent window class */
If(!RegisterClass(&wndclass))
return(0);
/* change the class definition for child */
wndclass.lpfnWndproc=ChildProc;
wndclass.hIcon=NULL;
wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
wndclasss.hbrBackground=GetStockObject(LTGRAY_BRUSH);
wndclass.lpszClassName=“chilclass”;
/* register the child window class */
If(!RegisterClass(&wndclass))
{ MessageBox(hWnd, “could not register child class”,
“message”,MB_OK)
return(0);
} }
hWnd=CreateWindow(“myclass”,
“child1”,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, NULL,NULL,h,NULL);
ShowWindow(hWnd,nCmdShow); /* display the Window */
While(GetMessage(&msg,NULL,NULL,NULL))
{ TranslateMessage(&msg);
DispatchMessage(&msg); }
return(msg.wParam);
}
Long
WndProc(HWND
hwnd,
WORD
wMessage,
WORD
wParam,LONG lparam)
{ static HWND;
hChild=NULL;
HINSTANCE h;
Switch(wMessage)
{ case WM_COMMAND:
swtich(wParam)
{ case IDM_CREATE:
if(hChild==NULL) // only create one child
h=GetWindowWord(hWnd,GWW_HINSTANCE);
/* create the child window and display it */
hChild=CreateWindow(“childclasss”,
“childwindow”,WS_CHILD|WS_BORDER|WS_CAPTION,10,
30,200,150,hWnd,h,NULL);
ShowWindow(hChild,1);
}
break;
case IDM_QUIT:
DestroyWindow(hWnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default: /*default Window message Processing */
return
DefWindowProc(hWnd, wMessage, wParam,lParam);
return (0);
Long ChildProc(HWND hChild, WORD wMessage, WORD
wParam, LONG lparam)
{
PAINTSTRUCT PS;
Switch (wMessage)
{ case: WM_PAINT:
BeginPaint(hChild,&ps);
TextOut(ps.hdc,10,25, “text in child window”,22);
EndPaint(hChild, &ps);
break;
default:
return
DefWindowProc(hChild,wMessage, wParam,lParam);
}
return(0);
}
Button control class
•
•
•
•
•
Button Messages
Button Styles
Push Buttons
Check Boxes
Radio Buttons
Push Buttons
• This is the most basic type of button, which
just provides a trigger-like input to the parent
window.
• The unique feature of this button is that it can
not be turn on or off.
• It can be just pressed to trigger a certain
action, after which it automatically returns to
its previous state.
• If they have input focus for the keyboard, they
can also be pressed by pressing and releasing
the spacebar.
• These buttons send a WM_COMMAND
message to their parent as soon as the mouse
button or spacebar is released.
• They also accept BM_GETCHECK and
BM_SETSTATE message.
• The style member of the push button is :BS_PUSHBUTTON and BS_DEFPUSHBUTTON
Check boxes
• Check boxes are designed to be turned on and
off, and they make it a point to remember their
on/off state at all times.
• A check box button starts off looking like a tiny
empty located at the button’s client area.
• The style member of the check box is
BS_AUTOCHECKBOX and BS_CHECKBOX
• The check box messages is
BM_GETCHECK , BM_SETSTATE, and
WM_COMMAND
Radio buttons
• Radio buttons too have an on/off state, but
the difference here is that they can’t be
turned off by clicking again.
• The style member of the radio button is that:BS_AUTORADIOBUTTON and
BS_RADIOBUTTON
• The messages is BM_SETCHECK and
WM_COMMAND.