Click here to Skip to main content
14,668,602 members
Rate this:
Please Sign up or sign in to vote.
See more:
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);
//Global Variables/////
//HWND
HWND hMainWindow, hClose, hStaticClose, hStaticYes, hStaticNo, hStaticGroup;
//HCURSOR
HCURSOR gauntlet = (HCURSOR)LoadImage(GetModuleHandle(NULL),
                    MAKEINTRESOURCE(IDC_SWORD), IMAGE_CURSOR, 32, 32, 0);
//HBITMAP
HBITMAP hBckground;
//Close window (Dialog Box) loop
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;
}
//Dialog Box/////////////////
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);
}
//Main Loop////////////
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));

            // Create status bar
                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;
}
//SDI Creation//////////////////
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.
Posted
Updated 19-Jun-20 6:15am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Got it.. deleted the WM_Destroy case and the SendMessage((HWND)hClose, WM_CLOSE, 0, 0); from the case ID_SNO and added PostQuitMessage(0); to the case ID_SYES

Works fine thanks..
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100