3

I just installed Visual Studio 2022 and I'm trying to get an application working with a dialog box as a main window in order to make the GUI easier to produce. But the thing is that I've found nothing on the web working without MFC, and I tried a lot of code but nothing concluded. Any help would be great.

Here's what I tried, the program is running but no windows appear on the screen (and i set the dialogbox as visible in the parameters) :

#include "framework.h"
#include "WindowsProject1.h"
#include <windows.h>

// +----------------------------------------------------------------------------
// | -DialogProc()-
// | This is the callback function that processes the messages sent to our
// | dialog box. Most everything of consequence in such a program is either
// | done here or called from here.
// +----------------------------------------------------------------------------
BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    // check message type
    switch (uMsg)
    {
    case WM_COMMAND:
        // here's where you would process commands sent to the dialog box
        switch (LOWORD(wParam))
        {
        case IDOK:
            // OK button clicked - perform data validation and such here
            EndDialog(hWnd, TRUE);
            break;

        case IDCANCEL:
            // Cancel button clicked
            EndDialog(hWnd, FALSE);
            break;

            // process messages from additional controls here
        }
        break;

    case WM_INITDIALOG:
        // perform any necessary initialization here - return FALSE if you
        // set the focus to a control yourself, otherwise just break and
        // allow the function to return TRUE to indicate that the keyboard
        // focus should be set automatically
        break;

    default:
        // return zero if we do not process this message
        return FALSE;
    }

    // return nonzero if we did process the message
    return TRUE;
}

// +----------------------------------------------------------------------------
// | -WinMain()-
// | Program entry point. Normally you'd find a message loop in WinMain(), but
// | for a dialog-based application, all we have to do is create the dialog and
// | then quit. You should probably add some kind of error-checking facility in
// | case the call to DialogBox() fails. You might also wish to do something
// | with the return value of DialogBox() if it's important. Finally, you could 
// | use DialogBoxParam() instead of DialogBox() if you wanted to pass a 
// | parameter to the dialog box.
// +----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    HWND hDlg =(HWND) DialogBox(hInstance,MAKEINTRESOURCE(IDD_FORMVIEW), NULL, (DLGPROC)DialogProc);
    ShowWindow(hDlg, SW_SHOW);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) == TRUE)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

edit 1 : I have found a project of a dialogbox written in C on visual studio 2017 compatible with visual studio 2022, here's the link : https://github.com/weirenxue/dialogboxparam-example

New contributor
ciddu42 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • (1) DialogBox() creates modal dialog, and runs its own internal message loop. This means ShowWindow() and message loop after it does not make any sense. (2) DialogBox() is a macro, returns void according to docs, so cast of the DialogBox() call result to HWND does not make sense and even shouldn't compile. Underlying function DialogBoxParam() returns dialog result, not HWND. Does this thing ever compile? Commented 2 days ago
  • 2
    A minimal reproducible example with the resource IDD_FORMVIEW should be posted. Commented 2 days ago
  • 1
    It's probably simpler to use MessageBox() for that instead of rolling your own. Commented 2 days ago
  • In fact, if dialog resource is OK, then all you needed is call to DialogBox(). So yes, check carefully your dialog resource. Commented 2 days ago

1 Answer 1

3

The DialogBox call creates the dialog box and runs it until your DialogProc calls EndDialog.

If the dialog doesn't appear, it's almost always because something went wrong creating it. The return value of DialogBox is not an HWND, but an INT_PTR. If it couldn't create the dialog, it returns -1. So check to see what it's returning. If you get -1, then GetLastError might give you a clue as to what went wrong.

Common problems are:

  • The dialog resource doesn't exist or has a bug. Make sure it's actually defined with the numeric ID that IDD_FORMVIEW is defined to be. If the resource is actually called IDD_FORMVIEW, then put that in a string instead of MAKEINTRESROUCE.
  • The DialogProc didn't handle the WM_INITDIALOG message correctly.

Since the DialogBox call runs the dialog, it won't return until your DialogProc called EndDialog. Once it does, there is no reason to call ShowWindow or to have your own message loop.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.