delphi 控件移动_如何在运行时移动和调整控件大小(在Delphi应用程序中)

本文介绍了如何在Delphi应用程序运行时通过鼠标启用控件的拖动和大小调整,包括处理OnMouseDown、OnMouseMove和OnMouseUp事件,以及如何在关闭和打开表单时记住控件的位置和大小。
摘要由CSDN通过智能技术生成

delphi 控件移动

Here's how to enable dragging and resizing controls (on a Delphi form) with a mouse, while the application is running.

以下是在应用程序运行时如何使用鼠标启用拖动和调整控件大小(在Delphi窗体上)的方法。

运行时的表单编辑器 ( Form Editor at Run-Time )

Once you place a control (visual component) on the form, you can adjust its position, size, and other design-time properties. There are situations, though, when you have to allow a user of your application to reposition form controls and change their size, at run-time.

将控件(可视组件)放置在窗体上后,就可以调整其位置,大小和其他设计时属性。 但是,在某些情况下,必须允许应用程序的用户在运行时重新定位表单控件并更改其大小。

To enable runtime user movement and resizing of controls on a form with a mouse, three mouse related events need special handling: OnMouseDown, OnMouseMove, and OnMouseUp.

为了使运行时用户能够使用鼠标移动和调整窗体上控件的大小,与鼠标相关的三个事件需要特殊处理:OnMouseDown,OnMouseMove和OnMouseUp。

In theory, let's say you want to enable a user to move (and resize) a button control, with a mouse, at run-time. Firstly, you handle the OnMouseDown event to enable the user to "grab" the button. Next, the OnMouseMove event should reposition (move, drag) the button. Finally, OnMouseUp should finish the move operation.

从理论上讲,您想让用户在运行时使用鼠标移动(并调整大小)按钮控件。 首先,您处理OnMouseDown事件以使用户能够“抓住”按钮。 接下来,OnMouseMove事件应重新定位(移动,拖动)按钮。 最后,OnMouseUp应该完成移动操作。

在实践中拖动和调整窗体控件的大小 ( Dragging and Resizing Form Controls in Practice )

Firstly, drop several controls on a form. Have a CheckBox to enable or disable moving and resizing controls at run-time.

首先,在窗体上放置几个控件。 有一个CheckBox可以在运行时启用或禁用移动和调整控件大小。

Next, define three procedures (in the interface section of the form declaration) that will handle mouse events as described above:

接下来,定义三个过程(在表单声明的接口部分中),这些过程将如上所述处理鼠标事件:


type
TForm1 = class(TForm)
...
procedure ControlMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
procedure ControlMouseMove(Sender: TObject;
Shift: TShiftState;
X, Y: Integer);
procedure ControlMouseUp(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
private
inReposition : boolean;
oldPos : TPoint;

Note: Two form level variables are required to mark if control movement is taking place (inReposition) and to store control old position (oldPos).

注意:需要两个表单级别变量来标记是否正在发生控件移动( inReposition )和存储控件的旧位置( oldPos )。

In the form's OnLoad event, assign mouse event handling procedures to corresponding events (for those controls you want to be draggable/resizable):

在窗体的OnLoad事件中,将鼠标事件处理过程分配给相应的事件(对于您想要可拖动/可调整大小的控件):


procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.OnMouseDown := ControlMouseDown;
Button1.OnMouseMove := ControlMouseMove;
Button1.OnMouseUp := ControlMouseUp;
Edit1.OnMouseDown := ControlMouseDown;
Edit1.OnMouseMove := ControlMouseMove;
Edit1.OnMouseUp := ControlMouseUp;
Panel1.OnMouseDown := ControlMouseDown;
Panel1.OnMouseMove := ControlMouseMove;
Panel1.OnMouseUp := ControlMouseUp;
Button2.OnMouseDown := ControlMouseDown;
Button2.OnMouseMove := ControlMouseMove;
Button2.OnMouseUp := ControlMouseUp;
end; (*FormCreate*)

Note: the above code enables run-time reposition of Button1, Edit1, Panel1, and Button2.

注意:上面的代码启用了Button1,Edit1,Panel1和Button2的运行时重定位。

Finally, here's the magic code:

最后,这是魔术代码:


procedure TForm1.ControlMouseDown(
Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
if (chkPositionRunTime.Checked) AND
(Sender is TWinControl) then
begin
inReposition:=True;
SetCapture(TWinControl(Sender).Handle);
GetCursorPos(oldPos);
end;
end; (*ControlMouseDown*)

ControlMouseDown in short: once a user presses a mouse button over a control, if run-time reposition is enabled (checkbox chkPositionRunTime is Checked) and the control that received the mouse down even is derived from TWinControl, mark that control reposition is taking place (inReposition:=True) and make sure all mouse processing is captured for the control - to prevent default "click" events from being processed.

简而言之,就是ControlMouseDown :用户在控件上按下鼠标按钮后,如果启用了运行时重定位(选中chkPositionRunTime复选框)并且接收到鼠标按下的控件甚至是从TWinControl派生的,则标记正在进行控件重定位( inReposition:= True),并确保已捕获控件的所有鼠标处理-以防止处理默认的“ click”事件。


procedure TForm1.ControlMouseMove(
Sender: TObject;
Shift: TShiftState;
X, Y: Integer);
const
minWidth = 20;
minHeight = 20;
var
newPos: TPoint;
frmPoint : TPoint;
begin
if inReposition then
begin
with TWinControl(Sender) do
begin
GetCursorPos(newPos);
if ssShift in Shift then
begin //resize
Screen.Cursor := crSizeNWSE;
frmPoint := ScreenToClient(Mouse.CursorPos);
if frmPoint.X > minWidth then
Width := frmPoint.X;
if frmPoint.Y > minHeight then
Height := frmPoint.Y;
end
else //move
begin
Screen.Cursor := crSize;
Left := Left - oldPos.X + newPos.X;
Top := Top - oldPos.Y + newPos.Y;
oldPos := newPos;
end;
end;
end;
end; (*ControlMouseMove*)

ControlMouseMove in short: change the Screen Cursor to reflect the operation: if the Shift key is pressed allow control resizing, or simply move the control to a new position (where the mouse is going). Note: minWidth and minHeight constants provide a sort of size constraint (minimum control width and height).

简而言之, ControlMouseMove :更改屏幕光标以反映操作:如果按下Shift键,则可以调整控件的大小,或者只是将控件移到新位置(鼠标移至的位置)。 注意: minWidthminHeight常量提供了一种大小限制(最小控件宽度和高度)。

When the mouse button is released, dragging or resizing is over:

释放鼠标按钮时,拖动或调整大小结束:


procedure TForm1.ControlMouseUp(
Sender: TObject;
Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if inReposition then
begin
Screen.Cursor := crDefault;
ReleaseCapture;
inReposition := False;
end;
end; (*ControlMouseUp*)

ControlMouseUp in short: when a user has finished moving (or resizing the control) release the mouse capture (to enable default click processing) and mark that reposition is finished.

简而言之, ControlMouseUp :当用户完成移动(或调整控件大小)后,释放鼠标捕获(以启用默认的单击处理),并标记重新放置完成。

And that does it! Download the sample application and try for yourself.

做到了! 下载示例应用程序,然后自己尝试。

Note: Another way to move controls at run-time is to use Delphi's drag and drop related properties and methods (DragMode, OnDragDrop, DragOver, BeginDrag, etc.). Dragging and dropping can be used to let users drag items from one control - such as a list box or tree view - into another.

注意:在运行时移动控件的另一种方法是使用Delphi的拖放相关属性和方法(DragMode,OnDragDrop,DragOver,BeginDrag等)。 拖放可用于让用户将项目从一个控件(例如列表框或树视图)拖到另一个控件中。

如何记住控制位置和大小? ( How to Remember Control Position and Size?​ )

If you allow a user to move and resize form controls, you have to ensure that control placement is somehow saved when the form is closed and that each control's position is restored when the form is created/loaded. Here's how to store the Left, Top, Width and Height properties, for every control on a form, in an INI file.

如果允许用户移动和调整表单控件的大小,则必须确保在关闭表单时以某种方式保存控件的位置,并确保在创建/加载表单时恢复每个控件的位置。 这是在INI文件中存储窗体上每个控件的Left,Top,Width和Height属性的方法。

8个大小手柄怎么样? ( How About 8 Size Handles?​ )

When you allow a user to move and resize controls on Delphi form, at run-time using the mouse, to fully mimic the design-time environment, you should add eight size handles to the control being resized.

如果允许用户在运行时使用鼠标在Delphi窗体上移动控件并调整其大小,以完全模仿设计时环境,则应在要调整大小的控件上添加八个大小句柄。

翻译自: https://www.thoughtco.com/how-to-move-and-resize-controls-at-run-time-4092542

delphi 控件移动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值