Control.DragOver イベントとは? わかりやすく解説

Control.DragOver イベント

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

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Event DragOver As
 DragEventHandler
Dim instance As Control
Dim handler As DragEventHandler

AddHandler instance.DragOver, handler
public event DragEventHandler DragOver
public:
event DragEventHandler^ DragOver {
    void add (DragEventHandler^ value);
    void remove (DragEventHandler^ value);
}
/** @event */
public void add_DragOver (DragEventHandler
 value)

/** @event */
public void remove_DragOver (DragEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説

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

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

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

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

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

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

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

使用例使用例

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

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

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

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

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

}
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 = 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( "Drops before item # ",
 (indexOfItemUnderMouseToDrop + 1) );
   }
   else
            DropLocationLabel->Text = "Drops at the end.";
}
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.get_Data().GetDataPresent(String.class.ToType())))
 {
        e.set_Effect(DragDropEffects.None);
        dropLocationLabel.set_Text("None - no string data.");
        return;
    }
    // Set the effect based upon the KeyState.
    if ((e.get_KeyState() & 8 + 32) == 8 + 32 && (e.get_AllowedEffect()
 
        & DragDropEffects.Link) == DragDropEffects.Link) {
        // KeyState 8 + 32 = CTL + ALT
        // Link drag-and-drop effect.
        e.set_Effect(DragDropEffects.Link);
    }
    else {
        if ((e.get_KeyState() & 32) == 32 && (e.get_AllowedEffect()
 
            & DragDropEffects.Link) == DragDropEffects.Link) {
            // ALT KeyState for link.
            e.set_Effect(DragDropEffects.Link);
        }
        else {
            if ((e.get_KeyState() & 4) == 4 && (e.get_AllowedEffect()
 
                & DragDropEffects.Move) == DragDropEffects.Move) {
                // SHIFT KeyState for move.
                e.set_Effect(DragDropEffects.Move);
            }
            else {
                if ((e.get_KeyState() & 8) == 8 &&
 (e.get_AllowedEffect() 
                    & DragDropEffects.Copy) == DragDropEffects.Copy) {
                    // CTL KeyState for copy.
                    e.set_Effect(DragDropEffects.Copy);
                }
                else {
                    if ((e.get_AllowedEffect() & DragDropEffects.Move)
 
                        == DragDropEffects.Move) {
                        // By default, the drop action should be move,
 
                        //if allowed.
                        e.set_Effect(DragDropEffects.Move);
                    }
                    else {
                        e.set_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.get_X(), e.get_Y())));
    // Updates the label text.
    if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) {
        dropLocationLabel.set_Text("Drops before item #" 
            + (indexOfItemUnderMouseToDrop + 1));
    }
    else {
        dropLocationLabel.set_Text("Drops at the end.");
    }
} //listDragTarget_DragOver
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からControl.DragOver イベントを検索した結果を表示しています。
Weblioに収録されているすべての辞書からControl.DragOver イベントを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からControl.DragOver イベント を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Control.DragOver イベント」の関連用語

Control.DragOver イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Control.DragOver イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS