Control.DragLeave 事件

定义

将对象拖出控件的边界时发生。Occurs when an object is dragged out of the control's bounds.

public:
 event EventHandler ^ DragLeave;
public event EventHandler DragLeave;
member this.DragLeave : EventHandler 
Public Custom Event DragLeave As EventHandler 

示例

下面的代码示例演示两个 ListBox 控件之间的拖放操作。The following code example demonstrates a drag-and-drop operation between two ListBox controls. 该示例在拖动操作开始时调用 DoDragDrop 方法。The example calls the DoDragDrop method when the drag action starts. 如果鼠标在 MouseDown 事件期间从鼠标位置移动了超出 SystemInformation.DragSize,则拖动操作将启动。The drag action starts if the mouse has moved more than SystemInformation.DragSize from the mouse location during the MouseDown event. IndexFromPoint 方法用于确定 MouseDown 事件期间要拖动的项的索引。The IndexFromPoint method is used to determine the index of the item to drag during the MouseDown event.

该示例还演示了如何对拖放操作使用自定义光标。The example also demonstrates using custom cursors for the drag-and-drop operation. 该示例要求在应用程序目录中分别有两个 cursor 文件 3dwarro.cur3dwno.cur,分别用于自定义的拖放和无删除光标。The example requires that two cursor files, 3dwarro.cur and 3dwno.cur, exist in the application directory, for the custom drag and no-drop cursors, respectively. 如果选中 UseCustomCursorsCheckCheckBox,则将使用自定义光标。The custom cursors will be used if the UseCustomCursorsCheckCheckBox is checked. 自定义游标是在 GiveFeedback 事件处理程序中设置的。The custom cursors are set in the GiveFeedback event handler.

键盘状态在右侧 ListBoxDragOver 事件处理程序中进行计算,以确定拖动操作将基于 SHIFT、CTRL、ALT 或 CTRL + ALT 键的状态。The keyboard state is evaluated in the DragOver event handler for the right ListBox, to determine what the drag operation will be based on state of the SHIFT, CTRL, ALT, or CTRL+ALT keys. DragOver 事件期间,也确定了放置的 ListBox 位置。The location in the ListBox where the drop would occur is also determined during the DragOver event. 如果要删除的数据不是 String,则将 DragEventArgs.Effect 设置为 DragDropEffects中的 NoneIf the data to drop is not a String, then the DragEventArgs.Effect is set to None in DragDropEffects. 最后,drop 的状态显示在 DropLocationLabelLabel中。Finally, the status of the drop is displayed in the DropLocationLabelLabel.

向右 ListBox 放置的数据是在 DragDrop 事件处理程序中确定的,String 值将添加到 ListBox中的适当位置。The data to drop for the right ListBox is determined in the DragDrop event handler and the String value is added at the appropriate place in the ListBox. 如果拖动操作移到窗体的边界之外,则会在 QueryContinueDrag 事件处理程序中取消拖放操作。If the drag operation moves outside the bounds of the form, then the drag-and-drop operation is canceled in the QueryContinueDrag event handler.

此代码摘录演示了如何使用 DragLeave 事件。This code excerpt demonstrates using the DragLeave event. 有关完整的代码示例,请参阅 DoDragDrop 方法。See the DoDragDrop method for the complete code example.

void ListDragTarget_DragLeave( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
  // Reset the label text.
   DropLocationLabel->Text = "None";
}
private void ListDragTarget_DragLeave(object sender, System.EventArgs e) {
    // Reset the label text.
    DropLocationLabel.Text = "None";
}
Private Sub ListDragTarget_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListDragTarget.DragLeave

    ' Reset the label text.
    DropLocationLabel.Text = "None"
End Sub

注解

当用户将光标拖出控件或用户取消当前拖放操作时,将引发 DragLeave 事件。The DragLeave event is raised when the user drags the cursor out of the control or the user cancels the current drag-and-drop operation.

下面描述与拖放操作相关的事件的引发方式以及引发时间。The following describes how and when events related to drag-and-drop operations are raised.

DoDragDrop 方法确定当前光标位置下的控件。The DoDragDrop method determines the control under the current cursor location. 然后,它会检查控件是否为有效的拖放目标。It then checks to see if the control is a valid drop target.

如果控件是有效的拖放目标,则会随指定的拖放效果引发 GiveFeedback 事件。If the control is a valid drop target, the GiveFeedback event is raised with the drag-and-drop effect specified. 有关拖放效果的列表,请参见 DragDropEffects 枚举。For a list of drag-and-drop effects, see the DragDropEffects enumeration.

跟踪鼠标光标位置、键盘状态和鼠标按钮状态的更改。Changes in the mouse cursor position, keyboard state, and mouse button state are tracked.

  • 如果用户移出一个窗口,则引发 DragLeave 事件。If the user moves out of a window, the DragLeave event is raised.

  • 如果鼠标进入另一个控件,则引发该控件的 DragEnterIf the mouse enters another control, the DragEnter for that control is raised.

  • 如果鼠标移动但停留在同一个控件中,则引发 DragOver 事件。If the mouse moves but stays within the same control, the DragOver event is raised.

如果键盘或鼠标按钮状态发生了更改,则会引发 QueryContinueDrag 事件,并确定是继续拖动、删除数据还是根据事件的 QueryContinueDragEventArgsAction 属性的值来取消操作。If there is a change in the keyboard or mouse button state, the QueryContinueDrag event is raised and determines whether to continue the drag, to drop the data, or to cancel the operation based on the value of the Action property of the event's QueryContinueDragEventArgs.

  • 如果 ContinueDragAction 的值,则将引发 DragOver 事件以继续操作,并将以新效果引发 GiveFeedback 事件,以便可以设置适当的视觉反馈。If the value of DragAction is Continue, the DragOver event is raised to continue the operation and the GiveFeedback event is raised with the new effect so appropriate visual feedback can be set. 有关有效放置效果的列表,请参见 DragDropEffects 枚举。For a list of valid drop effects, see the DragDropEffects enumeration.

    备注

    DragOverGiveFeedback 事件是成对的,因此当鼠标移动到放置目标上时,将为用户提供有关鼠标位置的最新反馈。The DragOver and GiveFeedback events are paired so that as the mouse moves across the drop target, the user is given the most up-to-date feedback on the mouse's position.

  • 如果 DropDragAction 的值,则将删除效果值返回到源,以便源应用程序可以对源数据执行适当的操作;例如,如果是移动操作,则剪切数据。If the value of DragAction is Drop, the drop effect value is returned to the source, so the source application can perform the appropriate operation on the source data; for example, cut the data if the operation was a move.

  • 如果 CancelDragAction 的值,则会引发 DragLeave 事件。If the value of DragAction is Cancel, the DragLeave event is raised.

有关处理事件的详细信息,请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅