我在Form上加了一个Image控件并Load了一副图象,请问如何OnMouseDown在图象上,而获得在Form上的坐标(X,Y)? ( 积分: 100 )

  • 主题发起人 主题发起人 々飞翔鸟々
  • 开始时间 开始时间

々飞翔鸟々

Unregistered / Unconfirmed
GUEST, unregistred user!
我在Form上加了一个Image控件并Load了一副图象,请问如何OnMouseDown在图象上,而获得在Form上的坐标(X,Y)?<br>procedure&nbsp;TfrmChina.FormMouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Button=mbRight&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;id&lt;&gt;1&nbsp;then&nbsp;&nbsp;//画出起点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.MoveTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;begin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//画出第二点并连线<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Color:=clblue;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Width:=3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.MoveTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frmChina.Canvas.LineTo(x,y);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cx:=x;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cy:=y;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>end;<br><br>在form上没有问题<br>但是在image上时候偏移量很大<br>请问怎么转换坐标啊<br>最好给出源码,谢谢
 
image.left&nbsp;+&nbsp;X<br>image.top&nbsp;+&nbsp;y<br>就可以得到在Form的坐标!
 
procedure&nbsp;TForm1.Image1MouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>var<br>&nbsp;&nbsp;p:tpoint;<br>begin<br>&nbsp;&nbsp;p:=image1.ClientToParent(point(x,y));<br>&nbsp;&nbsp;self.Caption:='x:'+inttostr(p.x)+'&nbsp;y:'+inttostr(p.Y);<br>end;
 
窗口坐标为图像坐标分别再加上X,Y坐标!
 
大家的方法都试过了<br>还是不行&nbsp;高手加我QQ:99522057&nbsp;吧<br>谢谢
 
你这个过程FormMouseDown己经就是相对于窗体的座标了啊.
 
.....不行??!!&nbsp;-_-......无语,把你代码贴上来看看
 
我截图了不懂怎么发上来给你们看<br>不用说别的了&nbsp;<br>鼠标点下去的第一个点都不对<br>点的是一个地方&nbsp;看到的点在另外一个地方<br>而且是比较远&nbsp;不是说一点误差<br>你加我QQ我好贴图给你们看啦<br>要不你给一个能运行的准确的发我邮箱吧<br>谢谢了&nbsp;&nbsp;我要的功能很简单,鼠标点第二个点的时候用线把第一第二个点连起来如此往下<br>只要能做到点在哪里就画在哪里就行了<br>bj_fuhong@126.com<br>procedure&nbsp;TfrmChina.Image2MouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>var<br>&nbsp;&nbsp;p:tpoint;<br>&nbsp;&nbsp;cx,cy:integer;<br>begin<br><br>&nbsp;&nbsp;p:=image2.ClientToParent(point(x,y));<br>//&nbsp;&nbsp;p:=image2.ClientToScreen(point(x,y));<br>&nbsp;&nbsp;self.Caption:='x:'+inttostr(p.x)+'&nbsp;y:'+inttostr(p.Y);<br>&nbsp;&nbsp;cx:=p.X;<br>&nbsp;&nbsp;cy:=p.Y;<br>//cx:=image2.Left+x;<br>//cy:=image2.Top+y;<br><br>&nbsp;&nbsp;if&nbsp;id&lt;&gt;1&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(cx,cy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(cx,cy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:=1;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clblue;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(cx,cy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(cx,cy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(cx,cy);<br>&nbsp;&nbsp;end;<br>end;
 
简单,就放一Timage:<br><br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;ExtCtrls;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Image1:&nbsp;TImage;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Image1MouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;Image1.Canvas.PenPos&nbsp;:=&nbsp;Point(-1,&nbsp;-1);&nbsp;&nbsp;&nbsp;//初始化,让画笔放在画布外<br>end;<br><br>procedure&nbsp;TForm1.Image1MouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>begin<br>&nbsp;&nbsp;with&nbsp;Image1.Canvas&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;PenPos.X&nbsp;&gt;&nbsp;-1&nbsp;then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//检查画笔的位置,如果在画布内说明是已经不是第一次点,画一直线到该点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LineTo(X,&nbsp;Y)<br>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;&nbsp;&nbsp;&nbsp;//否则根据初始化设置,说明为第一次点,那么移动画笔到该点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PenPos&nbsp;:=&nbsp;Point(X,&nbsp;Y);<br>end;<br><br>end.
 
倒......你画到image上&nbsp;取form的坐标干吗?事件里的XY本身就是image的位置<br>var<br>&nbsp;&nbsp;ox,oy:integer;<br><br>procedure&nbsp;TfrmChina.Image2MouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>begin<br>&nbsp;&nbsp;if&nbsp;id=0&nbsp;then&nbsp;&nbsp;{我假设id=0时是第一个点}<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clblue;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(ox,oy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(ox,oy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(ox,oy);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;ox:=x;<br>&nbsp;&nbsp;oy:=y;<br>&nbsp;&nbsp;id:=1;<br>end;
 
难道我的Delphi有问题?<br>你们都运行过了吗?<br>发你们能运行的可执行文件来看看
 
ANiDelphi&nbsp;的方法能行
 
...我就不明白了.....难道我那段代码还需要别的吗?给你发邮件了
 
procedure&nbsp;TfrmChina.Image2MouseDown(Sender:&nbsp;TObject;&nbsp;Button:&nbsp;TMouseButton;<br>&nbsp;&nbsp;Shift:&nbsp;TShiftState;&nbsp;X,&nbsp;Y:&nbsp;Integer);<br>begin<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Button=mbleft&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;id&lt;&gt;1&nbsp;then&nbsp;&nbsp;//画出起点<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:=1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;begin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//画出第二点并连线<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clblue;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=3;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Color:=clred;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Style:=psDash;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.Pen.Width:=10;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.MoveTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Image2.Canvas.LineTo(x,y);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br><br>end;<br><br>代码还是一样的<br>可以了<br>其实是&nbsp;strtch属性设置为true惹的祸<br>多谢各位的参与<br>结贴
 
多人接受答案了。
 
.....我吐血去了
 

Similar threads

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