(急急急!要不然哥们儿我就被。。。。。)!!!!问:怎样用消息去点击toolbar上的toolbutton(有2个)急急急急急!!!!(100分)

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

Sterntaler

Unregistered / Unconfirmed
GUEST, unregistred user!
一个窗体(不知道源代码)上有一个toolbar,toolbar上有2个toolbutton,<br>问怎样用消息去点击第一个toolbutton<br><br>注:toolbutton不是窗体,所以根本没有句柄。好像只能用鼠标坐标的定位方法<br>搞定
 
kao,有深度
 
建议用一个ActionList<br>每个ToolButton对应一个Action<br>执行时只要加入代码Action.Execute<br>这种方法非常方便<br>ToolButton上就不用写代码了<br>只要设置Action属性对应的Action
 
关键是我不知道,含toolbutton的程序的源代码<br>那该怎么办!!!!!!!!!!1
 
var<br>&nbsp; hTBar : HWND;<br>&nbsp; tb : TTBButton;<br>begin<br>&nbsp; hTBar := FindWindow(.....);//先找ToolBar的句柄<br>&nbsp; SendMessage(hTBar, TB_GETBUTTON, 0, Integer(@tb));<br>&nbsp; SendMessage(hTBar, TB_PRESSBUTTON, tb.idCommand, MAKELONG(1, 0));<br>end;
 
测试中,点击还未成功,特别是对其它应用程序的TOOLBAR发消息,必定造成对方死掉:(<br>相关文章:<br>http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20444739.html<br>VB的东东<br>http://www.vbaccelerator.com/home/VB/Code/Controls/Toolbar/vbAccelerator_ToolBar_and_CoolMenu_Control/VB6_Toolbar_Complete_Source_zip_cToolbarMenu_cls.asp
 
ToolBar.FindComponent('组件名称')<br>找到后就可以执行它的代码了。
 
FindWindow, PostMessage, WM_LButtonDown, WM_LButtonUp<br>
 
计算坐标,向toolbutton的父组件ToolBar发消息就行了
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ComCtrls, ToolWin, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ToolBar1: TToolBar;<br>&nbsp; &nbsp; ToolButton1: TToolButton;<br>&nbsp; &nbsp; ToolButton2: TToolButton;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure ToolButton1Click(Sender: TObject);<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>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure MouseMoveTo(x, y: integer);<br>var<br>&nbsp; p: TPoint;<br>&nbsp; moveX, moveY: integer;<br>&nbsp; i: integer;<br>begin<br><br>&nbsp; GetCursorPos(p);<br>&nbsp; while (abs(x - p.X) &gt; 20) or (abs(y - p.y) &gt; 20) do<br>&nbsp; &nbsp; begin<br><br>&nbsp; &nbsp; &nbsp; moveX := (x - p.X);<br>&nbsp; &nbsp; &nbsp; moveY := (y - p.y);<br>&nbsp; &nbsp; &nbsp; if abs(moveX) &gt; abs(moveY) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if moveX &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i := 20<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i := -20;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveY := round(moveY / moveX * i + 0.5);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveX := i;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if moveY &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i := 20<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i := -20;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveX := round(moveX / moveY * i + 0.5);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; moveY := i;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; mouse_event(MOUSEEVENTF_MOVE, moveX, movey, 0, 0);<br>&nbsp; &nbsp; &nbsp; sleep(50);<br><br>&nbsp; &nbsp; &nbsp; GetCursorPos(p);<br>&nbsp; &nbsp; end;<br>&nbsp; GetCursorPos(p);<br>&nbsp; while (p.x &lt;&gt; x) or (p.Y &lt;&gt; y) do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; moveX := 0;<br>&nbsp; &nbsp; &nbsp; moveY := 0;<br>&nbsp; &nbsp; &nbsp; if x &gt; p.X then<br>&nbsp; &nbsp; &nbsp; &nbsp; moveX := 1;<br>&nbsp; &nbsp; &nbsp; if x &lt; p.X then<br>&nbsp; &nbsp; &nbsp; &nbsp; moveX := -1;<br><br>&nbsp; &nbsp; &nbsp; if y &gt; p.y then<br>&nbsp; &nbsp; &nbsp; &nbsp; movey := 1;<br>&nbsp; &nbsp; &nbsp; if y &lt; p.y then<br>&nbsp; &nbsp; &nbsp; &nbsp; movey := -1;<br><br>&nbsp; &nbsp; &nbsp; mouse_event(MOUSEEVENTF_MOVE, moveX, movey, 0, 0);<br>&nbsp; &nbsp; &nbsp; GetCursorPos(p);<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure MouseMoveToControl(V: TControl; WillClick: boolean);<br>var<br>&nbsp; p: Tpoint;<br>begin<br><br>&nbsp; p.X := v.Width div 2;<br>&nbsp; p.Y := v.Height div 2;<br>&nbsp; p := v.ClientToScreen(p);<br>&nbsp; MouseMoveTo(p.X, p.Y);<br>&nbsp; if WillClick then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; sleep(400);<br><br>&nbsp; &nbsp; &nbsp; mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);<br>&nbsp; &nbsp; &nbsp; mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);<br>&nbsp; &nbsp; end;<br><br>end;<br>procedure MouseMoveToWillClick(x, y: integer);<br>begin<br>&nbsp; MouseMoveTo(x, y);<br>&nbsp; sleep(400);<br><br>&nbsp; mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);<br>&nbsp; mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);<br><br>end;<br>procedure TForm1.ToolButton1Click(Sender: TObject);<br>begin<br>&nbsp; showmessage(Sender.ClassName);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp;MouseMoveToControl &nbsp;(ToolButton1,true) ;<br>end;<br><br>end.<br><br>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
921
DelphiTeacher的专栏
D
顶部