我试图在 C++ 中的 OPENFILENAME 结构上使用自定义模板,但无法弄清楚我到底做错了什么。这是我到目前为止所拥有的:
#include <windows.h>
#include <iostream>
#include "resource.h"
void main() {
HWND hwnd = NULL;// owner window
OPENFILENAME ofn;
CHAR File[256];
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = File;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(File);
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = MAX_PATH;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = OFN_ENABLETEMPLATE;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = NULL;
ofn.lCustData = 0L;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = "IDD_DIALOGBAR";
if (GetOpenFileName(&ofn)==TRUE)
{
//do something with filename
}
std::cout << CommDlgExtendedError();
}
IDD_DIALOGBAR
是我添加到项目中的自定义资源。为此,我在 Visual Studio 中创建了一个新的空 C++ 项目,然后在解决方案资源管理器中右键单击项目名称,然后单击“添加”-->“资源”。然后我从可用资源列表中选择了“IDD_DIALOGBAR”。这为项目添加了一个新资源,当我在 Visual Studio 中切换到“资源视图”时可以查看该资源。
当我运行程序时,对话框根本没有出现。结果CommDlgExtendedError()
是CDERR_FINDRESFAILURE: The common dialog box function failed to find a specified resource
。
我也试过改变
ofn.lpTemplateName = "IDD_DIALOGBAR"
至
ofn.lpTemplateName = MAKEINTRESOURCE(IDD_DIALOGBAR)
但这导致了不同的错误消息:
CDERR_DIALOGFAILURE: The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle.
我错过了什么?我没有正确引用资源吗?