以编程方式按 ALT+TAB,直到活动字幕与 Delphi 中的给定字符串匹配

Programatically press ALT+TAB until the active caption matches a given string in Delphi

我正在尝试通过按 ALT+TAB 以编程方式 select 匹配标题中特定字符串的 windows。 知道我该怎么做吗?

我正在尝试为下面的代码找到一个替代解决方案,它有一个奇怪的缺点:外部应用程序的最小化按钮不起作用,除非我在任务栏上单击两次鼠标左键,其中形式是。我创建的记事本、Mozilla 和其他 delphi 应用程序不显示此行为。受此行为影响的唯一程序是我试图通过向其添加额外功能来控制的商业软件。

function FindWindowExtd(partialTitle: string): HWND;
var
  hWndTemp: hWnd;
  iLenText: Integer;
  cTitletemp: array [0..254] of Char;
  sTitleTemp: string;
begin
  hWndTemp := FindWindow(nil, nil);
  while hWndTemp <> 0 do begin
    iLenText := GetWindowText(hWndTemp, cTitletemp, 255);
    sTitleTemp := cTitletemp;
    sTitleTemp := UpperCase(copy( sTitleTemp, 1, iLenText));
    partialTitle := UpperCase(partialTitle);
    if pos( partialTitle, sTitleTemp ) <> 0 then
      Break;
    hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
  end;
  result := hWndTemp;
end;



 procedure Tform2.OnSchedule_1_Trigger(Sender: TScheduledEvent);
    var h:HWND;
        executabil:string;

    begin
    h:=FindWindowExtd(Edit_titlu_fereastra_program.text);
    if (h = 0) then
    begin
    // Oops not found
showmessage('THE WINDOW WAS NOT FOUND');

    end
    else
      begin
    // you got the handle!
    if iswindow(h) then begin

    showmessage('THE WINDOW WAS FOUND');
    ShowWindow(h,SW_MAXIMIZE);
    SetForegroundWindow(h);

      end;
      end;

非常感谢!

您可以从 Windows API 调用 FindWindow 函数来找到具有所需标题的 window ,然后根据需要将其置于最前面。

像这样

procedure Keybd_Press   (const AKey : byte);
begin
  Keybd_Event (AKey,0,0,0);
end;

procedure Keybd_Release (const AKey : byte);
begin
  Keybd_Event (AKey,0,KEYEVENTF_KEYUP,0);
end;

Keybd_Press (VK_MENU);
Keybd_Press (VK_TAB);
Keybd_Release (VK_TAB);
Keybd_Release (VK_MENU);