Dialog Box

//modal and modeless dialog box

////////////////////////////////////////////////////////////////////////////
//Microsoft Visual Studio generated resource script.
/////////////////////////////////////////////////////////////////////////////
/*
IDD_MODELESS DIALOG DISCARDABLE 0, 0, 186, 90
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
CONTROL "Red",IDC_RED,"Button",BS_AUTORADIOBUTTON,17,23,84,10
CONTROL "Yellow",IDC_YELLOW,"Button",BS_AUTORADIOBUTTON,17,41,84,
10
CONTROL "Green",IDC_GREEN,"Button",BS_AUTORADIOBUTTON,17,57,84,
10
GROUPBOX "Static",IDC_STATIC,7,15,126,59
END

IDD_MODAL DIALOG DISCARDABLE 0, 0, 186, 90
STYLE DS_SYSMODAL | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
LTEXT "Static",IDC_STATIC1,26,23,75,16
END
*/
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK ModelessDialogProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK ModalDialogProc(HWND, UINT, WPARAM, LPARAM);
void CreateModelessDialogBox(HWND);
void RegisterModelessDialogClass(HWND);
void CreateModalDialogBox(HWND );
void setbackground(HWND);
HBRUSH hBrush;
COLORREF bk_color=RGB(255, 255, 255);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PWSTR pCmdLine, int nCmdShow)
{
MSG msg;
HWND hwnd;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("myWindowClass");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, TEXT("Dialog box demo"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 700, 200, NULL, NULL, hInstance, NULL);

while( GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
RegisterModelessDialogClass(hwnd);
CreateWindow(TEXT("button"), TEXT("Show modeless"), WS_VISIBLE | WS_CHILD ,20, 50, 130, 35, hwnd, (HMENU) IDD_MODELESS, NULL, NULL);
CreateWindow(TEXT("button"), TEXT("Show modal"), WS_VISIBLE | WS_CHILD ,160, 50, 130, 35, hwnd, (HMENU) IDD_MODAL, NULL, NULL);
hBrush= GetSysColorBrush(COLOR_3DFACE);
break;
case WM_PAINT:
HDC hdc;
RECT rc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
//Erases window background with colour in hBrush
case WM_ERASEBKGND:
{
hdc = (HDC) wParam;
GetClientRect(hwnd, &rc);
FillRect(hdc, &rc, hBrush );
}
return 1l;
case WM_COMMAND:
switch (LOWORD (wParam))
{
//responds to buttons click
case IDD_MODELESS:
CreateModelessDialogBox(hwnd);
break;
case IDD_MODAL:
CreateModalDialogBox(hwnd);
break;
}
break;
case WM_DESTROY:
{
DeleteObject(hBrush);
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

//modeless callback function
BOOL CALLBACK ModelessDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND static mainwnd=GetParent(hwnd);
switch(msg)
{
case WM_INITDIALOG:
SetFocus(hwnd);
return TRUE;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED) {
switch (LOWORD (wParam))
{
//responds to radio button click
case IDC_RED://red button selected.
bk_color=RGB(255, 0, 0);
hBrush = CreateSolidBrush(bk_color);
break;
case IDC_YELLOW://yellow button selected
bk_color=RGB(255, 255, 0);
hBrush = CreateSolidBrush(bk_color);
break;
case IDC_GREEN://grey button selected
bk_color=RGB(0, 255, 0);
hBrush=CreateSolidBrush(bk_color);
break;
}
InvalidateRect(mainwnd, NULL,TRUE);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);//destroy dialog window
return 1 ;
break;
}
return 0;
}

//modal callback function
BOOL CALLBACK ModalDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND static mainwnd=GetParent( hwnd);
TCHAR *words = TEXT("A simple demonstration of a modal dialog window.");
HWND static staticbox;
switch(msg)
{
case WM_INITDIALOG:
staticbox = GetDlgItem(hwnd,IDC_STATIC1 );
SetWindowText(staticbox, words);
break;
return 0;
case WM_CLOSE:
EndDialog (hwnd, 0);//destroy dialog window
return TRUE ;
break;
}
return 0;
}

//register modeless dialog class
void RegisterModelessDialogClass(HWND hwnd) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = (WNDPROC) ModelessDialogProc;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszClassName = TEXT("DialogClass");
RegisterClassEx(&wc);
}

//register modal dialog class
void RegisterModalDialogClass(HWND hwnd) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.lpfnWndProc = (WNDPROC) ModalDialogProc;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpszClassName = TEXT("DialogClass");
RegisterClassEx(&wc);
}
void CreateModelessDialogBox(HWND hwnd)
{
//creates and shows modeless dialog window
HWND hDlgModeless = CreateDialog(NULL,MAKEINTRESOURCE(IDD_MODELESS), hwnd, (DLGPROC)ModelessDialogProc);
ShowWindow (hDlgModeless, SW_SHOW) ;
}
//creates and shows modal dialog window
void CreateModalDialogBox(HWND hwnd)
{
DialogBox(NULL,MAKEINTRESOURCE(IDD_MODAL), hwnd, ModalDialogProc);
}