扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
Delphi中,鼠标的消息响应是通过元件的OnMouseDown、OnMouseU p和OnMouseMove事件实现的,通过对此三个事件编程,可控制图像在有限区域内移动。考虑到所移动的图像的边界应总在该区域外,因此图像的左上角坐标应小于该区域对应坐标,图像右下角坐标应大于该区域对应坐标(除非图像大小比该区域小)。图1
具体方法是:
1、新建一工程Project1,在Form1中依次放入Panel1、Panel2和I mage1元件,注意Pa nel2和Image1分别在Panel1和Panel2上,再将一La bel1元件加入Panel2中,调整Panel1尺寸为适当大小,并修改各元件属性为:
元件 属性名 属性值 Panel1 BevelInner: bvRaised BevelOuter: bvNone BorderStyle: bsSingle Panel2 Align: alClient Image1 AutoSize: True Picture: ”Apple.bmp” Label1 Align: alClient Transparent : True |
注意:此处Label1的作用不是显示字符,而是利用它响应鼠标消息 ,如果不用Label1而直接利用Image1的鼠标事件响应,则会由于其OnMo useDown事件的激活与Image1的自身坐标移动事件冲突而使图像发生闪烁甚至不能移动。
2、在implementation后加入变量声明:
origin:Tpoint; image_left:integer; image_top:integer; visa1:Tpoint; (鼠标当前位置相对图像右下角的坐标) visa2:Tpoint; (鼠标当前位置相对图像左上角的坐标) canmove:boolean; |
编写Label1鼠标响应事件:
procedure TForm1.Label1MouseDown(Sender: TObject; Button : TMouseButton;S hift: TShiftState; X, Y: Integer); begin if Button=mbLeft then begin origin.x:=X; origin.y:=Y; image_left:=image1.left; image_top:=image1.top; visa1.x:=X-(image1.width-panel2.width+image1.left); visa1.y:=Y-(image1.height-panel2.height+image1.top); visa2.x:=X-image1.left; visa2.y:=Y-image1.top; canmove:=true; end; end; procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if canmove then begin if X< visa1.x then X:=visa1.x; if X>visa2.x then X:=visa2.x; if Y< visa1.y then Y:=visa1.y; if Y>visa2.y then Y:=visa2.y; image1.left:=image_left+(X-origin.x); image1.top:=image_top+(Y-origin.y); end; end; procedure TForm1.Label1MouseUp(Sender: TObject; Button: TMouseButton;Shi ft: TShiftState; X, Y: Integer); begin canmove:=false; end; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者