Control.DragOver イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

public: event DragEventHandler^ DragOver { void add (DragEventHandler^ value); void remove (DragEventHandler^ value); }

DragOver イベントは、ドラッグ アンド ドロップ操作中、マウス カーソルがコントロールの範囲内で移動するときに発生します。
ドラッグ アンド ドロップ操作関連のイベントがどのように、いつ発生するかについて次に示します。
DoDragDrop メソッドは、現在のカーソル位置に従ってコントロールを判別します。次に、コントロールが有効なドロップ ターゲットかどうかを確認します。
コントロールが有効なドロップ ターゲットの場合、指定したドラッグ アンド ドロップの効果が付いた状態で GiveFeedback イベントが発生します。ドラッグ アンド ドロップ効果の一覧については、DragDropEffects 列挙体を参照してください。
マウス カーソルの位置、キーボードの状態、およびマウス ボタンの状態の変更が監視されます。
キーボードまたはマウス ボタンの状態に変更があった場合、QueryContinueDrag イベントが生成され、このイベントの QueryContinueDragEventArgs の Action プロパティの値に応じて、ドラッグを継続するか、データをドロップするか、操作をキャンセルするかが判断されます。
-
DragAction の値が Continue の場合、操作を継続するための DragOver イベントが生成され、適切な視覚的フィードバックを設定できるように新しい効果による GiveFeedback イベントが生成されます。有効なドロップ効果の一覧については、DragDropEffects 列挙体を参照してください。
メモ
DragOver イベントおよび GiveFeedback イベントはペアになっています。したがって、マウスがドロップ ターゲットをまたがって移動した場合、マウスの位置に関する最新のフィードバックがユーザーに提示されます。
-
DragAction の値が Drop の場合、ドロップ効果の値がソースに返されます。ソース アプリケーションでは、ソース データに対して適切な操作を実行できます。たとえば、移動操作だった場合は、データの切り取りなどを実行できます。

2 つの ListBox コントロールの間でドラッグ アンド ドロップ操作を実行するコード例を次に示します。この例では、ドラッグ アクションが開始したときに DoDragDrop メソッドが呼び出されます。ドラッグ操作は、MouseDown イベント実行中のマウス位置から SystemInformation.DragSize を超えてマウスが移動したときに開始されます。IndexFromPoint メソッドは、MouseDown イベントで、ドラッグする項目のインデックスを判別するために使用します。
この例では、ドラッグ アンド ドロップ操作でカスタム カーソルを使用する方法についても示します。この例では、2 つのカーソル ファイル (3dwarro.cur と 3dwno.cur) がアプリケーション ディレクトリ内に存在している必要があります。なお、それぞれのファイルはドラッグ用のカスタム カーソルとドロップなしのカスタム カーソルを表します。カスタム カーソルは、UseCustomCursorsCheckCheckBox がオンになっている場合に使用されます。カスタム カーソルは、GiveFeedback イベント ハンドラで設定されます。
キーボードの状態は、右側の ListBox の DragOver イベント ハンドラで評価されます。ドラッグ操作の内容は、Shift キー、Ctrl キー、Alt キー、または Ctrl + Alt キーの状態によって決まります。ドロップが発生する ListBox 内の位置は、DragOver イベント時にも判定されます。ドロップするデータが String でない場合は、DragEventArgs.Effect が DragDropEffects で None に設定されます。最後に、ドロップのステータスが 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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からControl.DragOver イベントを検索する場合は、下記のリンクをクリックしてください。

- Control.DragOver イベントのページへのリンク