【问题标题】:How to use resource for Windows API custom dialog template?如何使用 Windows API 自定义对话框模板的资源?
【发布时间】:2014-01-15 16:05:48
【问题描述】:

我试图在 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.

我错过了什么?我没有正确引用资源吗?

【问题讨论】:

  • 模板可能正在使用您忘记注册的自定义控件

标签: c++ controltemplate getopenfilename


【解决方案1】:

ofn.hInstance 需要设置为具有对话框模板资源的模块(可执行文件或 DLL)的HINSTANCE

【讨论】:

    猜你喜欢
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多