gpt4 book ai didi

delphi - 尝试在Delphi XE6中发送 key 失败

转载 作者:行者123 更新时间:2023-12-03 19:41:07 26 4
gpt4 key购买 nike

以下是我用来将Ctrl + Shift + S键发送到PDF文档的完整例程。它应该显示保存对话框,但不能这样做。

该过程使用GetFiles打开sFolder中的pdf文档。 sFolder中只有一个pdf文档。

从注释行中可以看到,我也尝试了sndkey32而不成功。

procedure TForm1.Button1Click(Sender: TObject);
var
oBrowser: TBrowseForFolder;
oList: TStringDynArray;
sFile: string;
sFolder: string;
oShellExecuteInfo: TShellExecuteInfo;
begin
oBrowser := TBrowseForFolder.Create(self);
oBrowser.Execute;
sFolder := oBrowser.Folder;
oBrowser.Free;
if DirectoryExists(sFolder) then begin
oList := TDirectory.GetFiles(sFolder, '*.pdf', TSearchOption.soAllDirectories);
if Length(oList) > 0 then begin
for sFile in oList do begin
FillChar(oShellExecuteInfo, SizeOf(oShellExecuteInfo), 0);
oShellExecuteInfo.cbSize := SizeOf(TShellExecuteInfo);
with oShellExecuteInfo do begin
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := Application.Handle;
lpFile := PChar(sFile);
nShow := SW_SHOWNORMAL;
end;
if ShellExecuteEx(@oShellExecuteInfo) then begin
ShowWindow(oShellExecuteInfo.Wnd, 1);
SetForegroundWindow(oShellExecuteInfo.Wnd);
Winapi.Windows.SetFocus(oShellExecuteInfo.Wnd);
SendKey(Ord('s'), [ssCtrl, ssShift], False);
// if sndkey32.AppActivate('adobe') then
// sndkey32.SendKeys('^+S', False);
end;
end;
end;
end;
end;

procedure TForm1.SendKey(key: Word; const shift: TShiftState; specialkey: Boolean);
type
TShiftKeyInfo = record
shift: Byte;
vkey: Byte;
end;
ByteSet = set of 0 .. 7;
const
shiftkeys: array [1 .. 3] of TShiftKeyInfo = ((shift: Ord(ssCtrl); vkey: VK_CONTROL), (shift: Ord(ssShift); vkey: VK_SHIFT), (shift: Ord(ssAlt); vkey: VK_MENU));
var
flag: DWORD;
bShift: ByteSet absolute shift;
j: Integer;
begin
for j := 1 to 3 do begin
if shiftkeys[j].shift in bShift then keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0);
end;
if specialkey then flag := KEYEVENTF_EXTENDEDKEY
else flag := 0;
keybd_event(key, MapVirtualKey(key, 0), flag, 0);
flag := flag or KEYEVENTF_KEYUP;
keybd_event(key, MapVirtualKey(key, 0), flag, 0);
for j := 3 downto 1 do begin
if shiftkeys[j].shift in bShift then keybd_event(shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), KEYEVENTF_KEYUP, 0);
end;
end;

最佳答案

oShellExecuteInfo.Wnd窗口是Delphi进程中的一个窗口。您将其分配为Application.Handle。您似乎希望它将成为PDF查看器的主窗口,但事实并非如此。

因此,您需要找到PDF查看器的主窗口。这涉及调用EnumerateWindows以获取所有顶级窗口。然后,对于每一个,使用GetWindowThreadProcessId来测试该窗口是否归PDF查看器进程所有。

其他一些评论:


您在调用API函数时忽略了错误检查。
您应该使用SendInput而不是keybd_event
您泄漏了ShellExecuteEx返回的进程句柄。
ShellExecuteEx可能根本不返回任何进程句柄。这取决于如何设置文件关联以及Acrobat是否已在运行。
您可能需要等到新进程完成启动后才能发送输入。
您的程序似乎假定已安装的PDF查看器为Acrobat。如果不是,那该怎么办?

关于delphi - 尝试在Delphi XE6中发送 key 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23589955/

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