Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

c++ - Dialog Box Closes Immediately After It Opens

I have a dialog box that I am using for user input, and it was working just fine, now no matter what I do the dialog pops up for one second and then the program dies with no warning, calling WM_DESTROY even after I comment out all the possible exits. There is only one error, which is the no_init_all error, but that shows up every time there is a runtime error, and is pretty useless in trying to find the solution.

Here is my code:

project.cpp

BOOL CreateMyDialog(HINSTANCE hInstance) {
    HWND hWnd = CreateDialog(NULL, MAKEINTRESOURCE(myDialogResource), NULL, (DLGPROC)myDialogProcess);
    if (!hWnd) {
        return FALSE;
    }
    myDialogGlobalHandle = hWnd;
    ShowWindow(hWnd, SW_SHOW);
    //UpdateWindow(hWnd);

    return TRUE;
}

INT_PTR CALLBACK myDialogProcess(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    INITCOMMONCONTROLSEX InitCtrlEx;

    InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
    InitCommonControlsEx(&InitCtrlEx);

    switch(msg) {
        case WM_INITDIALOG: {
            //Do stuff to prep the dialog
            return TRUE;
        }
        case WM_DESTROY: {
            PostQuitMessage(WM_QUIT);
            break;
        }
    }
    return TRUE;
}

project.rc

myDialogResource DIALOGEX 600, 400, 286, 108
    STYLE DS_SYSMODAL | DS_SETFONT | DS_SETFOREGROUND | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION
    EXSTYLE WS_EX_OVERLAPPEDWINDOW /*| WS_EX_APPWINDOW*/
    CAPTION "My Dialog"
    FONT 8, "MS Shell Dlg", 400, 0, 0x1
    BEGIN
        COMBOBOX        dropdownList,112,7,61,30,CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
        //... it's all syntactically correct controls that still work the split second the dialog is on the screen
    END

This is the shortened code, and even with this the dialog opens on top of itself and glitches on the screen. With my full code, it appears normally for a split-second before the app closes. I believe if I can fix this small bit, I can find the issue with my dialog. So my question is, what's the issue?

EDIT:

Here is my wWinMain:

HWND myDialogGlobalHandle;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    INITCOMMONCONTROLSEX InitCtrlEx;

    InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
    InitCommonControlsEx(&InitCtrlEx);
    if (!CreateMyDialog(hInstance)) {
        //return FALSE;
    }
    else {
        MSG message = { 0 };
        while (GetMessage(&message, nullptr, 0, 0)) {
            if (!IsDialogMessage(myDialogGlobalHandle, &message)) {
                DispatchMessage(&message);
            }
        }
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PROJECT));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int)msg.wParam;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As it turns out, all my code was correct. The issue was with the case statements in my dialog procedure, for some reason Visual Studio didn't like brackets in the case statements. After trying everything and then just replacing them with break; statements, my code started working again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...