先注册一个热键,再所有窗口句柄都找出来,适合得把他提到前面。procedure TForm1.Button1Click(Sender: TObject); var myhandel : HWND; begin //setforegroundwindow myhandel := FindWindow(nil,'大富翁论坛'); //最简单的,也可以发消息处理。 if myhandel <> 0 then SetForegroundWindow(myhandel);end;
贴一段我以前写的:--------------------------- unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) ComboBox1: TComboBox; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm} function GetText(Wnd : HWND) : string; var textlength : integer; Text : string; begin textlength := GetWindowTextLength(Wnd); if textlength=0 then Result := '' else begin SetLength(Text,textlength+1); GetWindowText(Wnd,PChar(Text),textlength+1); Result:=string(Text); end; end;function EnumWindowsProc (Wnd: HWND; LParam: LPARAM): BOOL; stdcall; begin Result := True; if (IsWindowVisible(Wnd)) and (GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) and (GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then Form1.ComboBox1.Items.AddObject(GetText(Wnd),TObject(Wnd)); end;procedure TForm1.FormCreate(Sender: TObject); var Param : Longint; begin //枚举任务栏窗口 Param := 0; ComboBox1.Items.Clear; EnumWindows(@EnumWindowsProc , Param); end;procedure TForm1.Button1Click(Sender: TObject); var hWD : HWND; begin hWD := Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]); SetForegroundWindow(hWD); end;end.
var
myhandel : HWND;
begin
//setforegroundwindow
myhandel := FindWindow(nil,'大富翁论坛'); //最简单的,也可以发消息处理。
if myhandel <> 0 then
SetForegroundWindow(myhandel);end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function GetText(Wnd : HWND) : string;
var
textlength : integer;
Text : string;
begin
textlength := GetWindowTextLength(Wnd);
if textlength=0 then
Result := ''
else
begin
SetLength(Text,textlength+1);
GetWindowText(Wnd,PChar(Text),textlength+1);
Result:=string(Text);
end;
end;function EnumWindowsProc (Wnd: HWND; LParam: LPARAM): BOOL; stdcall;
begin
Result := True;
if (IsWindowVisible(Wnd)) and
(GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) and
(GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
Form1.ComboBox1.Items.AddObject(GetText(Wnd),TObject(Wnd));
end;procedure TForm1.FormCreate(Sender: TObject);
var
Param : Longint;
begin
//枚举任务栏窗口
Param := 0;
ComboBox1.Items.Clear;
EnumWindows(@EnumWindowsProc , Param);
end;procedure TForm1.Button1Click(Sender: TObject);
var
hWD : HWND;
begin
hWD := Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
SetForegroundWindow(hWD);
end;end.