如何捕获当前活动窗口的标题和handle? 如何捕获当前活动窗口的标题和handle?如:当我在使用notpad.exe时,我能捕获notpad.exe的句柄和标题(‘无标题-记事本’) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用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);取得某窗口的标题 procedure TForm1.Button2Click(Sender: TObject);var MyHWnd: HWND; MyCaption: Array[0..50] of char;begin MyH := GetActiveWindow; GetWindowText(MyH, MyCaption, 50); ShowMessage(MyCaption);end; 以下就是例子:将在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. 前面说错了GetActiveWindow取得的只是当前线程有关的活动窗口帮其他不能捕获其他应用程序的活动窗口句柄可以用GetForegroundWindow 在窗体上放一个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; 如何把TWebBrowser的Undo内容清除掉? 这里躺着一个普通的战士(转) delphi客户端和asp服务端 sql 自定义函数 求下载指定文件的完整程序代码 如何读取IE的版本号,密钥长度等信息? 在delphi中如何实现左移、右移m位阿? 怎样用Rave5使数据分组显示,并统计分组的数据? 自己写的两个exe程序,同时运行,如何传递内容? 有扬州的人码?请留下QQ号? Select * Into 语句? 新手高分求解,问题有解后再给500分;((Delphi6针对Corba开发的支持提供了VisiBroker3.3和VisiBroker4.0,我现在手头关于Corba的资料都是针
GetActiveWindow
取得当前活动窗口的句柄
使用
SendMessage(
(HWND) hWnd, // handle to destination window
WM_GETTEXT, // message to send
(WPARAM) wParam, // number of characters to copy
(LPARAM) lParam // text buffer
);
取得某窗口的标题
var
MyHWnd: HWND;
MyCaption: Array[0..50] of char;
begin
MyH := GetActiveWindow;
GetWindowText(MyH, MyCaption, 50);
ShowMessage(MyCaption);
end;
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.
GetActiveWindow
取得的只是当前线程有关的活动窗口
帮其他不能捕获其他应用程序的活动窗口句柄可以用
GetForegroundWindow
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;