用于自定义 'Save As' 通用对话框的示例对话框定义

Sample dialog definition for customizing 'Save As' common dialog box

我正在尝试自定义标准 win32 "Save As" 对话框。我的代码如下所示:

OPENFILENAME ofn;
//... skipped
ofn.Flags = OFN_ENABLETEMPLATE;
ofn.lpTemplateName = MAKEINTRESOURCEW(RES); // what is the RES?
//... skipped
GetSaveFileName(&ofn);

但是我找不到任何关于什么是 RES 的例子。它必须是资源文件或类似的东西。我需要一个简单的例子来开始。我现在需要的只是将文本行添加到标准 'Save As' 对话框的底部。

终于找到样本了: CommDlgCust.zip

它已经 14 岁了,但仍然有效。可以使用 Visual C++ 2013 轻松打开旧项目文件。

所以 "RES" 是位于 rc 文件中的对话框定义。我想要的答案是:

IDD_OPENSAVE DIALOG FIXED IMPURE  0, 0, 280, 53
STYLE WS_CHILD | WS_CLIPSIBLINGS
FONT 8, "MS Shell Dlg"
BEGIN
    CONTROL         "",IDC_STATIC,"Static",SS_ETCHEDFRAME,6,6,266,38
    CTEXT           "",IDC_STATIC_HINT,12,10,256,28,SS_CENTERIMAGE
END

这是基于 CommDlgCust.zip:

的代码示例
// ---------- resource.h
// last line of the file must be an empty line!

#ifndef IDC_STATIC
#define IDC_STATIC              -1
#endif

#define IDC_STATIC_HINT                 1001
#define IDD_OPENSAVE                    110

// ---------- project.rc

#include "resource.h"

#include "targetver.h"
#include "windows.h"

IDD_OPENSAVE DIALOG FIXED IMPURE  0, 0, 280, 53
STYLE WS_CHILD | WS_CLIPSIBLINGS
FONT 8, "MS Shell Dlg"
BEGIN
    CONTROL         "",IDC_STATIC,"Static",SS_ETCHEDFRAME,6,6,266,38
    CTEXT           "",IDC_STATIC_HINT,12,10,256,28,SS_CENTERIMAGE
END

// ---------- main.cpp

// ...skipped...

UINT CALLBACK OfnHookProc(HWND hDlg, UINT uMsg, UINT /*wParam*/, LONG lParam)
{
    switch (uMsg)
    {
    case WM_NOTIFY:
        OFNOTIFY* pofNotify = (OFNOTIFY*)lParam;
        switch (pofNotify->hdr.code)
        {
            case CDN_FOLDERCHANGE:
            {
                LPTSTR pszPathName;
                pszPathName = new TCHAR[_MAX_DIR];
                HWND hTrueDlg = GetParent(hDlg);
                SendMessage(hTrueDlg, CDM_GETFOLDERPATH, _MAX_PATH, (LONG)pszPathName);
                SetDlgItemText(hDlg, IDC_STATIC_HINT, pszPathName);
                delete[] pszPathName;
                return TRUE;
            }
        }
    }
    return FALSE;
}

bool MyGetSaveFileName()
{
    WCHAR lpstrFile[1024] = {0};

    OPENFILENAME ofn;
    memset(&ofn, 0, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hWnd;
    ofn.hInstance = (HINSTANCE)GetWindowLong(ofn.hwndOwner, GWL_HINSTANCE);
    ofn.lpstrFilter = L"*.TXT";
    ofn.lpstrFile = lpstrFile;
    ofn.nMaxFile = 1023;
    ofn.lpstrTitle = L"Save";
    ofn.Flags = OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
    ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENSAVE);
    ofn.lpfnHook = OfnHookProc;

    if (GetSaveFileName(&ofn) != 0)
        return true;

    return false;
}

我希望这会对某人有所帮助。