Hello.. I created a custom Dialog Box that has a message and 2 buttons
typically to the a quit message The "Yes" message is closing the application but the "No" button instead to close the Dialog Box is either closing the app. Now this is the code:
#include <windows.h>
#include <commctrl.h>
#include <cstdio>
#include <mmsystem.h>
#include "resources.h"
#define IDC_STATUS 50
#define ID_SYES 51
#define ID_SNO 52
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
HWND hMainWindow, hClose, hStaticClose, hStaticYes, hStaticNo, hStaticGroup;
HCURSOR gauntlet = (HCURSOR)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDC_SWORD), IMAGE_CURSOR, 32, 32, 0);
HBITMAP hBckground;
LRESULT CALLBACK CloseWndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)
{
case WM_COMMAND:
switch(wp)
{
case ID_SYES:
{
DestroyWindow(hWnd);
break;
}
case ID_SNO:
{
EnableWindow(hMainWindow, true);
SendMessage((HWND)hClose, WM_CLOSE, 0, 0);
break;
}
}
case WM_SETCURSOR:
{
SetCursor(gauntlet);
return 1;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
return 0;
}
void registerCloseClass(HINSTANCE hInst)
{
WNDCLASSW close = {0};
close.hbrBackground = (HBRUSH)COLOR_WINDOW;
close.hInstance = hInst;
close.lpfnWndProc = CloseWndProc;
close.lpszClassName = L"myCloseClass";
RegisterClassW(&close);
}
void displayClose(HWND hWnd)
{
hClose = CreateWindowW(L"myCloseClass", L"Warning !", WS_VISIBLE | WS_CAPTION,
555, 280, 280, 140, hWnd, NULL, NULL, NULL);
EnableWindow(hWnd, false);
hStaticClose = CreateWindowW(L"static", L"This will close the program. Are you sure ?",
WS_CHILD | WS_VISIBLE | SS_CENTER, 37, 20, 200, 50, hClose, NULL, NULL, NULL);
hStaticYes = CreateWindowW(L"button", L"Yes",
WS_CHILD | WS_VISIBLE | BS_CENTER, 33, 70, 80, 25, hClose, (HMENU)ID_SYES, NULL, NULL);
hStaticNo = CreateWindowW(L"button", L"No",
WS_CHILD | WS_VISIBLE | BS_CENTER, 160, 70, 80, 25, hClose, (HMENU)ID_SNO, NULL, NULL);
hStaticGroup = CreateWindowW(L"button", NULL,
WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 7, 0, 261, 107, hClose, NULL, NULL, NULL);
}
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)
{
case WM_PAINT:
{
BITMAP bm;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBckground);
GetObject(hBckground, sizeof(bm), &bm);
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
}
case WM_CREATE:
{
hBckground = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_MAINBMP));
HWND hStatus;
int statwidths[] = {150, 450, -1};
hStatus = CreateWindowW(STATUSCLASSNAMEW, NULL, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
0, 0, 0, 0, hWnd, (HMENU)IDC_STATUS, GetModuleHandle(NULL), NULL);
SendMessageW(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
SendMessageW(hStatus, SB_SETTEXT, 0, (LPARAM)" Author: Me");
SendMessageW(hStatus, SB_SETTEXT, 1, (LPARAM)" Ver. 1.00 ");
SendMessageW(hStatus, SB_SETTEXT, 2, (LPARAM)" e-mail & other stuff");
}
case WM_SETCURSOR:
SetCursor(gauntlet);
return 1;
case WM_CLOSE:
{
PlaySound(MAKEINTRESOURCE(IDW_CLICK), NULL, SND_RESOURCE | SND_ASYNC);
displayClose(hWnd);
}
break;
case WM_DESTROY:
DeleteObject(hBckground);
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hWnd, msg, wp, lp);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
InitCommonControls();
WNDCLASSW wc = {0};
wc.hbrBackground = (HBRUSH)COLOR_WINDOW + 1;
wc.hIcon = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, 32, 32, 0);
wc.hInstance = hInst;
wc.lpfnWndProc = MainWndProc;
wc.lpszClassName = L"myMainClass";
wc.style = 0;
wc.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU);
if(!RegisterClassW(&wc))
return -1;
registerCloseClass(hInst);
hMainWindow = CreateWindowW(L"myMainClass", L"Application",
WS_SYSMENU | WS_MINIMIZEBOX | WS_CAPTION,
300, 100, 800, 550, NULL, NULL, NULL, NULL);
ShowWindow(hMainWindow, nCmdShow);
UpdateWindow(hMainWindow);
MSG msg = {0};
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Does anyone has the idea how to do that ? Probably some of you will ask why didn't I just use a system dialog box.. Just because I have a custom cursor and when I pass over a dialog box it change to the default one in the system.
Thank you !
What I have tried:
I tried to send message to the window that holds the dialog and all other controls on
ID_SNO is pressed, but instead pressing the No button close the hole aplication.