Control.DragOver イベント

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

Public Event DragOver As DragEventHandler
[C#]
public event DragEventHandler DragOver;
[C++]
public: __event DragEventHandler* DragOver;

[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。

イベント データ

イベント ハンドラが、このイベントに関連するデータを含む、DragEventArgs 型の引数を受け取りました。次の DragEventArgs プロパティには、このイベントの固有の情報が記載されます。

プロパティ 説明
AllowedEffect ドラッグ イベントの元の場所 (またはソース) で実行できるドラッグ アンド ドロップ操作を取得します。
Data このイベントに関連付けられているデータを含む IDataObject を取得します。
Effect ドラッグ アンド ドロップ操作のターゲットのドロップ効果を取得または設定します。
KeyState マウス ボタンの状態と同様に、Shift、Ctrl、Alt の各キーの現在の状態を取得します。
X マウス ポインタの x 座標 (画面座標) を取得します。
Y マウス ポインタの y 座標 (画面座標) を取得します。

解説

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

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

DoDragDrop メソッドは、現在のカーソル位置に従ってコントロールを判別します。次に、コントロールが有効なドロップ ターゲットかどうかを確認します。

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

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

  • ユーザーがウィンドウの外に移動した場合、 DragLeave イベントが生成されます。
  • マウスが別のコントロールに移動した場合は、そのコントロールの DragEnter が生成されます。
  • マウスが移動しても同じコントロール内の場合は、 DragOver イベントが生成されます。

キーボードまたはマウス ボタンの状態に変更があった場合、 QueryContinueDrag イベントが生成され、このイベントの QueryContinueDragEventArgsAction プロパティの値に応じて、ドラッグを継続するか、データをドロップするか、操作をキャンセルするかが判断されます。

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

メモ    DragOver イベントおよび GiveFeedback イベントはペアになっています。したがって、マウスがドロップ ターゲットをまたがって移動した場合、マウスの位置に関する最新のフィードバックがユーザーに提示されます。

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

値が DragAction.Cancel の場合、 DragLeave イベントが生成されます。

メモ    DragEventArgsX プロパティおよび Y プロパティは、クライアント座標ではなく、画面座標の値です。次の C# のコードでは、これらのプロパティをクライアントの Point に変換します。

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

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

使用例

[Visual Basic, C#, C++] 2 つの ListBox コントロールの間でドラッグ アンド ドロップ操作を実行する例を次に示します。この例では、ドラッグ アクションが開始したときに DoDragDrop メソッドが呼び出されます。ドラッグ操作は、 MouseDown イベント実行中のマウス位置から SystemInformation.DragSize を超えてマウスが移動したときに開始されます。 IndexFromPoint メソッドは、 MouseDown イベントで、ドラッグする項目のインデックスを判別するために使用します。

[Visual Basic, C#, C++] この例では、ドラッグ アンド ドロップ操作でカスタム カーソルを使用する方法についても示します。この例では、2 つのカーソル ファイル (3dwarro.cur3dwno.cur) がアプリケーション ディレクトリ内に存在していることを想定しています。なお、それぞれのファイルはドラッグ用のカスタム カーソルとドロップなしのカスタム カーソルを表します。カスタム カーソルは、 UseCustomCursorsCheck CheckBox がオンになっている場合に使用されます。カスタム カーソルは、 GiveFeedback イベント ハンドラで設定されます。

[Visual Basic, C#, C++] キーボードの状態は、右側の ListBoxDragOver イベント ハンドラで評価されます。ドラッグ操作の内容は、Shift キー、Ctrl キー、Alt キー、または Ctrl + Alt キーの状態によって決まります。ドロップが発生する ListBox 内の位置は、 DragOver イベント時にも判定されます。ドロップするデータが String でない場合は、 DragEventArgs.EffectDragDropEffects.None に設定されます。最後に、ドロップのステータスが DropLocationLabel Label に表示されます。

[Visual Basic, C#, C++] 右側の ListBox にドロップするデータは、 DragDrop イベント ハンドラで判定されます。また、 String 値が ListBox の該当する場所に追加されます。ドラッグ操作がフォームの範囲を超えて移動した場合、ドラッグ アンド ドロップ操作は QueryContinueDrag イベント ハンドラでキャンセルされます。

[Visual Basic, C#, C++] DragOver イベントの使用方法を次のコード例に示します。コード例全体については、 DoDragDrop メソッドのトピックを参照してください。

 
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 = CTL + 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

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

[C#] 
private 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(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 = CTL + 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) {

        // CTL 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.";

}

[C++] 
private:
   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(__typeof(System::String))) {

         e->Effect = DragDropEffects::None;
         DropLocationLabel->Text = S"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 = CTL + 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)
      {
         // CTL 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(S"Drops before item # ",
            __box( (indexOfItemUnderMouseToDrop + 1)));
      } else
         DropLocationLabel->Text = S"Drops at the end.";

   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Control クラス | Control メンバ | System.Windows.Forms 名前空間 | OnDragOver