Control.DragOver Control.DragOver Control.DragOver Control.DragOver Event

定义

在将对象拖到控件的边界上发生。Occurs when an object is dragged over the control's bounds.

public:
 event System::Windows::Forms::DragEventHandler ^ DragOver;
public event System.Windows.Forms.DragEventHandler DragOver;
member this.DragOver : System.Windows.Forms.DragEventHandler 
Public Custom Event DragOver As DragEventHandler 

示例

下面的代码示例演示两个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. 如果在SystemInformation.DragSize MouseDown事件过程中鼠标移动了多个鼠标位置,则拖动操作将启动。The drag action starts if the mouse has moved more than SystemInformation.DragSize from the mouse location during the MouseDown event. 方法用于确定MouseDown事件期间要拖动的项的索引。 IndexFromPointThe 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. 该示例要求在应用程序目录中3dwarro.cur分别3dwno.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. UseCustomCursorsCheck 如果CheckBox选中,则将使用自定义光标。The custom cursors will be used if the UseCustomCursorsCheckCheckBox is checked. 自定义游标是在GiveFeedback事件处理程序中设置的。The custom cursors are set in the GiveFeedback event handler.

键盘状态在右侧DragOver ListBox的事件处理程序中进行计算,以根据 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. ListBox 事件DragOver期间也会确定放置过程中的位置。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. 最后,放置的状态显示在中DropLocationLabelLabelFinally, 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.

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

void ListDragTarget_DragOver( Object^ /*sender*/, System::Windows::Forms::DragEventArgs^ e )
{
   // Determine whether string data exists in the drop data. If not, then
   // the drop effect reflects that the drop cannot occur.
   if (  !e->Data->GetDataPresent( System::String::typeid ) )
   {
      e->Effect = DragDropEffects::None;
      DropLocationLabel->Text = "None - no string data.";
      return;
   }

   // Set the effect based upon the KeyState.
   if ( (e->KeyState & (8 + 32)) == (8 + 32) && ((e->AllowedEffect & DragDropEffects::Link) == DragDropEffects::Link) )
   {
      // KeyState 8 + 32 = CTL + ALT
      // Link drag-and-drop effect.
      e->Effect = DragDropEffects::Link;
   }
   else
   if ( (e->KeyState & 32) == 32 && ((e->AllowedEffect & DragDropEffects::Link) == DragDropEffects::Link) )
   {
      // ALT KeyState for link.
      e->Effect = DragDropEffects::Link;
   }
   else
   if ( (e->KeyState & 4) == 4 && ((e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move) )
   {
      // SHIFT KeyState for move.
      e->Effect = DragDropEffects::Move;
   }
   else
   if ( (e->KeyState & 8) == 8 && ((e->AllowedEffect & DragDropEffects::Copy) == DragDropEffects::Copy) )
   {
      // CTL KeyState for copy.
      e->Effect = DragDropEffects::Copy;
   }
   else
   if ( (e->AllowedEffect & DragDropEffects::Move) == DragDropEffects::Move )
   {
      // By default, the drop action should be move, if allowed.
      e->Effect = DragDropEffects::Move;
   }
   else
            e->Effect = DragDropEffects::None;





   
   // Get the index of the item the mouse is below.
   // The mouse locations are relative to the screen, so they must be
   // converted to client coordinates.
   indexOfItemUnderMouseToDrop = ListDragTarget->IndexFromPoint( ListDragTarget->PointToClient( Point(e->X,e->Y) ) );
   
   // Updates the label text.
   if ( indexOfItemUnderMouseToDrop != ListBox::NoMatches )
   {
      DropLocationLabel->Text = String::Concat( "Drops before item # ", (indexOfItemUnderMouseToDrop + 1) );
   }
   else
            DropLocationLabel->Text = "Drops at the end.";
}
private void ListDragTarget_DragOver(object sender, System.Windows.Forms.DragEventArgs e) 
{

    // Determine whether string data exists in the drop data. If not, then
    // the drop effect reflects that the drop cannot occur.
    if (!e.Data.GetDataPresent(typeof(System.String))) {

        e.Effect = DragDropEffects.None;
        DropLocationLabel.Text = "None - no string data.";
        return;
    }

    // Set the effect based upon the KeyState.
    if ((e.KeyState & (8+32)) == (8+32) && 
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) {
        // KeyState 8 + 32 = CTL + ALT

        // Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link;

    } else if ((e.KeyState & 32) == 32 && 
        (e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) {

        // ALT KeyState for link.
        e.Effect = DragDropEffects.Link;

    } else if ((e.KeyState & 4) == 4 && 
        (e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) {

        // SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move;

    } else if ((e.KeyState & 8) == 8 && 
        (e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) {

        // CTL KeyState for copy.
        e.Effect = DragDropEffects.Copy;

    } else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move)  {

        // By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move;

    } else
        e.Effect = DragDropEffects.None;
        
    // Get the index of the item the mouse is below. 

    // The mouse locations are relative to the screen, so they must be 
    // converted to client coordinates.

    indexOfItemUnderMouseToDrop = 
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y)));

    // Updates the label text.
    if (indexOfItemUnderMouseToDrop != ListBox.NoMatches){

        DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1);
    } else
        DropLocationLabel.Text = "Drops at the end.";

}
Private Sub ListDragTarget_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragOver
    ' Determine whether string data exists in the drop data. If not, then
    ' the drop effect reflects that the drop cannot occur.
    If Not (e.Data.GetDataPresent(GetType(System.String))) Then

        e.Effect = DragDropEffects.None
        DropLocationLabel.Text = "None - no string data."
        Return
    End If

    ' Set the effect based upon the KeyState.
    If ((e.KeyState And (8 + 32)) = (8 + 32) And _
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then
        ' KeyState 8 + 32 = CTL + ALT

        ' Link drag-and-drop effect.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 32) = 32 And _
        (e.AllowedEffect And DragDropEffects.Link) = DragDropEffects.Link) Then

        ' ALT KeyState for link.
        e.Effect = DragDropEffects.Link

    ElseIf ((e.KeyState And 4) = 4 And _
        (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' SHIFT KeyState for move.
        e.Effect = DragDropEffects.Move

    ElseIf ((e.KeyState And 8) = 8 And _
        (e.AllowedEffect And DragDropEffects.Copy) = DragDropEffects.Copy) Then

        ' CTL KeyState for copy.
        e.Effect = DragDropEffects.Copy

    ElseIf ((e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move) Then

        ' By default, the drop action should be move, if allowed.
        e.Effect = DragDropEffects.Move

    Else
        e.Effect = DragDropEffects.None
    End If

    ' Gets the index of the item the mouse is below. 

    ' The mouse locations are relative to the screen, so they must be 
    ' converted to client coordinates.

    indexOfItemUnderMouseToDrop = _
        ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(New Point(e.X, e.Y)))

    ' Updates the label text.
    If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then

        DropLocationLabel.Text = "Drops before item #" & (indexOfItemUnderMouseToDrop + 1)
    Else
        DropLocationLabel.Text = "Drops at the end."
    End If

End Sub

注解

在拖放操作过程中,当鼠标光标移到控件的边界内时,会引发事件。DragOverThe DragOver event is raised when the mouse cursor moves within the bounds of the control during a 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则会引发事件,并确定是继续拖动、删除数据还是根据事件的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 value 的值为ContinueDragOver则会引发事件以继续操作,并会以GiveFeedback新的效果引发事件,以便可以设置适当的视觉反馈。If the value of DragAction value 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.

  • 如果的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值为CancelDragLeave则引发事件。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. 以下C#代码行将属性转换为客户端PointThe following line of C# code converts the properties to a client Point:

    Point clientPoint = targetControl.PointToClient(new Point(de.X, de.Y));Point clientPoint = targetControl.PointToClient(new Point(de.X, de.Y));

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

适用于

另请参阅