本文整理自网络,侵删。
//网上的思路
procedure TForm1.Timer1Timer(Sender: TObject);
var
hwnd,hwnd1,hwnd2,hwnd3:Thandle;
begin
hwnd:=Getforegroundwindow;
hwnd1:=findwindowEx(hwnd,0,pchar('AfxMDIFrame42s'),nil);
hwnd2:=findwindowEx(hwnd,0,pchar('AfxControlBar42s'),nil);
if hwnd1 <> 0 then
if hwnd2 <>0 then
begin
PostMessage(hwnd,WM_CLOSE,0,0);
timer2.Enabled :=true;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
keybd_event(vk_return,MapVirtualKey(vk_return, 0 ),0,0);
keybd_event(vk_return, MapVirtualKey(vk_return, 0 ), KEYEVENTF_KEYUP , 0 );
end;
==========我的实现=======================
procedure TForm1.Timer1Timer(Sender: TObject);
var
hwnd,hwnd1,hwnd2,hwnd3:Thandle;
begin
timer1.Enabled :=false;
//hwnd:=Getforegroundwindow; //查找当前窗口
hwnd:=GetWindow(Handle,GW_HWNDFIRST);
while hwnd<>0 do
begin
hwnd1:=findwindowEx(hwnd,0,pchar('AfxMDIFrame42s'),nil);
hwnd2:=findwindowEx(hwnd,0,pchar('AfxControlBar42s'),nil);
if hwnd1 <> 0 then
if hwnd2 <>0 then
begin
PostMessage(hwnd,WM_CLOSE,0,0);
hwnd3:=GetFocus;
if hwnd3<> hwnd then
setforegroundwindow(hwnd); //设为激活窗口
//PostMessage(hwnd3, WM_KEYDOWN, VK_RETURN, 0);
keybd_event(vk_return,MapVirtualKey(vk_return, 0 ),0,0);
keybd_event(vk_return, MapVirtualKey(vk_return, 0 ), KEYEVENTF_KEYUP , 0 );
break;
end;
hwnd:=GetWindow(hwnd,GW_HWNDNEXT);
end;
timer1.Enabled :=true;
end;
//////////////////////////////稍微改动////////////////////////////////////////////////////////
//得到指定窗体的焦点子窗体的句柄
function GetActiveControl(ParentWnd: HWND): HWND;
var
OtherThread,
Buffer: DWord;
idCurrThread: DWord;
begin
OtherThread:= GetWindowThreadProcessID(ParentWnd, @Buffer);
idCurrThread:= GetCurrentThreadID;
if AttachThreadInput(idCurrThread, OtherThread, True) then
begin
Result:= GetFocus;
AttachThreadInput(idCurrThread, OtherThread, False);
end
else
Result:= GetFocus;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
hwnd,hwnd1,hwnd2,hwnd3:Thandle;
begin
timer1.Enabled :=false;
hwnd:=GetWindow(Handle,GW_HWNDFIRST);
while hwnd<>0 do
begin
hwnd1:=findwindowEx(hwnd,0,pchar('AfxMDIFrame42s'),nil);
hwnd2:=findwindowEx(hwnd,0,pchar('AfxControlBar42s'),nil);
if hwnd1 <> 0 then
if hwnd2 <>0 then
begin
PostMessage(hwnd,WM_CLOSE,0,0);
hwnd3:=GetActiveControl(hwnd);//得到该窗体的焦点子窗体句柄
PostMessage(hwnd3, WM_KEYDOWN, VK_RETURN, 0);
break;
end;
hwnd:=GetWindow(hwnd,GW_HWNDNEXT);
end;
timer1.Enabled :=true;
end;
相关阅读 >>
Delphi 10.2 新特性之―tfdbatchmovejsonwriter
Delphi Delphi tparallel cleanup needed用法
更多相关阅读请进入《Delphi》频道 >>