Control.QueryContinueDrag 事件

定義

發生於拖放作業時,讓拖曳來源能夠決定是否應取消拖放作業。

public:
 event System::Windows::Forms::QueryContinueDragEventHandler ^ QueryContinueDrag;
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 事件取消拖放作業。 如需完整的程式碼範例, DoDragDrop 請參閱 方法。

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, 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, 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當拖放作業期間鍵盤或滑鼠按鍵狀態有所變更時,就會引發 事件。 事件 QueryContinueDrag 可讓拖曳來源判斷是否應該取消拖放作業。

以下描述與拖放作業相關的事件,其引發的方法與時機。

方法 DoDragDrop 會決定目前游標位置下的 控制項。 然後,它會檢查控制項是否為有效的置放目標。

如果控制項是有效的置放目標,則會 GiveFeedback 使用指定的拖放效果引發事件。 如需拖放效果的清單,請參閱 DragDropEffects 列舉型別。

系統會追蹤滑鼠游標位置、鍵盤狀態和滑鼠按鈕狀態的變更。

  • 如果使用者移出視窗外,便會引發 DragLeave 事件。

  • 如果滑鼠進入另一個控制項,便會引發該控制項的 DragEnter 事件。

  • 如果滑鼠移動,但是待在相同的控制項內,便會引發 DragOver 事件。

如果鍵盤或滑鼠按鍵狀態有變更, QueryContinueDrag 則會引發 事件,並判斷是否要繼續拖曳、卸載資料,或根據 Action 事件的 QueryContinueDragEventArgs 屬性值取消作業。

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

    注意

    DragOverGiveFeedback 事件會配對,如此一來,當滑鼠在置放目標上移動時,系統會提供使用者對滑鼠位置的最新意見反應。

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

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

根據預設, QueryContinueDrag 如果按下 ESC 鍵,事件會設定 ActionCancelDragAction 為 ,並在按下左鍵、中間鍵或滑鼠右鍵時設定 Action 為 。 DragActionDrop

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

適用於

另請參閱