Control.GiveFeedback 事件

定義

發生於拖曳作業時。Occurs during a drag operation.

public:
 event System::Windows::Forms::GiveFeedbackEventHandler ^ GiveFeedback;
public event System.Windows.Forms.GiveFeedbackEventHandler GiveFeedback;
member this.GiveFeedback : System.Windows.Forms.GiveFeedbackEventHandler 
Public Custom Event GiveFeedback As GiveFeedbackEventHandler 

範例

下列程式碼範例示範兩個 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. 當滑鼠在 MouseDown 事件期間,從滑鼠位置移動超過 SystemInformation.DragSize 時,就會啟動拖曳動作。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.

鍵盤狀態會在右側 ListBoxDragOver 事件處理常式中進行評估,以根據 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. 最後,放置的狀態會顯示在 DropLocationLabelLabel中。Finally, the status of the drop is displayed in the DropLocationLabelLabel.

會在 DragDrop 事件處理常式中決定要卸載的 ListBox 的資料,而 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.

這段程式碼摘錄會示範如何使用 GiveFeedback 事件。This code excerpt demonstrates using the GiveFeedback event. 如需完整的程式碼範例,請參閱 DoDragDrop 方法。See the DoDragDrop method for the complete code example.

void ListDragSource_GiveFeedback( Object^ /*sender*/, System::Windows::Forms::GiveFeedbackEventArgs^ e )
{
   // Use custom cursors if the check box is checked.
   if ( UseCustomCursorsCheck->Checked )
   {
      // Sets the custom cursor based upon the effect.
      e->UseDefaultCursors = false;
      if ( (e->Effect & DragDropEffects::Move) == DragDropEffects::Move )
                  ::Cursor::Current = MyNormalCursor;
      else
                  ::Cursor::Current = MyNoDropCursor;
   }
}
private void ListDragSource_GiveFeedback(object sender, System.Windows.Forms.GiveFeedbackEventArgs e) 
{
    // Use custom cursors if the check box is checked.
    if (UseCustomCursorsCheck.Checked) {

        // Sets the custom cursor based upon the effect.
        e.UseDefaultCursors = false;
        if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
            Cursor.Current = MyNormalCursor;
        else 
            Cursor.Current = MyNoDropCursor;
    }

}
Private Sub ListDragSource_GiveFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles ListDragSource.GiveFeedback

    ' Use custom cursors if the check box is checked.
    If (UseCustomCursorsCheck.Checked) Then

        ' Set the custom cursor based upon the effect.
        e.UseDefaultCursors = False
        If ((e.Effect And DragDropEffects.Move) = DragDropEffects.Move) Then
            Cursor.Current = MyNormalCursor
        Else
            Cursor.Current = MyNoDropCursor
        End If
    End If

End Sub

備註

當拖放作業啟動時,就會引發 GiveFeedback 事件。The GiveFeedback event is raised when a drag-and-drop operation is started. 使用 GiveFeedback 事件時,拖曳事件的來源可以修改滑鼠指標的外觀,以便在拖放作業期間為使用者提供視覺效果的意見反應。With the GiveFeedback event, the source of a drag event can modify the appearance of the mouse pointer in order to give the user visual feedback 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 事件,並決定是否要繼續拖曳、卸載資料,或根據事件 QueryContinueDragEventArgsAction 屬性值來取消作業。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 的值為 Continue,則會引發 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.

  • 如果 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 的值為 Cancel,則會引發 DragLeave 事件。If the value of DragAction is Cancel, the DragLeave event is raised.

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

適用於

另請參閱