工人线程中关闭窗体的实现

王朝delphi·作者佚名  2006-01-09
宽屏版  字体: |||超大  

在本人的《自动关闭信息提示窗体的实现》中实现了利用线程来关闭不需要的窗体技术,它所实现的是关闭同一程序中的窗体,而有时是需要关闭不在一个程序中的窗体的。比如,现在所见到的“广告杀手”(自动关闭广告窗体的一个程序)。

实现关闭窗体的关键在于找到该窗体的句柄,之后就可以发送WM_CLOSE给该窗体实现窗体的关闭。下面就是一个典型的例子用来关闭指定窗体标题以及窗体类名称的一个函数。

Procedure TForm1.Button1Click(Sender: TObject);

var

hCurrentWindow: HWnd;

szText: array[0..254] of char;

begin

hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);

while hCurrentWindow <> 0 do

begin

if GetWindowText(hCurrentWindow, @szText, 255)>0 then

begin

if StrPas(@szText))=’窗体标题’ then

if GetClassName(hCurrentWindow, @szText, 255)>0 then

if StrPas(@szText))=’窗体类名称’ then

break;

end;

hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);

end;

实际上有时需要在工人线程(MFC将线程分类为工人或者用户接口。主要的差别是用户接口线程可以接收消息,而工人线程不能。一般地,应该使用工人线程作为不需要用户干预的后台线程,比如电子表格的重新计算、打印操作或者拼写检查)中来实现这样的功能,而上面的程序则不能实现该功能,因为其中使用的Handle,该Handle也就是Self.Handle(窗体的句柄)。

如果能够找到一个有意义的窗口句柄来替代该Handle的话,就可以实现了。

查找Windwos帮助,相关的函数包括

HWND GetWindow(HWND hWnd, INT uCmd);获取一个窗体的相关窗体

HWND GetNextWindow(HWND hWnd, UINT wCmd);获取下一个窗体

HWND GetTopWindow(HWND hWnd);获取当前的顶窗体

但这三个函数都需要事先指定hWnd;

另一个函数HWND GetActiveWindow(VOID);返回的是线程相关的活动的窗体,但工人线程中没有窗体。

进一步查找可以发现HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName); 可以满足我们的要求。其中的参数lpClassName是指向类名称字符串的指针,而lpWindowName是指向窗体名称的指针,如果该参数为NULL,那么所有的窗体都匹配。

它关键的性质“而lpWindowName是指向窗体名称的指针,如果该参数为NULL,那么所有的窗体都匹配。”所以可以利用此特性,找到任何一个有意义的窗体句柄,在利用此句柄作为GetWindow的参数,最终查找到我们所需要的窗体句柄。具体实现如下所示:

unit UWindowKiller;

interface

uses

Classes, Windows, Messages, SysUtils, Dialogs;

type

WindowKiller = class(TThread)

protected

procedure Execute; override;

end;

implementation

var

aKiller: WindowKiller;

{ WindowKiller }

procedure WindowKiller.Execute;

var

Handle: THandle;

hCurrentWindow: HWnd;

szText: array[0..254] of char;

begin

Handle := FindWindow(nil, nil); // 先找到任意一个有意义的窗体句柄

hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);

while hCurrentWindow <> 0 do

begin

if GetWindowText(hCurrentWindow, @szText, 255)>0 then

begin

if StrPas(@szText)='我的电脑' then // 匹配窗体标题

break;

end;

hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);

end;

If hCurrentWindow<>0 then

PostMessage(hCurrentWindow, WM_CLOSE, 0, 0); //将窗体关闭

end;

initialization

aKiller := WindowKiller.Create(False); // 自动创建该线程,在程序启动时就执行

end.

结合上面的程序可以给出“广告杀手”的伪实现过程:

procedure WindowKiller.Execute;

var

i: integer;

str: string;

slWindow: TStrings;

Handle, hCurrentWindow: HWnd;

szText: array[0..254] of char;

begin

slWindow := TStringList.Create;

try

while not Terminated do

begin

slWindow.Clear;

Handle := FindWindow(nil, nil);

hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);

while hCurrentWindow <> 0 do

begin

if GetWindowText(hCurrentWindow, @szText, 255)>0 then

begin

str := StrPas(@szText);

if str 符合广告窗体标题的特征then

slWindow.Add(IntToStr(hCurrentWindow));

// 其他可能的根据窗体属性的判断

end;

hCurrentWindow:=GetWindow(hCurrentWindow, GW_HWNDNEXT);

end;

for i:=0 to slWindow.Count-1 do

PostMessage(HWnd(StrToInt(slWindow[i])), WM_CLOSE, 0, 0);

Sleep(1000);

end; // end-while

finally

slWindow.Free;

end;

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有