有时常需要查找当前有哪些窗口打开着,所以先用一种方法写下来:
效果:

//工程文件
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
// 单元文件
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function EnumWindowsProc(hwnd: HWND; lparam: Integer): Boolean ; stdcall;
var
Form1: TForm1;
slist: TStringList;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Clear;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
slist := TStringList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
slist.Free;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
SetWindowText(Handle,ListBox1.Items[ListBox1.ItemIndex]);
end;
function EnumWindowsProc(hwnd: HWND; lparam: Integer):Boolean;
var
buf: array[Byte] of char;
swnd: string;
begin
GetWindowText(hwnd, buf, sizeof(buf)); //取得窗口标题
swnd := Inttostr(hwnd);
slist.Add('窗口句柄:'+swnd +'---'+'标题:'+ buf);
Result := True; //重要
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumWindows(@EnumWindowsProc, 0);
ListBox1.Items.Assign(slist);
end;
end.
//窗体代码
object Form1: TForm1
Left = 0
Top = 0
BorderIcons = [biSystemMenu, biMinimize]
Caption = 'Form1'
ClientHeight = 295
ClientWidth = 448
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 8
Top = 8
Width = 121
Height = 18
AutoSize = False
Caption = #24403#21069#30340#39030#23618#31383#21475#26377#65306
end
object Button1: TButton
Left = 352
Top = 48
Width = 75
Height = 41
Caption = #26597#25214
TabOrder = 0
OnClick = Button1Click
end
object ListBox1: TListBox
Left = 8
Top = 32
Width = 298
Height = 257
ItemHeight = 13
TabOrder = 1
OnClick = ListBox1Click
end
object Button2: TButton
Left = 352
Top = 184
Width = 75
Height = 41
Caption = #28165#31354
TabOrder = 2
OnClick = Button2Click
end
end