Positioning a Dialog in Win32 C++

I am trying to create a dialog such that it always opens in the middle of my application window. This is the usual behaviour for a dialog created with something like MessageBox(). However, if the application window is moved before the dialog is opened, the dialog opens in the middle of where the window WAS and not where it is now. So something behind the scenes is obviously caching the position of the window.

Is there anyway to update that position to indicate where the window actually is? The problem is that with MessageBox(), you don’t get options of positioning a dialog, so I can’t easily reposition it correctly.

There’s an interesting answered question here:
How to Change MessageBox Position in C++ and I’ve tried the proposed solution and to be fair, using TaskDialogIndirect() almost works. You can indeed set the (X, Y) position of the dialog, but the X position is slightly out and you can’t alter the dialog size.

So I decided to create the dialog myself and thought that subclassing my main application window was the easiest.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>LRESULT CALLBACK DialogWndProc(HWND wnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, [[maybe_unused]] DWORD_PTR dwRefData)
{
switch (uMsg)
{
case WM_CREATE:
CreateWindowEx(NULL, L"BUTTON", L"Okay", WS_CHILD | BS_DEFPUSHBUTTON | WS_VISIBLE, 50, 50, 100, 40, wnd, (HMENU)1002, GetModuleHandle(NULL), nullptr);
break;
case WM_CLOSE:
{
HWND owner = GetWindow(wnd, GW_OWNER);
EnableWindow(owner, true);
SetFocus(owner);
DestroyWindow(wnd);
return 0;
}
case WM_NCDESTROY:
RemoveWindowSubclass(wnd, DialogWndProc, uIdSubclass);
break;
}
return DefSubclassProc(wnd, uMsg, wParam, lParam);
}
// hwd is the HANDLE of the main application window
void CustomDialog(HWND hwd, const std::wstring title, const std::wstring body, size_t xp, size_t yp, size_t xs, size_t ys)
{
// Get the class name from the parent window
std::vector<wchar_t> class_name(256);
class_name.resize(static_cast<size_t>(GetClassName(hwd, class_name.data(), static_cast<int>(class_name.size()))));
// Subclass the window and change WNDPROC
BOOL t = SetWindowSubclass(hwd, DialogWndProc, 1, 0);
// Create the modal dialog window
HWND dlg = CreateWindowEx(WS_EX_DLGMODALFRAME, class_name.c_str(), title.c_str(), WS_POPUPWINDOW | WS_CAPTION | DS_MODALFRAME | DS_NOIDLEMSG, xp, yp, xs, ys, hwd, NULL, GetModuleHandle(NULL), nullptr);
ShowWindow(dlg, SW_SHOW);
EnableWindow(hwd, false);
}
</code>
<code>LRESULT CALLBACK DialogWndProc(HWND wnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, [[maybe_unused]] DWORD_PTR dwRefData) { switch (uMsg) { case WM_CREATE: CreateWindowEx(NULL, L"BUTTON", L"Okay", WS_CHILD | BS_DEFPUSHBUTTON | WS_VISIBLE, 50, 50, 100, 40, wnd, (HMENU)1002, GetModuleHandle(NULL), nullptr); break; case WM_CLOSE: { HWND owner = GetWindow(wnd, GW_OWNER); EnableWindow(owner, true); SetFocus(owner); DestroyWindow(wnd); return 0; } case WM_NCDESTROY: RemoveWindowSubclass(wnd, DialogWndProc, uIdSubclass); break; } return DefSubclassProc(wnd, uMsg, wParam, lParam); } // hwd is the HANDLE of the main application window void CustomDialog(HWND hwd, const std::wstring title, const std::wstring body, size_t xp, size_t yp, size_t xs, size_t ys) { // Get the class name from the parent window std::vector<wchar_t> class_name(256); class_name.resize(static_cast<size_t>(GetClassName(hwd, class_name.data(), static_cast<int>(class_name.size())))); // Subclass the window and change WNDPROC BOOL t = SetWindowSubclass(hwd, DialogWndProc, 1, 0); // Create the modal dialog window HWND dlg = CreateWindowEx(WS_EX_DLGMODALFRAME, class_name.c_str(), title.c_str(), WS_POPUPWINDOW | WS_CAPTION | DS_MODALFRAME | DS_NOIDLEMSG, xp, yp, xs, ys, hwd, NULL, GetModuleHandle(NULL), nullptr); ShowWindow(dlg, SW_SHOW); EnableWindow(hwd, false); } </code>
LRESULT CALLBACK DialogWndProc(HWND wnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, [[maybe_unused]] DWORD_PTR dwRefData)
{
    switch (uMsg)
    {
        case WM_CREATE:
            CreateWindowEx(NULL, L"BUTTON", L"Okay", WS_CHILD | BS_DEFPUSHBUTTON | WS_VISIBLE, 50, 50, 100, 40, wnd, (HMENU)1002, GetModuleHandle(NULL), nullptr);
            break;
        case WM_CLOSE:
        {
            HWND owner = GetWindow(wnd, GW_OWNER);
            EnableWindow(owner, true);
            SetFocus(owner);
            DestroyWindow(wnd);
            return 0;
        }
        case WM_NCDESTROY:
            RemoveWindowSubclass(wnd, DialogWndProc, uIdSubclass);
            break;
    }
    return DefSubclassProc(wnd, uMsg, wParam, lParam);
}

// hwd is the HANDLE of the main application window
void CustomDialog(HWND hwd, const std::wstring title, const std::wstring body, size_t xp, size_t yp, size_t xs, size_t ys)
{
    // Get the class name from the parent window
    std::vector<wchar_t> class_name(256);
    class_name.resize(static_cast<size_t>(GetClassName(hwd, class_name.data(), static_cast<int>(class_name.size()))));

    // Subclass the window and change WNDPROC
    BOOL t = SetWindowSubclass(hwd, DialogWndProc, 1, 0);

    // Create the modal dialog window
    HWND dlg = CreateWindowEx(WS_EX_DLGMODALFRAME, class_name.c_str(), title.c_str(), WS_POPUPWINDOW | WS_CAPTION | DS_MODALFRAME | DS_NOIDLEMSG, xp, yp, xs, ys, hwd, NULL, GetModuleHandle(NULL), nullptr);
    ShowWindow(dlg, SW_SHOW);
    EnableWindow(hwd, false);
}

The problem that I’ve found is that WM_CREATE is never received and I don’t understand why. Surely, during the call to CreateWindowEx() the new WNDPROC will receive a WM_CREATE?

Or am I going to have Register an entirely new window class atom for this window?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật