深度了解电脑操作_了解拖放操作

深度了解电脑操作

To "drag and drop" is to hold down the computer mouse button as the mouse is moved, and then release the button to drop the object. Delphi makes it easy to program dragging and dropping into applications.

“拖放”是在移动鼠标时按住计算机鼠标按钮,然后释放按钮以放置对象。 通过Delphi,可以轻松地对拖放到应用程序中的程序进行编程。

You can really drag and drop from/to wherever you like, like from one form to another, or from Windows Explorer to your application.

您可以真正将其拖放到任何位置,例如从一种形式拖放到另一种形式,或从Windows资源管理器拖放到您的应用程序。

拖放示例 ( Dragging and Dropping Example )

Start up a new project and put one image control on a form. Use Object Inspector to load a picture (Picture property) and then set the DragMode property to dmManual. We'll create a program that will allow moving a TImage control runtime using the drag and drop technique.

启动一个新项目,并将一个图像控件放在窗体上。 使用对象检查器加载图片(图片属性),然后将DragMode属性设置为dmManual 。 我们将创建一个程序,该程序将允许使用拖放技术移动TImage控件运行时。

拖动模式 ( DragMode )

Components permit two types of dragging: automatic and manual. Delphi uses the DragMode property to control when the user is able to drag the control. The default value this property is dmManual, which means that dragging components around the application is not allowed, except under special circumstances, for which we have to write the appropriate code. Regardless of the setting for the DragMode property, the component will move only if the correct code is written to reposition it.

组件允许两种类型的拖动:自动和手动。 Delphi使用DragMode属性控制用户何时可以拖动控件。 此属性的默认值为dmManual,这意味着除非在特殊情况下(我们必须为此编写适当的代码),否则不允许在应用程序周围拖动组件。 无论DragMode属性的设置如何,仅当编写正确的代码以重新定位组件时,组件才会移动。

OnDragDrop ( OnDragDrop )

The event that recognizes dragging and dropping is called the OnDragDrop event. We use it to specify what we want to happen when the user drops an object. Therefore, if we want to move a component (image) to a new location on a form, we have to write code for the form's OnDragDrop event handler.

识别拖放的事件称为OnDragDrop事件。 我们使用它来指定用户放下对象时想要发生的事情。 因此,如果要将组件(图像)移动到表单上的新位置,则必须为表单的OnDragDrop事件处理程序编写代码。

The Source parameter of the OnDragDrop event is the object being dropped. The type of the source parameter is TObject. To access its properties, we have to cast it to the correct component type, which in this example is TImage.

OnDragDrop事件的Source参数是要删除的对象。 源参数的类型为TObject。 要访问其属性,我们必须将其强制转换为正确的组件类型,在本示例中为TImage。

接受 ( Accept )

We have to use the form's OnDragOver event to signal that the form can accept the TImage control we want to drop on it. Although the Accept parameter defaults to True, if an OnDragOver event handler is not supplied, the control rejects the dragged object (as if the Accept parameter was changed to False).

我们必须使用窗体的OnDragOver事件来表示该窗体可以接受我们要放置在其上的TImage控件。 尽管Accept 参数默认为True,但如果未提供OnDragOver事件处理程序,则控件将拒绝拖动的对象(就像Accept参数已更改为False一样)。

Run your project, and try dragging and dropping your image. Notice that the image remains visible in its original location while the drag mouse pointer moves. We cannot use the OnDragDrop procedure to make the component invisible while the dragging takes place because this procedure is called only after the user drops the object (if at all).

运行您的项目,然后尝试拖放您的图像。 请注意,在拖动鼠标指针移动时,图像在其原始位置仍然可见。 我们无法使用OnDragDrop过程在拖动过程中使组件不可见,因为仅在用户放下对象后(如果有的话)才调用此过程。

拖动光标 ( DragCursor )

If you want to change the cursor image presented when the control is being dragged, use the DragCursor property. The possible values for the DragCursor property are the same as those for the Cursor property. You can use animated cursors or whatever you like, like a BMP image file or a CUR cursor file.

如果要更改在拖动控件时显示的光标图像,请使用DragCursor属性。 DragCursor属性的可能值与Cursor属性的可能值相同。 您可以使用动画光标或任何喜欢的光标,例如BMP图像文件或CUR光标文件。

BeginDrag ( BeginDrag )

If DragMode is dmAutomatic, dragging begins automatically when we press a mouse button with the cursor on the control. If you've left the value of TImage's DragMode property at its default of dmManual, you have to use BeginDrag/EndDrag methods to allow dragging of the component. A more common way to drag and drop is to set DragMode to dmManual and start the dragging by handling mouse-down events.

如果DragMode为dmAutomatic,则当我们在控件上按下光标按钮时,拖动会自动开始。 如果您将TImage的DragMode属性的值保留为默认值dmManual,则必须使用BeginDrag / EndDrag方法来拖动组件。 一种更常见的拖放方式是将DragMode设置为dmManual并通过处理鼠标按下事件来开始拖动。

Now, we'll use the Ctrl+MouseDown keyboard combination to allow dragging to take place. Set TImage's DragMode back to dmManual and write the MouseDown event handler like this:

现在,我们将使用Ctrl + MouseDown键盘组合来进行拖动。 将TImage的DragMode设置回dmManual并编写MouseDown事件处理程序,如下所示:

BeginDrag takes a Boolean parameter. If we pass True (like in this code), dragging begins immediately; if False, it doesn't start until we move the mouse a short distance. Remember that it requires the Ctrl key.

BeginDrag采用布尔参数。 如果我们传递True(如此代码中所示),则拖动立即开始;否则,开始拖动。 如果为False,则直到我们将鼠标移动一小段距离后才开始。 请记住,它需要Ctrl键。

翻译自: https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386

深度了解电脑操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值