怎么点击其它软件窗口中的指定按钮?(200分)

  • 主题发起人 主题发起人 smartrich
  • 开始时间 开始时间
S

smartrich

Unregistered / Unconfirmed
GUEST, unregistred user!
其它软件窗口标题为“自动点击测试”,窗口中有两个按钮,第一个按钮的Caption包含有“&Y”,第二个按钮的Caption为“Cancel”<br><br>我用FindWindow (nil,'自动点击测试');获取控制权,想用FindWindowEx(hwnd,0,nil,'&Y');获取按钮控制权,但是第一个按钮的Caption只是包含有“&Y”,还有其它不断变化的字符,<br><br>请教大家,要怎么写代码才可以获取窗口中第一个按钮的控制权,并点击第一个按钮?
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2365884 看有关模拟鼠标点击按钮那一段。
 
使用DUnit
 
“&Y”很明显就是故意让你不能够找到它的句柄 但是找句柄的方法很多 有时候为了找句柄 必须从父句柄的父句柄开始找 句柄找到了用<br>postmessage或者SendMessage就可以点击
 
你用Spy++查看他的句柄 有什么特点 能够找到它的父句柄不 找到了就一定能够找出它的句柄
 
先找出窗口的句柄 chuangkou:= Findwindow('XXXX','自动点击测试');<br>然后用Spy++查出第一个按钮的ID 假设查出的ID是5<br>那么GetDlgItem(chuangkou,5);就得到按纽的句柄<br>最后用SendMessage(chuangkou,WM_command, 5,0);就可以实现点击了<br><br>如果ID等于0 那么也有其他办法 我不知道软件的具体情况 最详细只能这样跟你讲了
 
这个应该不困难,以下程序是找一个秒表程序里的所有按钮的标题,你知道了按钮标题后,用POS函数比较你的特征字串在不在里面,就可以知道是不是这个按钮了,然后用SENDMESSAGE或POSTMESSAGE向它发送消息就是:<br><br>var<br> &nbsp; ParentHandle , ChildHanlde : THandle;<br> &nbsp; c : array [0..255] of char;<br>begin<br> &nbsp; ParentHandle := FindWindow(nil, PChar('STOPWATCH'));<br> &nbsp; if ParentHandle &lt;&gt; 0 then<br> &nbsp; begin<br> &nbsp; ChildHanlde := FindWindowEx(ParentHandle, 0, PChar('TButton'), nil);<br> &nbsp; while ChildHanlde &lt;&gt; 0 do<br> &nbsp; begin<br> &nbsp; &nbsp; GetWindowText(ChildHanlde,@c,255);<br> &nbsp; &nbsp; memo1.Lines.Add(c);<br> &nbsp; &nbsp; ChildHanlde := FindWindowEx(ParentHandle, ChildHanlde, PChar('TButton'), nil);<br> &nbsp; end;<br> &nbsp; end;<br>end;
 
//先取得这个程序的主窗口句柄,根据句柄枚举子窗口,或子窗口的子窗口,找到button<br>http://www.delphibbs.com/keylife/images/u196832/AdvWNDView06-13.rar <br>窗口查看程序<br>这个程序中的代码是:<br>procedure EnumChildWindows(Parent: HWND);<br> &nbsp;function EChildProc(ChWnd: HWND; lParam: LPARAM): BOOL; stdcall;<br> &nbsp;var<br> &nbsp; &nbsp;sClassName: array [0..254] of Char;<br> &nbsp; &nbsp;Buf: PChar;<br> &nbsp; &nbsp;Len: Integer;<br> &nbsp; &nbsp;A: TListItem;<br> &nbsp; &nbsp;Rect: TRect;<br> &nbsp;begin<br> &nbsp; &nbsp;if ChWnd &lt;&gt; 0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Windows.GetClassName(ChWnd, sClassName, SizeOf(sClassName));<br> &nbsp; &nbsp; &nbsp;A := fm_tasker.LV_ChildWind.Items.Add;<br> &nbsp; &nbsp; &nbsp;A.Caption := &nbsp;Format('%8.8x', [ChWnd]);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp;if Windows.IsWindowEnabled(ChWnd) then<br> &nbsp; &nbsp; &nbsp; &nbsp;A.SubItems.AddObject('True', Pointer(ChWnd))<br> &nbsp; &nbsp; &nbsp;else A.SubItems.AddObject('False', Pointer(ChWnd));<br><br> &nbsp; &nbsp; &nbsp;A.SubItems.Add(Format('%s', [sClassName]));<br> &nbsp; &nbsp; &nbsp;Len := SendMessage(ChWnd, WM_GETTEXTLENGTH, 0, 0) + 1;<br> &nbsp; &nbsp; &nbsp;GetMem(Buf, Len);<br> &nbsp; &nbsp; &nbsp;SendMessage(ChWnd, WM_GETTEXT, Len, Longint(Buf));<br> &nbsp; &nbsp; &nbsp;if Windows.lstrlen(Buf) &gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;A.SubItems.Add(Buf)<br> &nbsp; &nbsp; &nbsp;else A.SubItems.Add('');<br> &nbsp; &nbsp; &nbsp;FreeMem(Buf, Len);<br> &nbsp; &nbsp; &nbsp;A.SubItems.Add(Format('%8.8x', [lParam]));<br> &nbsp; &nbsp; &nbsp;Windows.GetWindowRect(ChWnd, Rect);<br> &nbsp; &nbsp; &nbsp;A.SubItems.Add(Format('X:%d &nbsp;Y:%d &nbsp;W:%d &nbsp;H:%d', [Rect.Left, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top]));<br> &nbsp; &nbsp; &nbsp;//MenuItemToTreeNode(GetMenu(ChWnd), fm_tasker.TV_Menu, nil);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Result := True;<br> &nbsp;end;<br>begin<br><br> &nbsp;fm_tasker.TV_Menu.Items.Clear;<br> &nbsp;fm_tasker.TV_Menu.Tag := Parent;<br> &nbsp;MenuItemToTreeNode(GetMenu(Parent), fm_tasker.TV_Menu, nil);<br> &nbsp;MenuItemToTreeNode(GetSystemMenu(Parent, False), fm_tasker.TV_Menu, nil, True);<br><br> &nbsp;fm_tasker.LV_ChildWind.Clear;<br> &nbsp;fm_tasker.LV_ChildWind.Items.BeginUpdate;<br> &nbsp;Windows.EnumChildWindows(Parent, @EChildProc, Parent);<br> &nbsp;fm_tasker.LV_ChildWind.Items.EndUpdate;<br>end;<br><br>窗口操作:<br>3: &nbsp;begin &nbsp; &nbsp; // 最大化<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // SetForegroundWindow(H);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowWindow(H, SW_MAXIMIZE);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;4: begin &nbsp; &nbsp; // &nbsp;最小化<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// SetForegroundWindow(H);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowWindow(H, SW_MINIMIZE);<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;5: begin //单击<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(H, WM_LBUTTONDOWN, 0, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SendMessage(H, WM_LBUTTONUP, 0, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;6: &nbsp;// 双击<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(H, WM_LBUTTONDBLCLK, 0, 0);<br> &nbsp; &nbsp; &nbsp;7: &nbsp;begin // 右击<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(H, WM_RBUTTONDOWN, 0, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(H, WM_RBUTTONUP, 0, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;8: begin &nbsp;// Enabled<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; En := not Windows.IsWindowEnabled(H);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Windows.EnableWindow(H, En);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp;AppMenu.PopupComponent = LV_Child_HWND &nbsp;then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumWindow(StrToInt(LV_Child_HWND.Selected.Caption))<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if AppMenu.PopupComponent = LV_ChildWind then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumChildWindows(THandle(LV_Child_HWND.Selected.SubItems.Objects[0]));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; &nbsp;9: begin &nbsp;// Enabled<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; En := not Windows.IsWindowVisible(H);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Windows.ShowWindow(H, SWCOMMAD[En]);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if &nbsp;AppMenu.PopupComponent = LV_Child_HWND &nbsp;then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumWindow(StrToInt(LV_Child_HWND.Selected.Caption))<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if AppMenu.PopupComponent = LV_ChildWind then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnumChildWindows(THandle(LV_Child_HWND.Selected.SubItems.Objects[0]));<br> &nbsp; &nbsp; &nbsp; &nbsp; end;
 
还可以通过鼠标位置找句柄
 
function EnumProc(hwnd:Thandle;lparam:lparam):boolean; Stdcall;<br>var<br> &nbsp; &nbsp;lpText,lpCaption &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:array[0..255] of char;<br><br>begin<br> &nbsp; &nbsp;Result := true;<br> &nbsp; &nbsp;GetClassName(hwnd,@lpText,255);//得到类名。<br> &nbsp; &nbsp;if LowerCase(lpText)=lowercase('tbutton') then<br>//这个用类别来查找。这里举例用TBUTTON<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Hchild := hwnd;<br> &nbsp; &nbsp; &nbsp; &nbsp;GetWindowText(Hchild,lpCaption,100);<br> &nbsp; &nbsp; &nbsp; &nbsp;if trim(lpCaption)='按钮的CAPTION写在这里' then//在这边用CAPTION来查找<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := false//打到返回FALSE<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := true;<br> &nbsp; &nbsp;end;<br>end;<br><br>function TForm1.GetEditHandle(WinCap:string):integer;<br>var<br> &nbsp; &nbsp;hParent &nbsp; &nbsp; &nbsp; &nbsp; :Thandle;<br> &nbsp; &nbsp;i &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :integer;<br>begin<br> &nbsp; &nbsp;Result := 0;<br> &nbsp; &nbsp;Hchild := 0;<br> &nbsp; &nbsp;hParent := FindWindow(nil,pchar(''));//窗体的名称写在这边<br> &nbsp; &nbsp;if hParent=0 then Exit;<br> &nbsp; &nbsp;EnumChildWindows(hParent,@EnumProc,0);//回调EnumProc<br> &nbsp; &nbsp;Result := Hchild;<br><br>end;<br>也可用<br>var<br> H1,H2:THandle;<br>begin<br> &nbsp;H1:=FindWindow('TForm1','shenqw');<br> &nbsp;H2:=Findwindowex(H1,0,'TButton','Button1');<br> &nbsp;SendMessage(H2,bm_click,0,0
 
谢谢大家了,散分...
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
605
import
I
后退
顶部