获取指定窗口控件句柄

Tody Guo 2008-08-27 02:24:50
希望能得到高手的指点:
我想得到form1里面其中一个按钮的文本,下面是我的代码,但是总是执行不成功。希望高手能指点小弟一下。谢谢!
代码下面的那个图片就是我测试的环境。


unit Unit1;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
function EnumChildWndProc(AhWnd: HWND; AlParam: lParam):Boolean;stdcall;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Hwnd: THandle;
CHwnd: THandle;
Buf: array [0..20] of char;
i: LongInt;
begin
Hwnd := FindWindow(nil,Pchar('Project1'));
Edit1.Text:=IntToStr(Hwnd);
Memo1.Lines.Clear;
Memo1.Lines.Add('List of Buttons:');

if Handle <> 0 then
begin
CHwnd:=FindWindowEx(Hwnd,0,nil,nil); // Cannot Find anything, Why???
GetWindowText(CHwnd,buf,199);
Memo1.Lines.Add(IntToStr(CHwnd)+'->'+Buf);

EnumChildWindows(Self.Handle,@EnumChildWndProc,0); // Find self form ok!!!
if EnumChildWindows(Hwnd,@EnumChildWndProc,0) = True then // Return False, Why???? ShowMessage('Load items ok')
else
ShowMessage('Unload...');
{ Sleep(1000);
for i:=100000 to 999999 do // To Find Control ID, but no use, why???
begin
CHwnd := GetDlgItem(Handle,i);
GetWindowText(CHwnd,buf,20);
Memo1.Lines.Add(IntToStr(CHwnd)+':'+Buf+IntToStr(i));
if buf<>'' then
ShowMessage('Found!');
Sleep(10);
end;
}
end
else
MessageBox(self.Handle,'Window not found!','Tips',0);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Application.Terminate;
end;

function EnumChildWndProc(AhWnd: HWND; AlParam: lParam):Boolean;stdcall;
var
WndClassName: Array[0..254] of Char;
WndCaption: Array[0..254] of Char;
begin
GetClassName(AhWnd,WndClassName,254);
GetWindowText(AhWnd,WndCaption,254);
With Form1.Memo1 Do
begin
Lines.Add(String(WndClassName));
Lines.Add(String(WndCaption));
Lines.Add('-------------------');
end;
Result:= True;
end;
end.


...全文
216 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiwenjun 2009-09-15
  • 打赏
  • 举报
回复
Tody Guo 2008-08-27
  • 打赏
  • 举报
回复

对不起,是我太粗心大意了。按照你的意思我调试出来了。真是太感谢你了。
lihuasoft 2008-08-27
  • 打赏
  • 举报
回复
大体看了一下,至少有一处失误:
Hwnd := FindWindow(nil,Pchar('Project1'));
//~~~~~~~~~~~应改为你要获取的窗体的标题:Form1

其他地方没仔细看
你先改一下这里试试能用不

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧