Code:
#include <windows.h>
LRESULT CALLBACK Owner_WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
static HWND hwndModal;
switch(msg)
{
case WM_CREATE:
return 0;
case WM_LBUTTONDOWN:
hwndModal = CreateWindow( "DialogWnd",
"Modal Window",
WS_POPUPWINDOW|WS_CAPTION|WS_VISIBLE,
(GetSystemMetrics( SM_CXSCREEN )-200)/2,
(GetSystemMetrics( SM_CYSCREEN )-100)/2,
200, 100,
hwnd, 0, GetModuleHandle(0), 0 );
return 0;
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
LRESULT CALLBACK Modal_WndProc( HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
static HWND hwndOwner;
switch( msg )
{
case WM_CREATE:
hwndOwner = GetWindow(hwnd,GW_OWNER);
EnableWindow( hwndOwner, FALSE );
return 0;
case WM_DESTROY:
EnableWindow( hwndOwner, TRUE );
SetFocus( hwndOwner );
return 0;
default:
return DefWindowProc( hwnd, msg, wParam, lParam );
}
}
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrev, LPSTR args, int nShow )
{
WNDCLASS wc = {0};
// Register the owner window class.
wc.lpszClassName = TEXT( "OwnerWnd" );
wc.hInstance = hInst ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = Owner_WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
// Register the modal dialog window's class.
wc.lpszClassName = TEXT( "DialogWnd" );
wc.lpfnWndProc = Modal_WndProc ;
RegisterClass(&wc);
// Create the owner window.
CreateWindow("OwnerWnd",TEXT("Owner Window"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
(GetSystemMetrics( SM_CXSCREEN )-400)/2,
(GetSystemMetrics( SM_CYSCREEN )-300)/2,
400,300, 0,0, hInst, 0);
MSG msg ;
while( GetMessage(&msg,0,0,0) > 0 ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}