共用方式為


Control.DragOver 事件

定義

發生於物件拖曳至控件的界限上時。

public:
 event System::Windows::Forms::DragEventHandler ^ DragOver;
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 控制項間的拖放操作。 範例中當拖曳動作開始時呼叫該 DoDragDrop 方法。 拖曳動作會開始,前提是滑鼠在事件中MouseDown移動SystemInformation.DragSize超過滑鼠位置。 此 IndexFromPoint 方法用於判定事件中 MouseDown 要拖曳項目的索引。

此範例也會示範如何針對拖放作業使用自定義數據指標。 此範例要求應用程式目錄中存在兩個游標檔案, 3dwarro.cur3dwno.cur分別用於自訂拖曳游標和不可放下游標。 如果 UseCustomCursorsCheckCheckBox 被勾選,就會使用自訂游標。 自訂游標則在事件處理程序中 GiveFeedback 設定。

鍵盤狀態會在事件處理程序中評估 DragOver 右鍵 ListBox,根據 SHIFT、CTRL、ALT 或 CTRL+ALT 鍵的狀態來決定拖曳操作。 ListBox掉落地點也會在事件中DragOver決定。 若要丟棄的資料不是 String,則 DragEventArgs.Effect 在 中 被設定為 NoneDragDropEffects。 最後,掉落狀態會顯示在 DropLocationLabelLabel

右側要丟棄 ListBox 的資料由事件處理程序決定 DragDrop ,並將 String 值加入適當位置 ListBox。 如果拖曳操作超出表單範圍,則拖放操作會在事件處理程序中被 QueryContinueDrag 取消。

這段程式碼摘錄展示了使用該 DragOver 事件的過程。 完整程式碼範例請參見方法。DoDragDrop

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 = CTRL + 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) )
   {
      // CTRL 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, 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 = CTRL + 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)
    {
        // CTRL 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 = CTRL + 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

        ' CTRL 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

備註

DragOver當滑鼠游標在拖放操作中移動到控制範圍內時,事件就會被觸發。

下列說明如何和何時引發與拖放作業相關的事件。

此方法決定 DoDragDrop 在當前游標位置下的控制。 然後它會檢查控件是否為有效的置放目標。

若控制點為有效的投放目標, GiveFeedback 事件會觸發並指定拖放效果。 拖放效果列表請參見 DragDropEffects 列舉。

追蹤滑鼠游標位置、鍵盤狀態和滑鼠按鈕狀態的變更。

  • 如果使用者移出視窗, DragLeave 事件就會被觸發。

  • 若滑鼠進入另一個控制項,該控制項的 會 DragEnter 提高。

  • 如果滑鼠移動但仍停留在同一控制範圍內, DragOver 事件會被觸發。

若鍵盤或滑鼠按鈕狀態有變化,QueryContinueDrag事件會被觸發,並根據事件QueryContinueDragEventArgs屬性的值Action決定是否繼續拖曳、資料下放或取消操作。

  • DragAction 值為 ContinueDragOver 則事件會被觸發以繼續操作,並 GiveFeedback 以新效果觸發事件,以便設定適當的視覺回饋。 有關有效落差效應的列表,請參閱列舉。DragDropEffects

    注意

    DragOverGiveFeedback事件會配對,當滑鼠移動到落點時,使用者會獲得最多 up-to-日期的滑鼠位置回饋。

  • 若 的值 DragActionDrop,則 drop effect 值會回傳給來源資料,因此來源應用程式可以對來源資料執行適當的操作;例如,如果操作是移動,則會切割資料。

  • 若 值 DragActionCancelDragLeave 則事件會被觸發。

    注意

    X Y和 的屬性DragEventArgs是顯示螢幕座標,不是客戶端座標。 以下一行 C# 程式碼將屬性轉換為用戶端 Point

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

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱