图形移动,简单(50分)

  • 主题发起人 主题发起人 dyxhy2003
  • 开始时间 开始时间
D

dyxhy2003

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit2;

interface

uses
Windows, SysUtils, Messages, Graphics, Classes, Controls;

type
TShowImage = class(TCustomControl) //(TGraphicControl) //
private
{ Private declarations }
FPicture: TBitMap; //TPicture;
FBmp: TBitMap;
FMouseDown: Boolean;
FMouseMove: Boolean;
function GetBoundsRect: TRect;
procedure SetBoundsRect(const Rect: TRect);
procedure SetPicture(const Value: TBitMap);

protected
{ Protected declarations }
FMousePos, FBmpPos: TPoint;
procedure Paint; override;
procedure DoDraw;
public
{ Public declarations }
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;

constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Canvas;
property BoundsRect: TRect read GetBoundsRect write SetBoundsRect;
property Picture: TBitMap read FPicture write SetPicture;

published
{ Published declarations }

end;


implementation
uses unit1;


{ TShowImage }

constructor TShowImage.Create(AOwner: TComponent);
begin
inherited;
FPicture := TBitMap.Create;
FBmp := TBitMap.Create;

end;

destructor TShowImage.Destroy;
begin
FPicture.Free;
FBmp.Free;
inherited;
end;

procedure TShowImage.DoDraw;
var
SourceRect: TRect;
begin
if (Abs(FBmpPos.X) + Width) > FPicture.Width then
FBmpPos.X := Width - FPicture.Width;
if (Abs(FBmpPos.Y) + Height) > FPicture.Height then
FBmpPos.Y := Height - FPicture.Height;

if FBmpPos.X > 0 then
FBmpPos.X := 0;
if FBmpPos.Y > 0 then
FBmpPos.Y := 0;

SourceRect := RECT(0, 0, Width, Height);
OffsetRect(SourceRect, -FBmpPos.X, -FBmpPos.Y);

Canvas.CopyRect(Rect(0, 0, Width, Height),
FPicture.Canvas,
SourceRect);
end;


function TShowImage.GetBoundsRect: TRect;
begin
Result.Left := Left;
Result.Top := Top;
Result.Right := Left + Width;
Result.Bottom := Top + Height
end;

procedure TShowImage.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
if Button = mbLeft then
begin
FMouseDown := True;
FMousePos.X := x;
FMousePos.Y := y;
end;
end;

procedure TShowImage.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
Form1.Edit1.Text:=inttostr(x);
Form1.Edit2.Text:=inttostr(y);

if X=Left then
begin
条件达到的时候图象往右移[离开才不移动],这里写不成,麻烦各位大虾了,分不够好说。
DoDraw;
end;




if FMouseDown then
if (FPicture.Width > Width) or (FPicture.Height > Height) then
begin
FBmpPos.X := FBmpPos.X + (x - FMousePos.X);
FBmpPos.Y := FBmpPos.Y + (y - FMousePos.Y);
FMousePos.X := x;
FMousePos.Y := y;
DoDraw;
end;
end;

procedure TShowImage.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
FMouseDown := False;
//FMousePos.X := 0;
//FMousePos.Y := 0;
end;

procedure TShowImage.Paint;
begin
if not FPicture.Empty then
begin
DoDraw;
Canvas.Draw(0, 0, FPicture);
end;
end;

procedure TShowImage.SetBoundsRect(const Rect: TRect);
begin
with Rect do SetBounds(Left, Top, Right - Left, Bottom - Top);
end;

procedure TShowImage.SetPicture(const Value: TBitMap);
begin
FBmpPos.X:=0;
FBmpPos.Y:=0;
FPicture.Assign(Value);
end;

end.
 
procedure TShowImage.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
Form1.Edit1.Text:=inttostr(x);
Form1.Edit2.Text:=inttostr(y);

if X=Left then
begin
条件达到的时候图象往右移[离开才不移动],这里写不成,麻烦各位大虾了,分不够好说。
DoDraw;
end;

 
移动前存图片位置坐标
FMousePos.X FMousePos.Y
这里已经记录了当前图像的坐标,mousemove( ..x,y:integer);
这里是鼠标移动的坐标,x-FMousePos.X// y-FMousePos.Y//
这个就是偏移量根据偏移量调整图像位置就可以了,调整完成后保存移动后图像的坐标
给FmousePos



 
移动前存图片位置坐标?我的意思是要达到TShowImage的左边上才往右移动[不停的移动,离开以后才停],如果移动前存图片位置坐标,那么~~~~~~~~~~~~~~~~~
我觉得应该到达TShowImage的左边在存图片位置坐标
现在就是不知道怎么写到他停的往右移动!
 
怎么没人啊
 
可以确定一个移动到左边距的范围呀
 

Similar threads

后退
顶部