如何定位给定主窗体标题的窗口里的子控件?急急急急急!!!!!

risingsoft 2004-04-20 02:26:14
各位大虾:

我想从给定标题的窗口里,定位给定标题的一个标签按钮。如何实现,我的参考程序代码如下。针对我的程序代码,本人有点迷茫,请各位大虾指点迷途:

procedure TForm1.Button2Click(Sender: TObject);
var
win_cap, //待查找的主窗体标题
btn_cap: string; //待查找的标签标题

hd, //待查找的主窗体句柄
hdl: hwnd; //遍历主窗体各子控件句柄

ctl_text: array[0..254] of char; //各子控件标题
begin
win_cap := 'flash 作品欣赏 - Microsoft Internet Explorer';
btn_cap := '投我一票';

//获取主窗体句柄
hd := FindWindow(nil,pchar(win_cap));

//获取成功则提取主窗体第一子控件窗口句柄
if hd >0 then
hdl := getWindow(hd,GW_CHILD+GW_HWNDFIRST);

//遍历主窗体各子控件
while hdl > 0 do
begin
//获取子控件标题
if GetWindowText(hdl, @ctl_text, 255)>0 then
begin
//与待定位按钮标题比较,一致则查找成功
if pos(btn_cap,StrPas(@ctl_text))>0 then
showmessage('哈哈,就是你...');
end;
hdl := getwindow(hdl,GW_HWNDNEXT);
end;

showMessage('结束了...');
end;

1、为何GetWindowText总是返回一个空字符串,无任何信息
2、如何正确定位到该控件,如果有懂的,一定要将程序代码调试无误,本人对分绝不吝啬!

谢谢大家!~
...全文
70 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
楚人无衣 2004-04-21
  • 打赏
  • 举报
回复
测试过,wb.OleObject.Document.Forms.Item(0).Submit;
要换成ov := wb.OleObject.document.all.item('submsg', 0); {按名找到那个发送按钮}
ov.Click; {这样表单就submit了,用上面的.Submit好象不是想要的结果}
楚人无衣 2004-04-20
  • 打赏
  • 举报
回复
改进楼上的,楼主是用来在网页灌水的吧 :P
for i := 0 to (ovElements.Length - 1) do
begin
if (ovElements.Item(i).tagName = 'INPUT') and (ovElements.Item(i).type = 'radio') and
(ovElements.Item(i).Value = 'c') then
begin
ovElements.item(i).Checked := true;
wb.OleObject.Document.Forms.Item(0).Submit;
end;
楚人无衣 2004-04-20
  • 打赏
  • 举报
回复
//用一个TWebBrowser,装那个网页进去
procedure TForm1.Button1Click(Sender: TObject);
var
ov : OleVariant;
begin
ov := wb.OleObject.Document.All.Item('btnVote', 0); //找到那个按钮
ov.OnClick;
end;
aiirii 2004-04-20
  • 打赏
  • 举报
回复
早說啊, 根本不是一回事
用 WebBrowser1 來控制

procedure TForm1.Button1Click(Sender: TObject);
var
ovElements: OleVariant;
i: Integer;
begin
ovElements := WebBrowser1.OleObject.Document.forms.item(0).elements;
for i := 0 to (ovElements.Length - 1) do
if (ovElements.item(i).tagName = 'INPUT') and
(ovElements.item(i).type = 'SUBMIT') and
(ovElements.item(i).Value = 'Recent Charges') then
ovElements.item(i).Click;
end;
risingsoft 2004-04-20
  • 打赏
  • 举报
回复
自己顶~
risingsoft 2004-04-20
  • 打赏
  • 举报
回复
你的好象不行。

算了,告诉我全部思路。

1、打开IE,URL为http://flash.163.com/show_one.php?idno=75925

2、查找该窗体里的“投我一票”按钮,模拟鼠标点击它一次

3、将电击后新打开的窗体关闭

如何实现这种功能,最好有完整的测试代码,谢谢啦!~

楼上的你的我测试过,怎么不行啊。对于这种标签按钮,好象不支持吧
SydPink 2004-04-20
  • 打赏
  • 举报
回复
你可以找到有HANDLE的东西.不过TLabel没有HANDLE,所以用这个办法找不齐全吧??

便利窗体上所有控件.如果这个控件是TWincontrol,就继续找它的子控件.你可以找到全部,包括TLabel.....
for I := 0 to ComponentCount - 1 do
begin
if (Components[I] is TLabel) then
begin
TLabel(Components[I]].left:=0;
end;
/// ComonentCount 属性好象只有TWinControl 派生类有.
bigery 2004-04-20
  • 打赏
  • 举报
回复
都是星星级别任务,来学习!
aiirii 2004-04-20
  • 打赏
  • 举报
回复
調用:
ClickButton(hdl, btn_cap);
aiirii 2004-04-20
  • 打赏
  • 举报
回复
function EnumChildProc(Wnd: hWnd; SL: TStrings): BOOL; stdcall;
var
szFull: array[0..MAX_PATH] of Char; //Buffer for window caption
begin
Result := Wnd <> 0;
if Result then
begin
GetWindowText(Wnd, szFull, SizeOf(szFull)); // put window text in buffer
if (Pos(SL[0], StrPas(szFull)) > 0) // Test for text
and (SL.IndexOfObject(TObject(Wnd)) < 0) // Test for duplicate handles
then SL.AddObject(StrPas(szFull), TObject(Wnd)); // Add item to list
EnumChildWindows(Wnd, @EnumChildProc, Longint(SL)); //Recurse into child windows
end;
end;

function ClickButton(ParentWindow: Hwnd; ButtonCaption: string): Boolean;
var
SL: TStringList;
H: hWnd;
begin
SL := TStringList.Create;
try
SL.AddObject(ButtonCaption, nil); // First item in list is text to find
EnumChildWindows(ParentWindow, @EnumChildProc, Longint(SL));
H := 0;
case SL.Count of
1: ShowMessage('Window text not found.');
2: H := hWnd(SL.Objects[1]);
else
ShowMessage('Ambiguous text detected.');
end;
finally
SL.Free;
end;
Result := H <> 0;
if Result then PostMessage(H, BM_CLICK, 0, 0);
end;

aiirii 2004-04-20
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2981/2981509.xml?temp=.8596002

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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