Control.DragOver イベント

定義

オブジェクトがコントロールの境界を越えてドラッグされると発生します。

public:
 event System::Windows::Forms::DragEventHandler ^ DragOver;
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 

イベントの種類

次のコード例は、2 つの ListBox コントロール間のドラッグ アンド ドロップ操作を示しています。 この例では、ドラッグ アクションの DoDragDrop 開始時に メソッドを呼び出します。 ドラッグ 操作は、イベント中にマウスの位置からマウスが移動 SystemInformation.DragSize した場合に MouseDown 開始されます。 メソッドは IndexFromPoint 、イベント中にドラッグする項目のインデックスを MouseDown 決定するために使用されます。

この例では、ドラッグ アンド ドロップ操作にカスタム カーソルを使用する方法も示します。 この例では、 3dwarro.cur カスタム ドラッグ カーソルと 3dwno.curドロップなしのカーソルに対して、それぞれ 2 つのカーソル ファイル と がアプリケーション ディレクトリに存在する必要があります。 がオンになっている場合 UseCustomCursorsCheckCheckBox は、カスタム カーソルが使用されます。 カスタム カーソルは、イベント ハンドラーで GiveFeedback 設定されます。

キーボードの状態は、右 ListBoxのイベント ハンドラーでDragOver評価され、Shift キー、Ctrl キー、Alt キー、または Ctrl + Alt キーの状態に基づいてドラッグ操作が決定されます。 ドロップが発生する内の ListBox 場所も、イベント中に DragOver 決定されます。 削除するデータが でないStringDragEventArgs.Effect場合、 は で DragDropEffectsNone設定されます。 最後に、ドロップの状態が に DropLocationLabelLabel表示されます。

右側 ListBox にドロップするデータはイベント ハンドラーで DragDrop 決定され、 内の String 適切な場所に値が ListBox追加されます。 ドラッグ操作がフォームの境界外に移動した場合、ドラッグ アンド ドロップ操作はイベント ハンドラーで QueryContinueDrag 取り消されます。

このコードの抜粋は、 イベントの使用を DragOver 示しています。 完全な DoDragDrop コード例については、 メソッドを参照してください。

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 = CTRL + 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) )
   {
      // CTRL 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, 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 = CTRL + 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)
    {
        // CTRL 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 = CTRL + 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

        ' CTRL 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

注釈

このイベントは DragOver 、ドラッグ アンド ドロップ操作中にマウス カーソルがコントロールの境界内を移動したときに発生します。

ドラッグ アンド ドロップ操作関連のイベントがどのように、いつ発生するかについて次に示します。

メソッドは DoDragDrop 、現在のカーソル位置の下のコントロールを決定します。 次に、コントロールが有効なドロップ ターゲットであるかどうかを確認します。

コントロールが有効なドロップ ターゲットの場合は、 GiveFeedback ドラッグ アンド ドロップ効果を指定してイベントが発生します。 ドラッグ アンド ドロップ効果の一覧については、DragDropEffects 列挙体を参照してください。

マウス カーソルの位置、キーボードの状態、およびマウス ボタンの状態の変更が監視されます。

  • ユーザーがウィンドウの外に移動した場合、DragLeave イベントが生成されます。

  • マウスが別のコントロールに移動した場合は、そのコントロールの DragEnter が生成されます。

  • マウスが移動しても同じコントロール内の場合は、DragOver イベントが生成されます。

キーボードまたはマウス ボタンの状態が変更された場合、イベントが発生し、QueryContinueDragドラッグを続行するか、データをドロップするか、イベントQueryContinueDragEventArgsの の プロパティの値に基づいて操作をActionキャンセルするかを決定します。

  • value の値が の DragAction 場合、 Continue操作を DragOver 続行するためにイベントが発生し、 GiveFeedback 適切な視覚的フィードバックを設定できるように、新しい効果でイベントが発生します。 有効なドロップ効果の一覧については、DragDropEffects 列挙体を参照してください。

    Note

    DragOverイベントと GiveFeedback イベントはペアになっているため、マウスがドロップ ターゲットを越えて移動すると、ユーザーにはマウスの位置に関する最新のフィードバックが与えられます。

  • DragAction 値が Dropの場合、ドロップ効果の値がソースに返されるため、ソース アプリケーションはソース データに対して適切な操作を実行できます。たとえば、操作が移動の場合はデータを切り取ります。

  • DragAction 値が の Cancel場合、イベントが発生します DragLeave

    Note

    Xの プロパティと Y プロパティDragEventArgsは、クライアント座標ではなく画面座標にあります。 次の C# コード行は、 プロパティをクライアント Pointに変換します。

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

イベントの処理の詳細については、「処理とイベントの発生」を参照してください。

適用対象

こちらもご覧ください