Code:
#include<windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ; //Child window procedure//
#define ID_BUTTON1 1
int CreateChildWindow(HINSTANCE, HWND); //Funtion to create child window//
//global variables
static MSG msg1;
HWND b1,childHwnd;
static TCHAR chappname[]= TEXT("Child");
WNDCLASS child_window;
static HINSTANCE ChildInstance;
HWND hwnd;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Modal_Window") ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = szAppName ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd=CreateWindow (szAppName, TEXT ("About Modal Window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstance ;
switch (message)
{
case WM_CREATE : b1=CreateWindow(TEXT("Button"),TEXT("Button1"),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,10,20,50,30,hwnd,(HMENU) ID_BUTTON1,hInstance,0);
return 0 ;
case WM_COMMAND :
{
switch(LOWORD(wParam))
{
case ID_BUTTON1: CreateChildWindow(ChildInstance,hwnd);
break;
default : break;
}
return 0 ;
}
case WM_DESTROY : PostQuitMessage (0) ;
return 0;
} //switch close
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
int CreateChildWindow(HINSTANCE ChildInstance, HWND hwnd)
{
//int error,i;
child_window.style = CS_HREDRAW | CS_VREDRAW ;
child_window.lpfnWndProc = ChildWndProc ;
child_window.cbClsExtra = 0 ;
child_window.cbWndExtra = 0 ;
child_window.hInstance = ChildInstance ;
child_window.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
child_window.hCursor = LoadCursor (NULL, IDC_ARROW) ;
child_window.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
child_window.lpszMenuName = NULL;
child_window.lpszClassName = chappname;
if (!RegisterClass (&child_window))
{
MessageBox (hwnd, TEXT ("This program requires Windows NT!"), chappname, MB_ICONERROR) ; //Problem here!//
return -1;
}
// CreateChildWindow(ChildInstance,hwnd);
childHwnd = CreateWindow (chappname, TEXT ("child Modal Window"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
600, 600,
hwnd, NULL,ChildInstance , NULL) ;
//error = GetLastError ();
EnableWindow(hwnd,FALSE); //disable main window
return 0;
}
LRESULT CALLBACK ChildWndProc(HWND childHwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE: break;
case WM_PAINT: break;
case WM_DESTROY :PostQuitMessage (0) ;
EnableWindow(hwnd,TRUE);
return 0;
default: break;
}
return DefWindowProc(childHwnd, message, wParam, lParam) ;
}
I tried again and what I found is that my child window is constrained inside the main window.. so when my main window is disabled the child becomes disabled. I can't find how to remove this so that my child window can move outside my main window as well.