添加一个IFileDialogCustomize PushButton事件 [英] Add a IFileDialogCustomize PushButton Event

查看:82
本文介绍了添加一个IFileDialogCustomize PushButton事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用FileSaveDialog1.Dialog.QueryInterface创建一个按钮,如下所示.您如何设置和处理OnPushButton单击事件,以便我可以响应按钮单击?

I can create a Pushbutton with FileSaveDialog1.Dialog.QueryInterface as shown below. How do you setup and handle the OnPushButton click event so I can respond to the button click?

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;

推荐答案

以下内容在XE2中对我适用:

The following works fine for me in 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;

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;

这篇关于添加一个IFileDialogCustomize PushButton事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆