在开发多媒体应用中,经常会遇到需要在有限区域内显示大图像的情况,有不少文章对此提出过解决方法,如通过调用windows的api函数,直接读写内存等。这些方法有某些优点,但实现起来较为复杂,且易出错。 笔者在实践中通过仔细摸索,利用delphi的强大的面向对象可视化开发环境开发了一种交互式图像漫游方法。
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; |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器