一个窗体form1,form1上面有2个panel,panel都没有名称,第二个panle上面有个按钮button1,怎样找到button1的句柄呢?
h1:=FindWindow(NIL,'Form1);
h1:=FindWindowEx(h1,0,nil,'');//这边没有名称 这边要怎么改呢?
h_button1:=FindWindowEx(h1,0,nil,'button1');
h1:=FindWindow(NIL,'Form1);
h1:=FindWindowEx(h1,0,nil,'');//这边没有名称 这边要怎么改呢?
h_button1:=FindWindowEx(h1,0,nil,'button1');
如果是name属性为空的话,那么是否你的窗口上面还有别的panel存在,而且name不为空?
我问这个其实有点离题,主要是因为,一个form上的任一类型控件必须要有一个name属性不为空才可能编译通过,否则会报错的。所以做了一个猜测。
至于你的问题,我滴回答是:可以获取。
下面是一个通过获取鼠标位置,获取该控件句柄的代码。你可以借鉴一下。
procedure TForm1.Timer1Timer(Sender: TObject);
var
Pos: TPoint;
Handle: HWND;
ScreenDC: HDC;
Buf: array[0..1024] of Char;
ScreenColor: COLORREF;
begin
GetCursorPos(Pos);
Handle := WindowFromPoint(Pos); // 返回当前位置的句柄
HandleText.Caption := IntToStr(Handle);
GetClassName(Handle, Buf, 1024); // 得到类名
ClassNameText.Caption := Buf;
SendMessage(Handle, WM_GETTEXT, 33, Integer(@Buf)); // 得到标题
TitleText.Caption := Buf;
end;记得放timer控件,嘿嘿。
或
h1:=FindWindowEx(h1,0,'TButton',nil);没有名称可以按类名来查。如果上述代码不行,建议你先用spy++查看一下按扭的类名,把类名填上去。
var
h1: Windows.HWND;
h_button1: Windows.HWND;
begin
h1:=FindWindow(NIL,'标题');
h1:=FindWindowEx(h1,0,'TPanel',nil);
h_button1:=FindWindowEx(h1,0,nil,'Button1');
if h_button1<>0 then
ShowMessage('找到');
end;
hChild:HWND;
buffer:array[1..200] of char;
begin
hMain:=FindWindow(nil, '标题');
hChild:=FindWindowEx(hMain,0,nil,nil);
while hChild<>0 do
begin
GetWIndowText(hChild,@buffer,200);
ListBox1.Items.Add(StrPas(@Buffer));
hChild:=FindWindowEx(hMain,hChild,nil,nil);//look for next control
end;
end;
是cation属性为空
spy++好像不能看到name属性
for i := 0 to self.ScrollBoxInfo.ComponentCount - 1 do
begin
if self.ScrollBoxInfo.Components[i] is TCheckBox then
begin
if TCheckBox(self.ScrollBoxInfo.Components[i]).Checked then
begin
MoveUnit.strInfo.Add(TCheckBox(self.ScrollBoxInfo.Components[i]).Caption);
if TCheckBox(self.ScrollBoxInfo.Components[i]).Color = clTeal then
MoveUnit.strInfoEnd.Add(TCheckBox(self.ScrollBoxInfo.Components[i]).Caption)
else
MoveUnit.strInfoEnd.Add('gb');
end
else
begin
MoveUnit.strInfo.Add('null');
MoveUnit.strInfoEnd.Add('gb');
end; end;
end;自己原来的代码 没修改
var
i, k: Integer;
Panel: TPanel;
begin
for i := 0 to self.ControlCount - 1 do
begin
if Self.Controls[i] is TPanel then
begin
Panel := TPanel(Self.Controls[i]);
for k := 0 to Panel.ControlCount - 1 do
begin
if Panel.Controls[i] is TButton then
begin
if TButton(Panel.Controls[k]).Name = 'Button1' then
begin
//找到了
TButton(Panel.Controls[k]).Caption := '找到了';
end;
end;
end;
end;
end;
end;