怎么列出当前桌面打开的所有窗体,找出它的窗口标题.句柄.(10分)

  • 主题发起人 主题发起人 易名烦
  • 开始时间 开始时间

易名烦

Unregistered / Unconfirmed
GUEST, unregistred user!
还有:全文检索怎么打不开?
 
好像是FindWindow函数
 
我只会在知道窗口标题时用FINDWINDOW查找窗口的句柄,怎么找出所有窗口呢?
 
定义Hwnd,使用一个循环
var
hCurWindow:HWnd;

hCurWindow:=GetWindow(Handle,GW_HWNDFIRST);
while hCurWindow <> 0 do
begin
//...GetWindowText(,,)
hCurWindow:=GetWindo(hCurWindow,GW_HWNDNEXT);
end
 
全文检索网址:http://www.richsearch.com
 
to tti:
我找到第二个就死循环了!
 
用EnumWindows这个吧
函数原型EnumWindows(WNDENUMPROC lpEnumFunc,LPARAM lParam);
lpEnumFunc:指向一个应用程序定义的回调函数指针
lParam:指定一个传递给回调函数的应用程序定义值

function FindWindowHandle (HuntFor: string): HWnd;
var
Proc: TFarProc;
HuntRec: PHuntRec;
begin
GetMem(HuntRec, SizeOf(THuntRec));
HuntRec^.HuntingFor := HuntFor;
HuntRec^.WindowFound := 0;
Proc := MakeProcInstance(@EnumWindowsFunc, HInstance);
EnumWindows(Proc, Longint(HuntRec));
FreeProcInstance(Proc);
FindWindowHandle := HuntRec^.WindowFound;
FreeMem(HuntRec, SizeOf(THuntRec));
end; {FindWindowHandle}
 
HuntFor: string 这个用来做什么?
几乎每条语句都不懂,给加个注释吧?
 
請參考《Delphi5開發人員指南>"dll"那一章的"回調函數",有完整的例子和解釋.
 
代碼如下:
DFM:
object MainForm: TMainForm
Left = 331
Top = 229
Width = 398
Height = 272
Caption = 'Delphi 6 Developers Guide Callback Demo'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object lbWinInfo: TListBox
Left = 12
Top = 28
Width = 364
Height = 161
Style = lbOwnerDrawFixed
ItemHeight = 13
TabOrder = 0
OnDrawItem = lbWinInfoDrawItem
end
object btnGetWinInfo: TButton
Left = 12
Top = 200
Width = 161
Height = 25
Caption = 'Get Window Info'
TabOrder = 1
OnClick = btnGetWinInfoClick
end
object hdWinInfo: THeaderControl
Left = 12
Top = 12
Width = 364
Height = 17
Align = alNone
DragReorder = False
Sections = <
item
ImageIndex = -1
Text = 'Window Name'
Width = 150
end
item
ImageIndex = -1
Text = 'Class Name'
Width = 150
end>
OnSectionResize = hdWinInfoSectionResize
end
end

Pas:

{ Copyright © 2001 Delphi 6 Developer's Guide Xavier Pacheco
and Steve Teixeira }

unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, ComCtrls;

type

{ Define a record/class to hold the window name and class name for
each window. Instances of this class will get added to ListBox1 }
TWindowInfo = class
WindowName, // The window name
WindowClass: String; // The window's class name
end;

TMainForm = class(TForm)
lbWinInfo: TListBox;
btnGetWinInfo: TButton;
hdWinInfo: THeaderControl;
procedure btnGetWinInfoClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lbWinInfoDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure hdWinInfoSectionResize(HeaderControl: THeaderControl;
Section: THeaderSection);
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}
function EnumWindowsProc(Hw: HWnd; AMainForm: TMainForm): Boolean; stdcall;
{ This procedure is called by the User32.DLL library as it enumerates
through windows active in the system. }
var
WinName, CName: array[0..144] of char;
WindowInfo: TWindowInfo;
begin
{ Return true by default which indicates not to stop enumerating
through the windows }
Result := True;
GetWindowText(Hw, WinName, 144); // Obtain the current window text
GetClassName(Hw, CName, 144); // Obtain the class name of the window
{ Create a TWindowInfo instance and set its fields with the values of
the window name and window class name. Then add this object to
ListBox1's Objects array. These values will be displayed later by
the listbox }
WindowInfo := TWindowInfo.Create;
with WindowInfo do
begin
SetLength(WindowName, strlen(WinName));
SetLength(WindowClass, StrLen(CName));
WindowName := StrPas(WinName);
WindowClass := StrPas(CName);
end;
MainForm.lbWinInfo.Items.AddObject('', WindowInfo); // Add to Objects array
end;

procedure TMainForm.btnGetWinInfoClick(Sender: TObject);
begin
{ Enumerate through all top-level windows being displayed. Pass in the
call back function EnumWindowsProc which will be called for each
window }
EnumWindows(@EnumWindowsProc, 0);
end;

procedure TMainForm.FormDestroy(Sender: TObject);
var
i: integer;
begin
{ Free all instances of TWindowInfo }
for i := 0 to lbWinInfo.Items.Count - 1 do
TWindowInfo(lbWinInfo.Items.Objects).Free
end;

procedure TMainForm.lbWinInfoDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
{ First, clear the rectangle to which drawing will be performed }
lbWinInfo.Canvas.FillRect(Rect);
{ Now draw the strings of the TWindowInfo record stored at the
Index'th position of the listbox. The sections of HeaderControl1
will give positions to which to draw each string }
with TWindowInfo(lbWinInfo.Items.Objects[Index]) do
begin
DrawText(lbWinInfo.Canvas.Handle, PChar(WindowName),
Length(WindowName), Rect,dt_Left or dt_VCenter);
{ Shift the drawing rectangle over by using the size
HeaderControl1's sections to determine where to draw the next
string }
Rect.Left := Rect.Left + hdWinInfo.Sections[0].Width;
DrawText(lbWinInfo.Canvas.Handle, PChar(WindowClass),
Length(WindowClass), Rect, dt_Left or dt_VCenter);
end;
end;

procedure TMainForm.hdWinInfoSectionResize(HeaderControl:
THeaderControl; Section: THeaderSection);
begin
lbWinInfo.Invalidate; // Force ListBox1 to redraw itself.
end;

end.

我貼的是D6書裏的例子.
 
英文的註釋,寫的很明白了.

建議購買<delphi6開發人員指南>, 118.
如果你在廣州的話,去華師后門五山路那裏有傢書店,75折.
 
不幸被你言中,我前天买了D6开发人员指南,一看,才印三千本,我好幸运。
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部