gpt4 book ai didi

delphi - 返回使用 IFileDialogCustomize.EnableOpenDropDown 时选择的 DropDown 项

转载 作者:行者123 更新时间:2023-12-01 20:20:56 24 4
gpt4 key购买 nike

我正在寻找一个公共(public)文件打开对话框,其中“打开”按钮上有“打开”和“打开只读”选项。

我已经按照...中的示例

Add a IFileDialogCustomize PushButton Event

(下面复制的大部分代码来自 Remy Lebeau 的回答)

...以及 MSDN 信息...

http://msdn.microsoft.com/en-us/library/windows/desktop/bb776913(v=vs.85).aspx#OnFileOk

该对话框可以工作,但我找不到如何确定打开用户选择的下拉列表

    (* this is skeleton implementation of the interfaces *)
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;

implementation

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;
end.

我在每个方法上都设置了断点,一旦用户选择“以只读方式打开”,我除了 FileOpenOK 方法调用之外什么也看不到。

    (* This is the test code hooking up the dialog *)
procedure TForm1.Button1Click(Sender: TObject);
var Ok: Boolean;
begin
FileDialog := nil;
MyEvents := nil;
MyEventsCookie := 0;
try
Ok := FileOpenDialog1.Execute;
finally
if (FileDialog <> nil) and (MyEventsCookie <> 0)
then FileDialog.Unadvise(MyEventsCookie);
FileDialog := nil;
MyEvents := nil;
MyEventsCookie := 0;
end;
if OK
then ; // open the file
end;

procedure TForm1.FileOpenDialog1Execute(Sender: TObject);
var c: IFileDialogCustomize;
d: IFileDialogEvents;
cookie: DWORD;
begin
if Supports(FileOpenDialog1.Dialog, IFileDialogCustomize, c)
then begin
c.EnableOpenDropDown(0);
c.AddControlItem(0,0,'&Open');
c.AddControlItem(0,1,'Open &read-only');
d:= TMyFileDialogEvents.Create;
if Succeeded(FileOpenDialog1.Dialog.Advise(d, cookie))
then begin
FileDialog := FileOpenDialog1.Dialog;
MyEvents := d;
MyEventsCookie := cookie;
end;
end;
end;

特别喜欢(不)的是MSDN示例如何建议使用与ComboBox相同的机制,并且在ComboBox示例中将该代码排除在外 “完整”示例包括 EnableOpenDropDown! ;)

最佳答案

查看 EnableOpenDropDown 的原型(prototype):

HRESULT EnableOpenDropDown(
[in] DWORD dwIDCtl
);

传入的参数是下拉按钮的ID。然后,您可以在添加项目时重新使用该 ID:

HRESULT AddControlItem(
[in] DWORD dwIDCtl,
[in] DWORD dwIDItem,
[in] LPCWSTR pszLabel
);

因此,就您而言,当您调用 EnableOpenDropDown 时,您为 dwIDCtl 传递了 0。然后,在添加这两个项目时,您可以使用与 AddControlItem 的第一个参数相同的值。这两个项目的 ID 分别为 01

要了解对话框关闭时选择的是哪一个,请调用 GetSelectedControlItem

HRESULT GetSelectedControlItem(
[in] DWORD dwIDCtl,
[out] DWORD *pdwIDItem
);

您需要为 dwIDCtl 传递 0 来识别您的下拉菜单。选定的控制项通过 pdwIDItem 返回。

假设您使用 Winapi.ShlObj 中的 IFileDialogCustomize 声明,那么您的代码将如下所示:

var
fdc: IFileDialogCustomize;
dwIDItem: DWORD;
....
// this code executes after the call to IFileDialog.Show returns
OleCheck(fdc.GetSelectedControlItem(0, dwIDItem));
// dwIDItem will be 0 for Open and 1 for Open read-only

您可能需要考虑为这些 ID 声明一些常量,而不是依赖于魔法文字值。

如果您需要在显示对话框之前设置选定的下拉项,请使用SetSelectedControlItem

关于delphi - 返回使用 IFileDialogCustomize.EnableOpenDropDown 时选择的 DropDown 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19766427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com