呵呵,我给你一个VCL组件,可以限制Ie的窗口数!unit IExplorerCounter;interfaceuses
Messages, ExtCtrls, Windows, SysUtils, Classes;type TIExplorer = class(TComponent)
private
FMaxWindow : Integer;
FOpenWindow : Integer;
WindowsList : Array of HWND;
Title : Array[0..512] of Char;
Timer : TTimer;
FEnabled : Boolean;
procedure SetEnabled(Value: Boolean);
procedure SetMaxWindow(Value: Integer);
procedure OnTimer (Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property OpenWindows : Integer read FOpenWindow default 0;
published
property MaxWindows : Integer read FMaxWindow write SetMaxWindow;
property Enabled : Boolean read FEnabled write SetEnabled;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Yalong', [TIExplorer]);
end;function EnumWindowsCallback(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
var
Explorer: TIExplorer;
begin
Explorer := TIExplorer(LParam); if GetWindowText(hwnd, Explorer.Title, SizeOf(Explorer.Title)) > 0 then
begin
if pos('microsoft internet explorer', lowercase(Explorer.Title)) > 0 then
begin
if Explorer.FOpenWindow >= Explorer.FMaxWindow then
begin { Kill Previous windows }
PostMessage(Explorer.WindowsList[0], WM_CLOSE,0,0); { Kill window } move(Explorer.WindowsList[1], Explorer.WindowsList[0], Explorer.FOpenWindow-1);
Explorer.WindowsList[Explorer.FOpenWindow-1] := hwnd;
end
else
begin
inc(Explorer.FOpenWindow);
Explorer.WindowsList[Explorer.FOpenWindow-1] := hwnd;
end;
end;
end;
Result := true;
if Explorer.FEnabled then
Explorer.Timer.Enabled := true;
end;procedure TIExplorer.OnTimer (Sender: TObject);
begin
Timer.Enabled := false;
FOpenWindow := 0;
EnumWindows(@EnumWindowsCallback, LPARAM(self));
end;constructor TIExplorer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMaxWindow := 2;
Setlength(WindowsList, FMaxWindow);
Timer := TTimer.Create(nil);
Timer.Enabled := false;
Timer.OnTimer := OnTimer;
Timer.Interval := 100;
end;destructor TIExplorer.Destroy;
begin
SetLength(WindowsList, 0);
inherited Destroy;
end;procedure TIExplorer.SetMaxWindow(Value: Integer);
begin
if Value <> FMaxWindow then
begin
SetLength(WindowsList, Value);
FMaxWindow := Value;
end;
end;procedure TIExplorer.SetEnabled(Value: Boolean);
begin
if FEnabled <> Value then
begin
FEnabled := Value;
Timer.Enabled := Value;
end;
end;
end.
Messages, ExtCtrls, Windows, SysUtils, Classes;type TIExplorer = class(TComponent)
private
FMaxWindow : Integer;
FOpenWindow : Integer;
WindowsList : Array of HWND;
Title : Array[0..512] of Char;
Timer : TTimer;
FEnabled : Boolean;
procedure SetEnabled(Value: Boolean);
procedure SetMaxWindow(Value: Integer);
procedure OnTimer (Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property OpenWindows : Integer read FOpenWindow default 0;
published
property MaxWindows : Integer read FMaxWindow write SetMaxWindow;
property Enabled : Boolean read FEnabled write SetEnabled;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Yalong', [TIExplorer]);
end;function EnumWindowsCallback(hwnd: HWND; lParam: LPARAM): BOOL; stdcall;
var
Explorer: TIExplorer;
begin
Explorer := TIExplorer(LParam); if GetWindowText(hwnd, Explorer.Title, SizeOf(Explorer.Title)) > 0 then
begin
if pos('microsoft internet explorer', lowercase(Explorer.Title)) > 0 then
begin
if Explorer.FOpenWindow >= Explorer.FMaxWindow then
begin { Kill Previous windows }
PostMessage(Explorer.WindowsList[0], WM_CLOSE,0,0); { Kill window } move(Explorer.WindowsList[1], Explorer.WindowsList[0], Explorer.FOpenWindow-1);
Explorer.WindowsList[Explorer.FOpenWindow-1] := hwnd;
end
else
begin
inc(Explorer.FOpenWindow);
Explorer.WindowsList[Explorer.FOpenWindow-1] := hwnd;
end;
end;
end;
Result := true;
if Explorer.FEnabled then
Explorer.Timer.Enabled := true;
end;procedure TIExplorer.OnTimer (Sender: TObject);
begin
Timer.Enabled := false;
FOpenWindow := 0;
EnumWindows(@EnumWindowsCallback, LPARAM(self));
end;constructor TIExplorer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FMaxWindow := 2;
Setlength(WindowsList, FMaxWindow);
Timer := TTimer.Create(nil);
Timer.Enabled := false;
Timer.OnTimer := OnTimer;
Timer.Interval := 100;
end;destructor TIExplorer.Destroy;
begin
SetLength(WindowsList, 0);
inherited Destroy;
end;procedure TIExplorer.SetMaxWindow(Value: Integer);
begin
if Value <> FMaxWindow then
begin
SetLength(WindowsList, Value);
FMaxWindow := Value;
end;
end;procedure TIExplorer.SetEnabled(Value: Boolean);
begin
if FEnabled <> Value then
begin
FEnabled := Value;
Timer.Enabled := Value;
end;
end;
end.
var
ZAppName: array[0..127] of char;
Hold: String;
Found: HWND;
begin
Hold := Application.Title;
Application.Title := 'OnlyOne'
+ IntToStr(HInstance); // 暂时修改窗口标题
StrPCopy(ZAppName, Hold); // 原窗口标题
Found := FindWindow(nil, ZAppName); // 查找窗口
Application.Title := Hold; // 恢复窗口标题
if Found<>0 then begin
// 若找到则激活已运行的程序并结束自身 ShowWindow(Found, SW_RESTORE);
Application.Terminate;
end;
end;