Help creating Modeless Dialogs using Resources

Hi, LeafyCiruits here!

Comp Spec:
OS: Windows 8.1 64-bit
IDE: Code::Blocks 13.12
Compiler: TDM-GCC MinGW 4.7.1 using C++11

Hey guys, it's been over a year since I've visited the forums, and I'm glad I'm back! However, I need your help once again.

I've recently started learning the harsh Win32API with theForger's tutorial on it (link at bottom of post). I'm learning how to create "modeless" Dialogs, as they're called. I've typed out the dialog as a resource so far, along with a menu. When I try to compile though, the compiler says I have a syntax error in my RC code. I've checked the MS documentation on RC syntax and I can't find anything I'm doing wrong.

Here's the resource.rc file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "resources.h"

IDR_MAINMENU MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP "&Dialog"
    BEGIN
        MENUITEM "&Show", ID_DIALOG_SHOW
        MENUITEM "&Hide", ID_DIALOG_HIDE
    END
END

IDD_TOOLBAR DIALOGEX 0, 0, 98, 52, ID_MAINMENU_HELPID
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION    //Error occurs here...
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My dialog toolbar"
FONT 10, "MS Sans Serif"
BEGIN
    PUSHBUTTON "&Press me!", IDC_PRESS, 7, 7, 84, 14
    PUSHBUTTON "&No me!", IDC_OTHER, 7, 31, 84, 14
END


and the resource.h:
1
2
3
4
5
6
7
8
9
10
11
#define IDR_MAINMENU 101
#define IDD_TOOLBAR 102

#define IDC_PRESS 201
#define IDC_OTHER 202

#define ID_FILE_EXIT 1001
#define ID_DIALOG_SHOW 1002
#define ID_DIALOG_HIDE 1003

#define ID_MAINMENU_HELPID 9001 


I don't have anything but a "windows skeleton" for my main.cpp, and it compiles fine, but here it is anyways:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <windows.h>
#include "resources.h"

const char g_szClassName[] = "name";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szClassName;

    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Class Registration failed!", "Error!",
                   MB_ICONERROR | MB_OK);
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Modeless Dialogs",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL);

    if (hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation failed!", "Error!",
                   MB_ICONERROR | MB_OK);
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}



I know that the "modeless" part of the dialog box isn't defined in the resource file, which is why this syntax error bugs me. I know you would use CreateDialog() and go about from there. I would implement that in WndProc under a WM_CREATE case, but I haven't done that yet, obviously.

I would really appreciate any help and/or constructive criticism, especially with my code. I'm not planning on getting a book nor taking a course on Win32API, so all I've got is theForger's tutorial. Since I don't know much about the API, if theForger makes a mistake, so will I. Your advice is very much appreciated.


theForger's Win32API Tutorial: http://www.winprog.org/tutorial/
Last edited on
The most obvious error in your .rc file is the include of "resources.h" if your header file is actually called "resource.h"

But you have many other errors if you want to create a modeless dialog from a resource template.

First you need to add the line CLASS "name" to your dialog description in the .rc file.

But even when you get the .rc file to compile, you need to use CreateDialog instead of CreateWindow. You also are going to need to set the cbWndExtra field of your WNDCLASS structure to DLGWINDOWEXTRA instead of 0.

Since this is a dialog, your message loop should only call Translate and Dispatch Message if IsDialogMessage returns false.

Finally your WndProc should return the return value of DefWindowProc if WndProc does not actually handle the message.
I think the problem with your rc file is you included ID_MAINMENU_HELPID for some reason.

Maybe try changing

1
2
3
4
5
6
7
8
9
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52, ID_MAINMENU_HELPID
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION    //Error occurs here...
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My dialog toolbar"
FONT 10, "MS Sans Serif"
BEGIN
    PUSHBUTTON "&Press me!", IDC_PRESS, 7, 7, 84, 14
    PUSHBUTTON "&No me!", IDC_OTHER, 7, 31, 84, 14
END


to

1
2
3
4
5
6
7
8
9
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52,
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION    //Error occurs here...
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My dialog toolbar"
FONT 10, "MS Sans Serif"
BEGIN
    PUSHBUTTON "&Press me!", IDC_PRESS, 7, 7, 84, 14
    PUSHBUTTON "&No me!", IDC_OTHER, 7, 31, 84, 14
END


At least that's the only difference I see between your rc file's format and the winforgers tutorial.

http://www.winprog.org/tutorial/modeless_dialogs.html

1
2
3
4
5
6
7
8
9
IDD_TOOLBAR DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "My Dialog Toolbar"
FONT 8, "MS Sans Serif"
BEGIN
    PUSHBUTTON      "&Press This Button",IDC_PRESS,7,7,84,14
    PUSHBUTTON      "&Or This One",IDC_OTHER,7,31,84,14
END
Last edited on
Topic archived. No new replies allowed.