Control.QueryContinueDrag 事件

定義

發生於拖放作業時,讓拖曳來源能夠決定是否應取消拖放作業。Occurs during a drag-and-drop operation and enables the drag source to determine whether the drag-and-drop operation should be canceled.

public:
 event System::Windows::Forms::QueryContinueDragEventHandler ^ QueryContinueDrag;
public event System.Windows.Forms.QueryContinueDragEventHandler QueryContinueDrag;
member this.QueryContinueDrag : System.Windows.Forms.QueryContinueDragEventHandler 
Public Custom Event QueryContinueDrag As QueryContinueDragEventHandler 

範例

這段程式碼摘錄會示範如何使用 QueryContinueDrag 事件來取消拖放作業(如果拖曳作業移到表單的範圍外)。This code excerpt demonstrates using the QueryContinueDrag event to cancel the drag-and-drop operation if the drag operation moves outside the bounds of the form. 如需完整的程式碼範例,請參閱 DoDragDrop 方法。See the DoDragDrop method for the complete code example.

void ListDragSource_QueryContinueDrag( Object^ sender, System::Windows::Forms::QueryContinueDragEventArgs^ e )
{
   // Cancel the drag if the mouse moves off the form.
   ListBox^ lb = dynamic_cast<ListBox^>(sender);
   if ( lb != nullptr )
   {
      Form^ f = lb->FindForm();

      // Cancel the drag if the mouse moves off the form. The screenOffset
      // takes into account any desktop bands that may be at the top or left
      // side of the screen.
      if ( ((Control::MousePosition.X - screenOffset.X) < f->DesktopBounds.Left) || ((Control::MousePosition.X - screenOffset.X) > f->DesktopBounds.Right) || ((Control::MousePosition.Y - screenOffset.Y) < f->DesktopBounds.Top) || ((Control::MousePosition.Y - screenOffset.Y) > f->DesktopBounds.Bottom) )
      {
         e->Action = DragAction::Cancel;
      }
   }
}
private void ListDragSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e) {
    // Cancel the drag if the mouse moves off the form.
    ListBox lb = sender as ListBox;

    if (lb != null) {

        Form f = lb.FindForm();

        // Cancel the drag if the mouse moves off the form. The screenOffset
        // takes into account any desktop bands that may be at the top or left
        // side of the screen.
        if (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) ||
            ((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) ||
            ((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) ||
            ((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) {

            e.Action = DragAction.Cancel;
        }
    }
}
Private Sub ListDragSource_QueryContinueDrag(ByVal sender As Object, ByVal e As QueryContinueDragEventArgs) Handles ListDragSource.QueryContinueDrag
    ' Cancel the drag if the mouse moves off the form.
    Dim lb as ListBox = CType(sender, System.Windows.Forms.ListBox)

    If (lb isNot nothing) Then

        Dim f as Form = lb.FindForm()

        ' Cancel the drag if the mouse moves off the form. The screenOffset
        ' takes into account any desktop bands that may be at the top or left
        ' side of the screen.
        If (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) Or _
            ((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) Or _
            ((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) Or _
            ((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) Then

            e.Action = DragAction.Cancel
        End If
    End if
End Sub

備註

拖放操作期間,鍵盤或滑鼠按鍵狀態變更時,就會引發 QueryContinueDrag 事件。The QueryContinueDrag event is raised when there is a change in the keyboard or mouse button state during a drag-and-drop operation. QueryContinueDrag 事件可讓拖曳來源判斷是否應取消拖放作業。The QueryContinueDrag event enables the drag source to determine whether the drag-and-drop operation should be canceled.

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

根據預設,如果按下 ESC 鍵,則 QueryContinueDrag 事件會將 Action 設定為 DragAction 中的 Cancel,如果按下左、中或滑鼠右鍵,則在 DragAction 中將 Action 設定為 DropBy default, the QueryContinueDrag event sets Action to Cancel in DragAction if the ESC key was pressed and sets Action to Drop in DragAction if the left, middle, or right mouse button is pressed.

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

適用於

另請參閱