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 メソッドを呼び出します。 ドラッグ 操作は、MouseDown イベント中にマウスの位置からマウスが SystemInformation.DragSize 以上移動した場合に開始されます。 IndexFromPoint メソッドは、MouseDown イベント中にドラッグする項目のインデックスを決定するために使用されます。

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

キーボードの状態は、右 ListBoxDragOver イベント ハンドラーで評価され、Shift キー、Ctrl キー、Alt キー、または Ctrl + Alt キーの状態に基づいてドラッグ操作を決定します。 ドロップが発生する ListBox 内の場所も、DragOver イベント中に決定されます。 削除するデータが Stringでない場合、DragEventArgs.EffectDragDropEffectsNone に設定されます。 最後に、ドロップの状態が 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 イベントが発生し、ドラッグを続行するか、データをドロップするか、イベントの QueryContinueDragEventArgsAction プロパティの値に基づいて操作をキャンセルするかを決定します。

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

    手記

    DragOver イベントと GiveFeedback イベントはペアになっているため、マウスがドロップ ターゲットを越えて移動すると、ユーザーはマウスの位置に対して最も up-to-date フィードバックを受け取ります。

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

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

    手記

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

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

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

適用対象

こちらもご覧ください