枚举窗口 EnumWindows 如何用。(20分)

  • 主题发起人 主题发起人 QSmile
  • 开始时间 开始时间
Q

QSmile

Unregistered / Unconfirmed
GUEST, unregistred user!
枚举窗口 EnumWindows 如何用。<br>它是返回所有顶级窗口还是当前窗口的子窗口。<br>我这样用<br><br>function EnumWindowProc(Handle :THandle;value :DWORD)BOOL;<br><br>.....<br>EnumWindows(@EnumWindowProc,0);<br><br>.....<br>function EnumWindowProc(Handle: Thandle; Value: DWORD): BOOL;<br>var<br>&nbsp; cBuf:array of char;<br>&nbsp; Len :Longint;<br>begin<br>&nbsp; Form1.Memo1.Lines.Add(IntToHex(Handle,8));<br><br>&nbsp; Len := GetWindowTextLength(Handle);<br>&nbsp; SetLength(cBuf,Len+1);<br>&nbsp; ZeroMemory(@cBuf[0],Len+1);<br>&nbsp; GetWindowText(Handle,@cBuf[0],Len);<br>&nbsp; Form1.Memo1.Lines.Add(String(cBuf));<br>&nbsp; SetLength(cBuf,0);<br>&nbsp; Result := true;<br>end;<br><br>可以得到一些句柄。但得不到窗口名称。窗口名称都是空的。<br>用 GetWindow 又得到一大堆窗口名称<br>什么 Define IME 有很多个。有很多是重复的。<br>有没有办法只枚举用 ALT + TAB 看到的窗口?<br>
 
BOOL EnumWindows( <br>WNDENUMPROC lpEnumFunc, <br>LPARAM lParam ); <br>lParam 这里你不能填0,要填相应值,查一下msdn
 
lParam<br>Specifies a 32-bit, application-defined value to be passed to the callback function. <br>以上是用帮助里COPY的。这个值是传给 回调函数据的。<br>也就是 EnumWindowProc(Handle: Thandle; Value: DWORD): 中的 Value<br>我也试过,用其它值也不行。<br>谁用过?
 
这个值是什么没关系的,一般是这样用的<br>function EnumWindowsProc(hwnd: HWND, lParam: LPARAM): BOOL; stdcall;<br>begin<br>&nbsp; with TForm1(lParam) do<br>&nbsp; begin<br>&nbsp; &nbsp; if IsWindowVisible(hwnd) then &nbsp;//如果窗口是可见的<br>&nbsp; &nbsp; &nbsp;...<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(SEnder: TObject);<br>begin<br>&nbsp; EnumWindows(@EnumWindowProc,Integer(Self));<br>end;
 
function EnumWindowProc(Handle: Thandle; Value: DWORD): BOOL;<br>var<br>&nbsp; cBuf:array of char;<br>&nbsp; Len :Longint;<br>begin<br><br>&nbsp; if IsWindowVisible(Handle) then<br>&nbsp; begin<br>&nbsp; &nbsp; Form1.Memo1.Lines.Add(IntToHex(Handle,8));<br>&nbsp; &nbsp; Len := GetWindowTextLength(Handle);<br>&nbsp; &nbsp; SetLength(cBuf,Len+1);<br>&nbsp; &nbsp; ZeroMemory(@cBuf[0],Len+1);<br>&nbsp; &nbsp; GetWindowText(Handle,@cBuf[0],Len);<br>&nbsp; &nbsp; Form1.Memo1.Lines.Add(String(cBuf));<br>&nbsp; &nbsp; SetLength(cBuf,0);<br>&nbsp; end;<br>&nbsp; Result := true;<br>end;<br><br><br>EnumWindows(@EnumWindowProc,Integer(Self));<br>我这样写返回的值一个都没有。所有的窗口都是不可见的。<br>谁帮我看看。<br>
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp; function EnumWndProc(AWnd:HWND;AlParam:LPARAM):Boolean;stdcall;<br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>function EnumWndProc(AWnd: HWND; AlParam: LPARAM):Boolean;stdcall;<br>var<br>&nbsp; WndCaption: array[0..254] of Char;<br>begin<br>&nbsp; if IsWindowVisible(AWnd) then<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowText(AWnd, @WndCaption, 254);<br><br>&nbsp; &nbsp; if WndCaption[0]&lt;&gt;chr(0) then<br>&nbsp; &nbsp; &nbsp; Form1.ListBox1.Items.Add(Format('%d &nbsp;= &nbsp;%s',[AWnd,StrPas(WndCaption)]));<br>&nbsp; end;<br><br>&nbsp; Result := True;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; EnumWindows(@EnumWndProc,0);<br>end;<br><br>end.
 
to : angelgekko<br>&nbsp; &nbsp;我用了你给的方法还是不行。<br>&nbsp; &nbsp;我想不出错在哪里。<br>&nbsp; &nbsp;但用 GetWindow 就可以了。<br>&nbsp; 我用的是 Win2000 是不是这个函数在 2000 下有BUG
 
上面的方法没错,是你写错了<br>function EnumWindowProc(Handle: Thandle; Value: DWORD): BOOL; [red]stdcall[/red];
 
&gt;&gt;有没有办法只枚举用 ALT + TAB 看到的窗口?<br><br>我用以上程序,可以得到以下结果。(D6 + Win2000)<br>985212 &nbsp;= &nbsp;Form1<br>1050718 &nbsp;= &nbsp;Project1<br>590776 &nbsp;= &nbsp;Unit1.pas<br>197602 &nbsp;= &nbsp;Delphi 6 - Project1 [Running]<br>263108 &nbsp;= &nbsp;Delphi 6<br>395168 &nbsp;= &nbsp;Microsoft Visio<br>197626 &nbsp;= &nbsp;UltraEdit-32<br>721182 &nbsp;= &nbsp;C:/Program Files/Borland/Delphi6/Projects<br>67730 &nbsp;= &nbsp;C:/<br>65784 &nbsp;= &nbsp;Program Manager
 
后退
顶部