如何通过鼠标单击将另一个应用程序的窗口句柄传递给Delphi [英] How do I get another application's window handle passed to Delphi via a mouse click

查看:293
本文介绍了如何通过鼠标单击将另一个应用程序的窗口句柄传递给Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何通过用户用鼠标选择该窗口(可以是任何其他应用程序的窗口)来获取要传递给Delphi的窗口的句柄.在我的Delphi应用程序中,我可以有一个用户单击的按钮来启动此检测过程,以及一个标签,该标签显示在Delphi应用程序中单击的窗口标题.当用户满意时,他可以选择正确的窗口,可以单击我的Delphi应用程序中的按钮(将是模态的)以停止选择过程,并让我的应用程序开始对其他窗口执行操作……

How can I get the handle of a window to be passed to Delphi by the user selecting the window (could be any other aplication's window) by clicking with the mouse on it. In my Delphi app I could have a button the user clicks that starts this detection process as well as a label displaying the clicked on window's title in the Delphi app. When the user is satisfied he selected the correct window he could click the button in my Delphi app (which will be modal) to stop the selection process and let my app start doing to the other window what it needs to do...

推荐答案

如果您知道窗口标题中包含什么文本,此代码将为您解决问题:

if you know what text is in the title of the window, this code will do the trick for you:

var
  WindowList: TList;

function GetHandle (windowtitle: string): HWND;
var
  h, TopWindow: HWND;
  Dest: array[0..80] of char;
  i: integer;
  s: string;

  function getWindows(Handle: HWND; Info: Pointer): BOOL; stdcall;
    begin
      Result:= True;
      WindowList.Add(Pointer(Handle));
    end;

begin
  result:= 0;

  try
    WindowList:= TList.Create;
    TopWindow:= Application.Handle;
    EnumWindows(@getWindows, Longint(@TopWindow));
    i:= 0;
    while (i < WindowList.Count) and (result = 0) do
      begin
        GetWindowText(HWND(WindowList[i]), Dest, sizeof(Dest) - 1);
        s:= dest;
        if length(s) > 0 then
          begin
            if (Pos(UpperCase(Windowtitle), UpperCase(s)) >= 1) then
              begin
                h:= HWND(WindowList[i]);
                if IsWindow(h) then
                  result:= h
             end
           end;
        inc(i)
      end
    finally
      WindowList.Free;
    end;
end;

示例中的用法(记事本将打开的文件的名称放在窗口标题中):

Usage in your example (notepad puts the name of the opened file in the window caption):

h:= getHandle('text.txt');
if (h = 0)
  // Oops not found
else 
  begin
    // you got the handle!
  end;

我使用此代码检查我的应用程序是否已经启动并正在运行.但是它可以在任何启动的应用程序上使用.

I used this code to check if my application was already up and running. But it can be used on any launched application.

这篇关于如何通过鼠标单击将另一个应用程序的窗口句柄传递给Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆