建议的解决方案
keybd_event(VK_HOME, 0, 0, 0);
keybd_event(VK_HOME, 0, KEYEVENTF_KEYUP, 0);
不安全,无论何时执行。
如果我在应用程序中按 Ctrl+O 并在等待对话框显示时切换到其他应用程序怎么办?
然后这个其他应用程序将收到 HOME 键。那么任何事情都可能发生。另一个应用程序可能正在显示一个控制患者 IV 药物流速的轨迹栏,并且该 HOME 键可能会将轨迹栏设置为 0 cc/min。
更有可能:您丢失了资源管理器窗口中的选择(可能包含一千张图像)、文档中的插入符号位置(位于非常特殊的位置)、树视图中的选定节点等。或者您的媒体播放器重新启动当前轨道。
是的,很多人(比如我自己)确实以这种方式完成多项任务!
这是一个可能的(安全的)解决方案。不过,我并不认为它是最优雅的。
方法如下:
在创建对话窗口后,我使用该OnSelectionChange
事件有机会运行一些代码。但是,我必须手动确保仅在第一次触发此事件时才运行“文件名修复”。我还确保仅在(默认)文件名非空时运行此代码。
第一次触发事件时,我在其中找到了编辑框。我“知道”它是正确的控件,因为(1)它是一个编辑框,(2)它的文本等于(默认)文件名(不为空)。
然后我专门指示此编辑框将其插入符号移动到第一个字符,然后(重新)选择每个字符。
完整代码:
type
TOpenDialogFileNameEditData = class
FileName: string;
Handle: HWND;
end;
function EnumChildProc(h: HWND; lp: LPARAM): BOOL; stdcall;
var
WndClass, WndTxt: array[0..1024] of Char;
begin
Result := True;
FillChar(WndClass, SizeOf(WndClass), 0);
FillChar(WndTxt, SizeOf(WndTxt), 0);
if GetClassName(h, WndClass, Length(WndClass)) <> 0 then
begin
if SameText(WndClass, 'Edit') then
begin
if GetWindowText(h, WndTxt, Length(WndTxt)) <> 0 then
begin
if WndTxt = TOpenDialogFileNameEditData(lp).FileName then
begin
TOpenDialogFileNameEditData(lp).Handle := h;
Exit(False);
end;
end;
end;
end;
end;
procedure TForm1.FODE(Sender: TObject);
begin
if Sender is TOpenDialog then
begin
var OpenDialog := TOpenDialog(Sender);
if OpenDialog.Tag <> 0 then
Exit;
OpenDialog.Tag := 1;
var Data := TOpenDialogFileNameEditData.Create;
try
Data.FileName := ExtractFileName(OpenDialog.FileName);
if Data.FileName.IsEmpty then
Exit;
if OpenDialog.Handle <> 0 then
begin
EnumChildWindows(OpenDialog.Handle, @EnumChildProc, NativeInt(Data));
if Data.Handle <> 0 then
begin
SendMessage(Data.Handle, EM_SETSEL, 0, 0); // set caret at first char
SendMessage(Data.Handle, EM_SETSEL, 0, -1); // (re)select all
end;
end;
finally
Data.Free;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with TOpenDialog.Create(nil) do
try
FileName := 'This is the story of a horse that met a cat that met a dog the day before.txt';
OnSelectionChange := FODE;
Execute
finally
Free;
end;
end;
前:

后:

更新
根据要求,我制作了一个易于重复使用的单元和功能来应用此修复程序。
这是完整的单元:
unit OpenDialogUpgrader;
interface
uses
Windows, Messages, SysUtils, Types, Dialogs;
procedure FixOpenDialog(AOpenDialog: TOpenDialog);
implementation
type
TOpenDialogFileNameEditData = class
FileName: string;
Handle: HWND;
class procedure DialogSelectionChange(Sender: TObject);
end;
procedure FixOpenDialog(AOpenDialog: TOpenDialog);
begin
AOpenDialog.Tag := 0;
AOpenDialog.OnSelectionChange := TOpenDialogFileNameEditData.DialogSelectionChange;
end;
{ TOpenDialogFileNameEditData }
function EnumChildProc(h: HWND; lp: LPARAM): BOOL; stdcall;
var
WndClass, WndTxt: array[0..1024] of Char;
begin
Result := True;
FillChar(WndClass, SizeOf(WndClass), 0);
FillChar(WndTxt, SizeOf(WndTxt), 0);
if GetClassName(h, WndClass, Length(WndClass)) <> 0 then
begin
if SameText(WndClass, 'Edit') then
begin
if GetWindowText(h, WndTxt, Length(WndTxt)) <> 0 then
begin
if WndTxt = TOpenDialogFileNameEditData(lp).FileName then
begin
TOpenDialogFileNameEditData(lp).Handle := h;
Exit(False);
end;
end;
end;
end;
end;
class procedure TOpenDialogFileNameEditData.DialogSelectionChange(Sender: TObject);
begin
if Sender is TOpenDialog then
begin
var OpenDialog := TOpenDialog(Sender);
if OpenDialog.Tag <> 0 then
Exit;
OpenDialog.Tag := 1;
var Data := TOpenDialogFileNameEditData.Create;
try
Data.FileName := ExtractFileName(OpenDialog.FileName);
if Data.FileName.IsEmpty then
Exit;
if OpenDialog.Handle <> 0 then
begin
EnumChildWindows(OpenDialog.Handle, @EnumChildProc, NativeInt(Data));
if Data.Handle <> 0 then
begin
SendMessage(Data.Handle, EM_SETSEL, 0, 0); // set caret at first char
SendMessage(Data.Handle, EM_SETSEL, 0, -1); // (re)select all
end;
end;
finally
Data.Free;
end;
end;
end;
end.
要在您自己的单元中使用该单元和功能X
,只需将单元 ( OpenDialogUpgrader
) 添加到您X
单元的实施部分uses
条款并更改您的标准
var OpenDialog := TOpenDialog.Create(nil);
try
OpenDialog.FileName := 'This is the story of a horse that met a cat that met a dog the day before.txt';
OpenDialog.Execute;
finally
OpenDialog.Free;
end;
进入
var OpenDialog := TOpenDialog.Create(nil);
try
OpenDialog.FileName := 'This is the story of a horse that met a cat that met a dog the day before.txt';
FixOpenDialog(OpenDialog); // <-- just call this prior to Execute
OpenDialog.Execute;
finally
OpenDialog.Free;
end;
当然,如果您的应用程序在 20 个不同的单元中有 73 个打开的对话框,则您只需要该OpenDialogUpgrader
单元的一个副本,但您需要将其添加到uses
20 个单元中的每个单元的实现部分子句中。你需要FixOpenDialog
在每个TOpenDialog.Execute
.