Delphi 添加IFileDialogCustomize按钮事件
我可以使用FileSaveDialog1.Dialog.QueryInterface创建一个按钮,如下所示。如何设置和处理OnButton click事件,以便我可以响应按钮单击Delphi 添加IFileDialogCustomize按钮事件,delphi,Delphi,我可以使用FileSaveDialog1.Dialog.QueryInterface创建一个按钮,如下所示。如何设置和处理OnButton click事件,以便我可以响应按钮单击 procedure TForm1.FileSaveDialog1Execute(Sender: TObject); const dwVisualGroup1ID: DWORD = 1900; var c: IFileDialogCustomize; d: IFileDialogControlEvents;
procedure TForm1.FileSaveDialog1Execute(Sender: TObject);
const
dwVisualGroup1ID: DWORD = 1900;
var
c: IFileDialogCustomize;
d: IFileDialogControlEvents;
begin
if FileSaveDialog1.Dialog.QueryInterface(IFileDialogCustomize, c) = S_OK then
begin
// Add a Advanced Button
c.AddPushButton(dwVisualGroup1ID, 'Advanced');
c.MakeProminent(dwVisualGroup1ID);
// Setup the PushButton Click event?
end;
您需要实现IFileDialogControlEvents。然后调用IFileDialog。建议传递IFileDialogControlEvents接口。单击按钮时,将调用IFileDialogControlEvents.OnButtonClicked方法。在XE2中,以下方法对我很有效:
type
TMyFileDialogEvents = class(TInterfacedObject, IFileDialogEvents, IFileDialogControlEvents)
public
{ IFileDialogEvents }
function OnFileOk(const pfd: IFileDialog): HResult; stdcall;
function OnFolderChanging(const pfd: IFileDialog;
const psiFolder: IShellItem): HResult; stdcall;
function OnFolderChange(const pfd: IFileDialog): HResult; stdcall;
function OnSelectionChange(const pfd: IFileDialog): HResult; stdcall;
function OnShareViolation(const pfd: IFileDialog; const psi: IShellItem;
out pResponse: DWORD): HResult; stdcall;
function OnTypeChange(const pfd: IFileDialog): HResult; stdcall;
function OnOverwrite(const pfd: IFileDialog; const psi: IShellItem;
out pResponse: DWORD): HResult; stdcall;
{ IFileDialogControlEvents }
function OnItemSelected(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD;
dwIDItem: DWORD): HResult; stdcall;
function OnButtonClicked(const pfdc: IFileDialogCustomize;
dwIDCtl: DWORD): HResult; stdcall;
function OnCheckButtonToggled(const pfdc: IFileDialogCustomize;
dwIDCtl: DWORD; bChecked: BOOL): HResult; stdcall;
function OnControlActivating(const pfdc: IFileDialogCustomize;
dwIDCtl: DWORD): HResult; stdcall;
end;
const
dwVisualGroup1ID: DWORD = 1900;
function TMyFileDialogEvents.OnFileOk(const pfd: IFileDialog): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnFolderChange(const pfd: IFileDialog): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnFolderChanging(const pfd: IFileDialog;
const psiFolder: IShellItem): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnOverwrite(const pfd: IFileDialog;
const psi: IShellItem; out pResponse: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnSelectionChange(const pfd: IFileDialog): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnShareViolation(const pfd: IFileDialog;
const psi: IShellItem; out pResponse: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnTypeChange(const pfd: IFileDialog): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnItemSelected(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD; dwIDItem: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnButtonClicked(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD): HResult;
begin
if dwIDCtl = dwVisualGroup1ID then begin
// ...
Result := S_OK;
end else begin
Result := E_NOTIMPL;
end;
end;
function TMyFileDialogEvents.OnCheckButtonToggled(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD; bChecked: BOOL): HResult;
begin
Result := E_NOTIMPL;
end;
function TMyFileDialogEvents.OnControlActivating(const pfdc: IFileDialogCustomize; dwIDCtl: DWORD): HResult;
begin
Result := E_NOTIMPL;
end;
var
FileDialog:IFileDialog=nil;
MyEvents:IFileDialogEvents=nil;
MyEventsCookie:DWORD=0;
过程TForm1.FileSaveDialog1Execute(发送方:TObject);
变量
c:IFileDialogCustomize;
d:IFileDialogEvents;
曲奇:德沃德;
开始
如果支持(FileSaveDialog1.Dialog,IFileDialogCustomize,c),则
开始
//添加一个高级按钮
c、 添加按钮(dwVisualGroup1ID,“高级”);
c、 使突出(dwVisualGroup1ID);
//设置按钮点击事件
d:=TMyFileDialogEvents.Create;
如果成功(FileSaveDialog1.Dialog.advice(d,cookie)),则
开始
FileDialog:=FileSaveDialog1.Dialog
MyEvents:=d;
MyEventsCookie:=饼干;
结束;
结束;
结束;
程序TForm1.按钮1单击(发送方:TObject);
变量
Ok:布尔;
开始
FileDialog:=nil;
MyEvents:=零;
MyEventsCookie:=0;
尝试
确定:=FileSaveDialog1.Execute;
最后
如果(FileDialog nil)和(MyEventsCookie 0),则
FileDialog.Unadvise(MyEventsCookie);
FileDialog:=nil;
MyEvents:=零;
MyEventsCookie:=0;
结束;
如果可以的话。。。
结束;
不,我不能。剩下的你可以自己做。在这一点上,你有你需要的一切。现在您知道了所有相关的接口和方法。使用MSDN文档填写详细信息。如何设置事件类?键入TTestEvent=class(TInterfacedObject、IFileDialogEvents、IFileDialogControlEvents)函数OnButtonClicked(pfdc:cardinal;dwIDCtl:cardinal):HResult;谢谢。。事件正在顺利执行。我应该调用FileSaveDialog1.Dialog.Unadvise()?微软说你应该调用Unadvise()
。但是,要使用TFileSaveDialog
,必须在OnExecute
事件中保存对TFileSaveDialog.Dialog
接口的引用,以便退出Execute()
时不会释放对话框。然后您可以Unadvise()
处理程序并释放引用以完成释放对话框。“我会更新我的答案来证明这一点。”@MasonWheeler我无法想象我为什么要写那个。文件很清楚。我删除了评论。
var
FileDialog: IFileDialog = nil;
MyEvents: IFileDialogEvents = nil;
MyEventsCookie: DWORD = 0;
procedure TForm1.FileSaveDialog1Execute(Sender: TObject);
var
c: IFileDialogCustomize;
d: IFileDialogEvents;
cookie: DWORD;
begin
if Supports(FileSaveDialog1.Dialog, IFileDialogCustomize, c) then
begin
// Add a Advanced Button
c.AddPushButton(dwVisualGroup1ID, 'Advanced');
c.MakeProminent(dwVisualGroup1ID);
// Setup the PushButton Click event
d := TMyFileDialogEvents.Create;
if Succeeded(FileSaveDialog1.Dialog.Advise(d, cookie)) then
begin
FileDialog := FileSaveDialog1.Dialog
MyEvents := d;
MyEventsCookie := cookie;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Ok: Boolean;
begin
FileDialog := nil;
MyEvents := nil;
MyEventsCookie := 0;
try
Ok := FileSaveDialog1.Execute;
finally
if (FileDialog <> nil) and (MyEventsCookie <> 0) then
FileDialog.Unadvise(MyEventsCookie);
FileDialog := nil;
MyEvents := nil;
MyEventsCookie := 0;
end;
if Ok then ...
end;