delphi 鼠标拖动控件自由移动位置


本文整理自网络,侵删。

 

方法:

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    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 }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  isMouseDown : boolean ; // 是否拖拽
  posX,posY : Integer;    // 拖动开始时候的坐标
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isMouseDown := True;                          // 开始拖动
  posX := Mouse.CursorPos.X  - Button1.Left ;   // 记录开始的X坐标
  posY := Mouse.CursorPos.Y  - Button1.Top  ;   // 记录开始的y坐标
end;
 
procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if isMouseDown  then                           // 鼠标是按下状态(可以拖)
  begin
    Button1.Left := Mouse.CursorPos.X - posX ;   // 设置新的X位置(Left)
    Button1.Top := Mouse.CursorPos.Y - posY ;    // 设置新的Y位置(Top)
  end;
end;
 
procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  isMouseDown := False;                          // 拖动结束
end;
 
end.

 

来源:https://bbs.csdn.net/topics/391076577

相关阅读 >>

Delphi txt 指定行复制器

Delphi 关于汇编call的例子

Delphi 把exe嵌入到自己的exe中。Delphi xe3

Delphi 10.3.3 启动cnpack ide 专家 cnwizards coreide260.bpl错误解决办法

Delphi下载网站文件(支持https协议)

Delphi 检测用户超过多长时间没有操作键盘或鼠标

Delphi中使用词霸2005的动态库xdictgrb.dll实现屏幕取词

Delphi webbroker isapi 示例说明

为什么编程是独一无二的职业

Delphi动态创建tadoquery加access出现'不正常的定义参数对象 提供了不一致或不完整的信息'错误

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



打赏

取消

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

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

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

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

评论

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