C ++窗口对话框错误 [英] C++ window Dialog error

查看:57
本文介绍了C ++窗口对话框错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";


// defining dialog box
    IDD_ABOUT DIALOG DISCARDABLE  0, 0, 239, 66
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "My About Box"
    FONT 8, "MS Sans Serif"
    BEGIN
        DEFPUSHBUTTON   "&OK",IDOK,174,18,50,14
        PUSHBUTTON      "&Cancel",IDCANCEL,174,35,50,14
        GROUPBOX        "About this program...",IDC_STATIC,7,7,225,52
        CTEXT           "An example program showing how to use Dialog Boxes\r\n\r\nby theForger",
                        IDC_STATIC,16,18,144,33
    END

// about us dialog procedure
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:

        return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK:
                    EndDialog(hwnd, IDOK);
                break;
                case IDCANCEL:
                    EndDialog(hwnd, IDCANCEL);
                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;
            HICON hIcon, hIconSm;
            hMenu = CreateMenu();
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Stuff");
            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_HELP_ABOUT, "&About");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
            SetMenu(hwnd, hMenu);
        }
        break;
        case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case ID_HELP_ABOUT:
            {
                int ret = DialogBox(GetModuleHandle(NULL), 
                    MAKEINTRESOURCE(IDD_ABOUT), hwnd, AboutDlgProc);
                if(ret == IDOK){
                    MessageBox(hwnd, "Dialog exited with IDOK.", "Notice",
                        MB_OK | MB_ICONINFORMATION);
                }
                else if(ret == IDCANCEL){
                    MessageBox(hwnd, "Dialog exited with IDCANCEL.", "Notice",
                        MB_OK | MB_ICONINFORMATION);
                }
                else if(ret == -1){
                    MessageBox(hwnd, "Dialog failed!", "Error",
                        MB_OK | MB_ICONINFORMATION);
                }
            }
            break;
            case ID_FILE_EXIT:
                PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;
            case ID_STUFF_GO:

            break;
        }
        break;
        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;

    //Step 1: Registering the Window Class
    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.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

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

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}










$ b编译时$ b,它给了我错误:





IDD_ABOUT没有命名类型





请让我知道问题是什么






on compilation , it gives me error:


IDD_ABOUT does not name a type


please let me know whats the problem

推荐答案

StackOverflow上的这个帖子似乎有答案:

This thread on StackOverflow seems to have the answer:



创建一个名为resource.h的包含文件,并在其中添加以下行:


Create an include file called resource.h and add this line to it:

#define IDD_ABOUT   100



然后将它包含在.cpp和.rc文件的顶部:


Then include it at the top of your .cpp and .rc files:

#include "resource.h" // add this line






它看起来就像你已经包含 resource.h 文件一样;您是否仔细检查过您是否在其中定义了 IDD_ABOUT 符号?


It looks like you've already included the resource.h file; have you double-checked that you've defined the IDD_ABOUT symbol in it?


//定义对话框看起来很奇怪:

The entire code block under //defining dialog box looks very odd:
IDD_ABOUT DIALOG DISCARDABLE  0, 0, 239, 66
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My About Box"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "&OK",IDOK,174,18,50,14
    PUSHBUTTON      "&Cancel",IDCANCEL,174,35,50,14
    GROUPBOX        "About this program...",IDC_STATIC,7,7,225,52
    CTEXT           "An example program showing how to use Dialog Boxes\r\n\r\nby theForger",
                    IDC_STATIC,16,18,144,33
END





好​​像你用一个工具复制粘贴了这个工具,以某种方式随机消除了许多非字母字符,如括号和逗号。



有很多符号应该分开,但不是,所以即使你设法解决了IDD_ABOUT错误,你也会遇到下一个未知符号或缺少分隔符。



要么仔细检查您的来源,并确保您拥有完整正确的代码,或者只是自己动手 - 这并不困难。



在旁注中,该代码中没有任何内容是C ++ - 这是纯粹的C。



Seems like you copy-pasted this with a tool that somehow eliminated lots of non-alpha characters such as parenthesis and commas at random.

There are a whole lot of symbols that should be separated, but are not, so even if you manage to fix that IDD_ABOUT error, you'll just run into the next unknown symbol or missing separator.

Either double check your source that you got the code from and make sure you really have the complete and correct code, or just do it yourself - it's not that difficult.

On a sidenote, nothing in that code is C++ - this is pure C.


我的第一个猜测IDD_ABOUT没有在resource.h中定义一些数字。



完整的对话框模板内容属于rc文件。它可以在Visual Studio的资源视图窗口中打开。它可能会抱怨其他错误。
My first guess is that the IDD_ABOUT isnt defined in the resource.h with some number.

The complete dialog template stuff belongs in a rc-file. It can be opened in a Resource View Window in Visual Studio. It may complain about other bugs.


这篇关于C ++窗口对话框错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆