简体   繁体   English

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

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

I'm trying to programmatically select a windows that matches a specific string in caption by pressing ALT+TAB . 我试图通过按ALT+TAB来以编程方式选择与标题中的特定字符串匹配的窗口。 Any idea how can I do that? 知道我该怎么做吗?

I'm trying to find an alternate solution to the code below which has a strange downside: the minimize button of the external application does not work unless I click twice the left mouse button on the taskbar where the rectangle of the form is. 我正在尝试找到下面代码的替代解决方案,该解决方案有一个奇怪的缺点:除非我在窗体的矩形所在的任务栏上单击两次鼠标左键,否则外部应用程序的“最小化”按钮将不起作用。 Notepad, Mozilla and other delphi application created by me do not display this behavior. 我创建的记事本,Mozilla和其他delphi应用程序不显示此行为。 The only program that is affected by this behavior is a commercial software that I'm trying to control by adding extra functions to it. 受此行为影响的唯一程序是我试图通过向其添加额外功能来控制的商业软件。

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;

Thanks a lot! 非常感谢!

您可以从Windows API调用FindWindow函数来查找具有所需标题的窗口,然后根据需要将其放在最前面。

Like this 像这样

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM