关于图片拖动的问题!!! |
尚未結案
|
bleaksky
一般會員 發表:1 回覆:0 積分:0 註冊:2007-01-09 發送簡訊給我 |
是这样的我在 ScrollBox 上加了 Panel 和 Image
Image 上的图片大于 Panel 的尺寸想让 Image 上图片可以跟随鼠标拖动。 我在 Image 上写了: 在 private 定义了: Origin : TPoint; Image_Left: Integer; Image_Top : Integer; Visa1 : TPoint; Visa2 : TPoint; CanMove : Boolean; procedure TForm1.ImageMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if (Image1.Width <= Panel1.Width) and (Image1.Height <= Panel1.Height) then begin CanMove:=false; exit; end; if Button=mbLeft then begin Origin.X:= X; Origin.Y:= Y; Image_Left:= Image1.Left; Image_Top := Image1.Top; Visa1.X:= X-(Image1.Width - Panel1.Width Image1.Left); Visa1.y:= Y-(Image1.Height - Panel1.Height Image1.Top); Visa2.x:= X-Image1.left; Visa2.y:= Y-Image1.top; CanMove:= true; end; end; procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if (Image1.Width > Panel1.Width) or (Image1.Height > Panel1.Height) then 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; if Image1.Left <= Panel1.Left then Image1.Left:= Image_Left (X-Origin.X); if Image1.Top <= Panel1.Top then Image1.Top:= Image_top (Y-Origin.y); end; end; procedure TForm1.ImageMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin CanMove:=false; end; 这样写不行,Image上的图片不能跟鼠标移动,有谁能帮我一下。。。。 |
John Wong
初階會員 發表:1 回覆:35 積分:32 註冊:2004-09-18 發送簡訊給我 |
看不明你的程式碼, 為你寫了個在panel中放入一個button, 按著button可以令button移動但不會超出panel範圍的. 你可以參考一下.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Button1: TButton; Panel1: TPanel; procedure Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure Button1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } MD: Boolean; MX, MY: Integer; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button = mbLeft then begin MD := True; MX := X; MY := Y; end; end; procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var NewLeft, NewTop: Integer; begin if MD then with (Sender as TButton) do begin NewLeft := Left (X - MX); NewTop := Top (Y - MY); if NewLeft < 0 then NewLeft := 0; if NewLeft Width > Parent.Width then NewLeft := Parent.Width - Width; if NewTop < 0 then NewTop := 0; if NewTop height > Parent.Height then NewTop := Parent.Height - Height; SetBounds(NewLeft, NewTop, Width, Height); end; end; procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button = mbLeft then MD := False; end; end. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |