delphi 写一个可拖动的 TShape


本文整理自网络,侵删。

 本例效果图:


自定义类(TMyShape)单元 :
--------------------------------------------------------------------------------

unit Unit2;

interface

uses
Classes, Controls, ExtCtrls;

type
TMyShape = class(TShape)
private
fMouseFlag: Boolean;
fx,fy: Integer;
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer;
Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer;
Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X: Integer; Y: Integer); override;
end;

implementation

{ TMyShape }

procedure TMyShape.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
fx := X;
fy := Y;
fMouseFlag := True;
end;

procedure TMyShape.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
inherited;
if fMouseFlag then
begin
Left := Left + X - fx;
Top := Top + Y - fy;
end;
end;

procedure TMyShape.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
fMouseFlag := False;
end;

end.
--------------------------------------------------------------------------------

调用测试:
--------------------------------------------------------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
Randomize;
with TMyShape.Create(Self) do begin
Brush.Color := Random($FFFFFF);
Parent := Self;
Left := 10;
Top := 10;
end;
end;

end.

相关阅读 >>

Delphi 中的哈希表: thashedstringlist

Delphi 大小写字符串转换

Delphi窗口界面必学的知识

Delphi xe5 android 获取网络状态

Delphi 复制拷贝文件目录函数

Delphi 如何比较两个二维数组是否相等

Delphi 禁止alt+f4关闭

Delphi 如何在程序中动态设置墙纸(使用iactivedesktop接口)

Delphi 多种编码转换

Delphi xe8在firemonkey tlistbox中显示图像

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...