我正在运行以下命令在Windows中创建一个对话框。当我运行它时,我得到了以下错误:
Error 1 error C2065: 'IDD_DLGFIRST' : undeclared identifier
以下是代码:
HWND hWnd;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
// --------------------------------------------- ------------------------------
我知道有资源文件但我对此并不了解。 有人可以帮我解决这个错误。
答案 0 :(得分:3)
您需要在资源文件中定义符号,以及调用MAKEINTRESOURCE的文件。通常它是通过两个地方#include
的公共头文件完成的(例如resource.rc和main.cpp中的#include resource.h
。
例如,在resource.h
#define IDD_DLGFIRST 1
中放置了#define IDD_DLGFIRST 1001
#define IDC_STATIC 1002
。只需确保该数字在资源中是唯一的。
修改强>
举个例子:
RESOURCE.H
#include <windows.h>
#include "resource.h"
IDD_DLGFIRST DIALOGEX 0, 0, 170, 62
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My dialog"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "My first dialog box, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
DEFPUSHBUTTON "OK",IDOK,113,41,50,14,WS_GROUP
END
yourapp.rc
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//Open dialog box
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST), HWND_DESKTOP, DlgProc);
return 0;
}
INT_PTR CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
break; //Don't forget about the break;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
}
yourapp.cpp
{{1}}