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將設None為。If 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.

ListBoxString ListBox在事件處理常式中決定要卸載的資料,並將值加入至中的適當位置。 DragDropThe 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.

  • 如果滑鼠進入另一個控制項,便會引發該控制項的 DragEnter 事件。If 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.

  • 如果 value 的DragAction值為ContinueDragOverGiveFeedback會引發事件以繼續作業,並以新的效果引發事件,以便設定適當的視覺回應。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.

  • 如果的值DragActionDrop,卸載效果值就會傳回來源,因此來源應用程式可以對來源資料執行適當的作業; 例如,如果作業是移動時,則剪下資料。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.

  • 如果的值DragActionCancelDragLeave就會引發事件。If the value of DragAction is Cancel, the DragLeave event is raised.

    注意

    XY 屬性DragEventArgs是在螢幕座標中,而不是用戶端座標。The 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.

適用於

另請參閱