Control.DragEnter Control.DragEnter Control.DragEnter Control.DragEnter Event

定義

發生於將物件拖曳至控制項邊框時。Occurs when an object is dragged into the control's bounds.

public:
 event System::Windows::Forms::DragEventHandler ^ DragEnter;
public event System.Windows.Forms.DragEventHandler DragEnter;
member this.DragEnter : System.Windows.Forms.DragEventHandler 
Public Custom Event DragEnter 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. IndexFromPoint方法用來判斷要在拖曳的項目索引MouseDown事件。The 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.cur3dwno.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. 會使用自訂的資料指標,如果UseCustomCursorsCheckCheckBox已核取。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設為NoneDragDropEffectsIf the data to drop is not a String, then the DragEventArgs.Effect is set to None in DragDropEffects. 最後,卸除的狀態會顯示在DropLocationLabel LabelFinally, the status of the drop is displayed in the DropLocationLabelLabel.

若要卸除提供的權限的資料ListBox決定DragDrop事件處理常式和String值會加入適當的位置中ListBoxThe 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.

此程式碼摘錄示範使用DragEnter事件。This code excerpt demonstrates using the DragEnter event. 請參閱DoDragDrop完整的程式碼範例中的方法。See the DoDragDrop method for the complete code example.

void ListDragTarget_DragEnter( Object^ /*sender*/, System::Windows::Forms::DragEventArgs^ /*e*/ )
{
   // Reset the label text.
   DropLocationLabel->Text = "None";
}
private void ListDragTarget_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
    // Reset the label text.
    DropLocationLabel.Text = "None";
}
Private Sub ListDragTarget_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragEnter

    ' Reset the label text.
    DropLocationLabel.Text = "None"
End Sub

備註

DragEnter當使用者第一次將滑鼠游標停留在控制項拖放作業期間,會引發事件。The DragEnter event is raised when the user first drags the mouse cursor over the control during a drag-and-drop operation.

注意

在版本早於.NET Framework 2.0.NET Framework 2.0,如果您將UserControlDragEnterDragDropWindows Form 上的事件並拖到的項目UserControl在設計階段,DropDropDropEnter事件引發。In versions earlier than .NET Framework 2.0.NET Framework 2.0, if you put a UserControl with DragEnter and DragDrop events on a Windows Form and drag and drop something onto the UserControl at design time, the DropDrop and DropEnter events are raised. 不過,當您關閉並重新開啟方案,DragEnterDragDrop不會再次引發事件。However, when you close and reopen the solution, the DragEnter and DragDrop events are not raised again.

以下描述與拖放作業相關的事件,其引發的方法與時機。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事件的屬性QueryContinueDragEventArgsIf 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.

  • 如果的值DragActionContinue,則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.

  • 如果值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.

  • 如果值DragActionCancel,則DragLeave就會引發事件。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. Visual C# 程式碼的下一行會將屬性轉換為用戶端PointThe following line of Visual C# code converts the properties to a client Point.

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

如需有關如何處理事件的詳細資訊,請參閱 < 處理和引發事件For more information about handling events, see Handling and Raising Events.

適用於

另請參閱