ToolStripItem.DragEnter ToolStripItem.DragEnter ToolStripItem.DragEnter ToolStripItem.DragEnter Event

定义

当用户将某项拖动到该项的工作区内时发生。Occurs when the user drags an item into the client area of this item.

public:
 event System::Windows::Forms::DragEventHandler ^ DragEnter;
[System.ComponentModel.Browsable(false)]
public event System.Windows.Forms.DragEventHandler DragEnter;
member this.DragEnter : System.Windows.Forms.DragEventHandler 
Public Custom Event DragEnter As DragEventHandler 
属性

示例

下面的代码示例演示如何将XY属性转换为客户端PointThe following code example shows how to convert the X and Y properties to a client Point. 此代码示例是为ToolStripRenderer类提供的更大示例的一部分。This code example is part of a larger example provided for the ToolStripRenderer class.

// This method defines the DragOver event behavior. 
protected override void OnDragOver(DragEventArgs dea)
{
    base.OnDragOver(dea);

    // Get the ToolStripButton control 
    // at the given mouse position.
    Point p = new Point(dea.X, dea.Y);
    ToolStripButton item = this.GetItemAt(
        this.PointToClient(p)) as ToolStripButton;

    // If the ToolStripButton control is the empty cell,
    // indicate that the move operation is valid.
    if( item == this.emptyCellButton )
    {
        // Set the drag operation to indicate a valid move.
        dea.Effect = DragDropEffects.Move;
    }
}
' This method defines the DragOver event behavior. 
Protected Overrides Sub OnDragOver(dea As DragEventArgs)
   MyBase.OnDragOver(dea)
   
   ' Get the ToolStripButton control 
   ' at the given mouse position.
   Dim p As New Point(dea.X, dea.Y)
   Dim item As ToolStripButton = CType(Me.GetItemAt(Me.PointToClient(p)), ToolStripButton)
   
   
   ' If the ToolStripButton control is the empty cell,
   ' indicate that the move operation is valid.
     If item Is Me.emptyCellButton Then
         ' Set the drag operation to indicate a valid move.
         dea.Effect = DragDropEffects.Move
     End If
 End Sub

注解

在拖放操作过程中, 当用户首次将鼠标光标拖到项上时, 将引发事件。DragEnterThe DragEnter event is raised when the user first drags the mouse cursor over the item during a drag-and-drop operation.

以下备注说明了如何和何时引发与拖放操作相关的事件。The following remarks describe how and when events related to drag-and-drop operations are raised.

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

如果该项是有效的拖放目标, 则GiveFeedback会在指定了拖放效果的情况下引发事件。If the item 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 in the following manner:

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

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

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

如果键盘或鼠标按钮状态发生了更改, QueryContinueDrag则会引发事件, 并确定是继续拖动、删除数据还是根据事件的Action QueryContinueDragEventArgs属性的值来取消操作。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.

如果的DragAction值为Continue, 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, as follows:

  • 如果的DragAction值为Drop, 则放置效果值将返回到源, 以便源应用程序可以对源数据执行适当的操作; 例如, 如果是移动操作, 则剪切数据。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.

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

备注

XDragEventArgs属性处于屏幕坐标中, 而不是客户端坐标。 YThe X and Y properties of the DragEventArgs are in screen coordinates, not client coordinates.

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

适用于