罗伯特,您可以使用对话框模板来实现这一点。首先,您必须将模板作为应用程序资源存储,然后使用
TOpenFilename
结构加载模板(不用担心名称,对于打开和保存对话框是相同的),最后调用传递
TOpenFilename
结构的
GetSaveFileName
函数。请查看以下示例:创建一个资源文件(称为 SaveDialog.rc),并添加对话框模板(请注意添加了 MyCheckBox)。
MYSAVEFILE DIALOG -1, 1, 300, 60
STYLE DS_3DLOOK | DS_CONTROL | WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS
CAPTION ""
FONT 8, "Tahoma"
{
CONTROL "MyCheckBox", 666, "button", BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 84, 19, 60, 12
}
这是源代码
Uses
CommDlg;
var
lpofn : TOpenFilename;
lpstrFile: Array[0..MAX_PATH-1] of Char;
{$R *.dfm}
{$R SaveDialog.Res}
function _lpfnHook(hdlg: HWND; uiMsg:UINT;wParam:WPARAM;lParam:LPARAM): UINT stdcall;
begin
Result:=0;
case uiMsg of
WM_INITDIALOG : CheckDlgButton(hdlg,666,BST_CHECKED);
WM_COMMAND :
case wParam of
666:
begin
if (IsDlgButtonChecked(hdlg,666)=BST_CHECKED) then
ShowMessage('MyCheckBox was checked')
else
if (IsDlgButtonChecked(hdlg,666)=BST_UNCHECKED) then
ShowMessage('MyCheckBox was unchecked');
end;
end;
end;
end;
procedure TFrmMain.Button1Click(Sender: TObject);
begin
ZeroMemory(@lpofn,sizeof(lpofn));
lpofn.lStructSize := SizeOf(lpofn);
lpofn.hwndOwner := Handle;
lpofn.hInstance := hInstance;
lpofn.lpstrFilter := 'All files (*.*)'#0'*.*'#0#0;
lpofn.lpstrTitle := 'Save As';
lpofn.lpstrFile := lpstrFile;
lpofn.nMaxFile := MAX_PATH;
lpofn.lpTemplateName :='MYSAVEFILE';
lpofn.lpfnHook := _lpfnHook;
lpofn.Flags := OFN_EXPLORER or OFN_CREATEPROMPT or OFN_HIDEREADONLY or
OFN_PATHMUSTEXIST or OFN_ENABLEHOOK or OFN_ENABLETEMPLATE;
if GetSaveFileName(lpofn) then ShowMessage(lpofn.lpstrFile);
end;
这是输出结果:
![在此输入图片描述](https://istack.dev59.com/9W7XB.webp)