实现控件的移动,改变大小(DELPHI实现)

本文介绍如何使用DELPHI实现控件的移动与大小调整功能。通过在Panel控件的鼠标移动及按下事件中编写特定代码,可以实现对控件尺寸与位置的灵活调整,并根据不同鼠标位置显示相应的调整光标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现控件的移动,改变大小(DELPHI实现)

主要使用Perform方法
  function Perform(Msg: Cardinal; WParam, LParam: Longint): Longint;
 
只要能够使用类似于win32API的函数SendMessage(),本方法同样可在其他环境里应用。

用Delphi实现
首先,建立一个应用程序,在一个窗体里加入一个Panel1,保存为main.pas;
然后,分别在Panel1的鼠标移动、鼠标按下事件里添加代码;
鼠标移动:控制光标的形状
procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if (x>=0)and(x<=3) then
  begin
    if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNWSE;
    if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crSizeWE;
    if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Cursor:=crSizeNESW;
  end
  else if (x>3)and(x<Panel1.Width-3) then
  begin
    if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNS;
    if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crArrow;
    if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Cursor:=crSizeNS;
  end
  else if (x>=Panel1.Width-3)and(x<=Panel1.Width) then
  begin
    if (y>=0)and(y<=3) then Panel1.Cursor:=crSizeNESW;
    if (y>3)and(y<Panel1.Height-3) then Panel1.Cursor:=crSizeWE;
    if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Cursor:=crSizeNWSE;
  end;
end;

鼠标按下:控制Panel的大小或位置
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  if (x>=0)and(x<=3) then
  begin
    file://左上角方向改变大小
    if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F004,0);
    file://左侧
    if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F001,0);
    file://左下角
    if (y>=Panel1.Height-3)and(y<=Panel1.Height) then Panel1.Perform(WM_SysCommand,$F007,0);
  end
  else if (x>3)and(x<Panel1.Width-3) then
  begin
    file://上侧
    if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F003,0);
    file://移动控件
    if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F012,0);
    file://下侧
    if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F006,0);
  end
  else if (x>=Panel1.Width-3)and(x<=Panel1.Width) then
  begin
    file://右上角
    if (y>=0)and(y<=3) then Panel1.Perform(WM_SysCommand,$F005,0);
    file://右侧
    if (y>3)and(y<Panel1.Height-3) then Panel1.Perform(WM_SysCommand,$F002,0);
    file://右下角
    if (y>=Panel1.Height-3)and(y<=Panel1.Width) then Panel1.Perform(WM_SysCommand,$F008,0);
  end;
end;
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值