如何捕获当前活动窗口的标题和handle?
如:当我在使用notpad.exe时,
我能捕获notpad.exe的句柄和标题(‘无标题-记事本’)

解决方案 »

  1.   

    使用API函数
    GetActiveWindow
    取得当前活动窗口的句柄
    使用
    SendMessage( 
      (HWND) hWnd,              // handle to destination window 
      WM_GETTEXT,               // message to send
      (WPARAM) wParam,          // number of characters to copy
      (LPARAM) lParam           // text buffer
    );
    取得某窗口的标题
      

  2.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      MyHWnd: HWND;
      MyCaption: Array[0..50] of char;
    begin
      MyH := GetActiveWindow;
      GetWindowText(MyH, MyCaption, 50);
      ShowMessage(MyCaption);
    end;
      

  3.   

    以下就是例子:将在MEMO1中取得当前所有窗口的标题unit titletet;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      hcurwindow:hwnd;
      wintext:array[0..255]of char;
    begin
      hcurwindow:=getwindow(handle,GW_HWNDFIRST);
      while hcurwindow<>0 do
      begin
        if getwindowtext(hcurwindow,@wintext,255)>0 then
          memo1.Lines.Add(strpas(@wintext));
        hcurwindow:=getwindow(hcurwindow,GW_HWNDNEXT);
      end;
    end;end.
      

  4.   

    前面说错了
    GetActiveWindow
    取得的只是当前线程有关的活动窗口
    帮其他不能捕获其他应用程序的活动窗口句柄可以用
    GetForegroundWindow
      

  5.   

    在窗体上放一个Timer,Interval为3000,写OnTimer事件,就可得到你想要的结果:
    procedure TForm1.Timer1Timer(Sender: TObject);
    var
      H:HWND;
      pTitle:PChar;
    begin
      H:=GetForegroundWindow;;
      GetMem(pTitle,128);
      try
        GetWindowText(H,pTitle,128);
        MessageBox(H,pTitle,'当前窗口为:',0);
      finally
        FreeMem(pTitle);
      end;
    end;
    procedure TForm1.Timer1Timer(Sender: TObject);
    var
      H:HWND;
      pTitle:PChar;
    begin
      H:=GetForegroundWindow;;
      GetMem(pTitle,128);
      try
        GetWindowText(H,pTitle,128);
        MessageBox(H,pTitle,'当前窗口为:',0);
      finally
        FreeMem(pTitle);
      end;
    end;