In the development of multimedia applications, often encounter the need to display large images in a limited area, there are many articles on this proposed a solution, such as by invoking Windows API functions, directly read and write memory. These methods have some advantages, but are more complex to implement and error prone. In practice, the author develops an interactive image walkthrough method by using Delphi's powerful object-oriented visual development environment.
Delphi, the mouse message response is through the components of the onmousedown, Onmouseu p and onmousemove events, through the three events programming, can control the image in a limited area move. Considering that the boundary of the image being moved should always be outside the area, the upper-left corner of the image should be less than the corresponding coordinate of the area, and the lower-right corner of the image should be greater than the corresponding coordinate of the region (unless the image is smaller than the region). Figure 1
The specific methods are:
1, a new project Project1, in the Form1 in turn into the Panel1, Panel2 and I mage1 components, pay attention to PA Nel2 and Image1 respectively in Panel1 and Panel2, and then a la bel1 elements into the Panel2, Resize the Panel1 to the appropriate size and modify each component property to:
元件
属性名
属性值
Panel1
BevelInner:
bvRaised
BevelOuter: bvNone
BorderStyle: bsSingle
Panel2
Align:
alClient
Image1
AutoSize:
True
Picture:
”Apple.bmp”
Label1
Align:
alClient
Transparent : True
Note: The Label1 function here is not to display characters, but to use it in response to mouse messages, if you do not use the LABEL1 and directly take advantage of Image1 mouse event response, it will because of its ONMO The activation of the Usedown event conflicts with the Image1 's own coordinate moving events, causing the image to flicker or not move.
2. Add the variable declaration after implementation:
origin:Tpoint;
image_left:integer;
image_top:integer;
visa1:Tpoint; (鼠标当前位置相对图像右下角的坐标)
visa2:Tpoint; (鼠标当前位置相对图像左上角的坐标)
canmove:boolean;
To write Label1 mouse response events:
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;