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 設定。

鍵盤狀態會在右側 ListBox 的事件處理常式中 DragOver 評估,以根據 SHIFT、CTRL、ALT 或 CTRL+ALT 鍵的狀態來判斷拖曳作業會是什麼。 ListBox在 事件發生期間 DragOver 也會決定卸載的位置。 如果要卸載的資料不是 String ,則會 DragEventArgs.Effect 在 中 DragDropEffects 設定為 None 。 最後,卸載的狀態會顯示在 中 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 引發事件,並判斷是否要繼續拖曳、卸載資料,或根據 Action 事件的 QueryContinueDragEventArgs 屬性值取消作業。

  • 如果值為 DragActionContinue ,則會 DragOver 引發 事件以繼續作業,並且 GiveFeedback 以新效果引發事件,因此可以設定適當的視覺回饋。 如需有效置放效果的清單,請參閱 DragDropEffects 列舉型別。

    注意

    DragOverGiveFeedback 事件會配對,如此一來,當滑鼠在置放目標之間移動時,使用者就會獲得滑鼠位置的最新意見反應。

  • 如果 的值 DragActionDrop ,則卸載效果值會傳回至來源,因此來源應用程式可以在來源資料上執行適當的作業;例如,如果作業是移動,請剪下資料。

  • 如果 的值 DragActionCancel ,就會 DragLeave 引發 事件。

    注意

    XDragEventArgsY 屬性位於螢幕座標中,而不是用戶端座標。 下列 C# 程式程式碼會將屬性轉換成用戶端 Point

    Point clientPoint = targetControl.PointToClient (新的 Point (de。X, de.Y) ) ;

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

適用於

另請參閱