DragEventArgs クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


ユーザーが、オブジェクトをコントロールの上にドラッグし、マウス ボタンを離してオブジェクトをコントロールにドロップしてドラッグ アンド ドロップ操作を完了すると、DragDrop イベントが発生します。ユーザーがマウスを使ってオブジェクトをドラッグしている間に、マウス ポインタがコントロール内に入ってくると、DragEnter イベントが発生します。ユーザーがマウスを使ってオブジェクトをドラッグしている間に、マウス ポインタがコントロール内を移動すると、DragOver イベントが発生します。
DragEventArgs オブジェクトはこのイベントに関連付けられているデータ、Shift、Ctrl、Alt の各キーの状態、マウス ポインタの位置、ドラッグ イベントのソースとターゲットで可能なドラッグ アンド ドロップ効果などを指定します。

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 イベント ハンドラでキャンセルされます。
DragEventArgs クラスの使用方法を次のコード例に示します。コード例全体については、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
DragEventArgs が、ドラッグ アンド ドロップ操作のソースとターゲットの間で渡される例を次に示します。この例では、ListBox コントロールがデータのソース、RichTextBox コントロールがターゲットです。この例は、ListBox コントロールが有効なファイル名の一覧に読み込まれていることを前提にしています。ユーザーが表示されているファイル名の 1 つを ListBox コントロールから RichTextBox コントロールにドラッグすると、ファイル名が参照しているファイルを開きます。
ListBox コントロールの MouseDown イベントで操作が開始されます。DragEnter イベント ハンドラでは、GetDataPresent メソッドを使用して、RichTextBox コントロールが表示できるデータ形式かどうかを検証します。次に DragDropEffects プロパティを設定して、データがソース コントロールからターゲット コントロールにコピーされるように指定します。最後に、RichTextBox コントロールの DragDrop イベント ハンドラで、GetData メソッドを使用して開くファイル名を取得します。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Sets the AllowDrop property so that data can be dragged onto the control. RichTextBox1.AllowDrop = True ' Add code here to populate the ListBox1 with paths to text files. End Sub Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter ' If the data is text, copy the data to the RichTextBox control. If (e.Data.GetDataPresent("Text")) Then e.Effect = DragDropEffects.Copy End If End Sub Private Overloads Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop ' Loads the file into the control. RichTextBox1.LoadFile(e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText) End Sub Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown Dim Lb As ListBox Dim Pt As New Point(e.X, e.Y) Dim Index As Integer ' Determines which item was selected. Lb = sender Index = Lb.IndexFromPoint(Pt) ' Starts a drag-and-drop operation with that item. If Index >= 0 Then Lb.DoDragDrop(Lb.Items(Index), DragDropEffects.Link) End If End Sub
private void Form1_Load(object sender, EventArgs e) { // Sets the AllowDrop property so that data can be dragged onto the control. richTextBox1.AllowDrop = true; // Add code here to populate the ListBox1 with paths to text files. } private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { // Determines which item was selected. ListBox lb =( (ListBox)sender); Point pt = new Point(e.X,e.Y); int index = lb.IndexFromPoint(pt); // Starts a drag-and-drop operation with that item. if(index>=0) { lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link); } } private void richTextBox1_DragEnter(object sender, DragEventArgs e) { // If the data is text, copy the data to the RichTextBox control. if(e.Data.GetDataPresent("Text")) e.Effect = DragDropEffects.Copy; } private void richTextBox1_DragDrop(object sender, DragEventArgs e) { // Loads the file into the control. richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText); }
private: void Form1_Load( Object^ /*sender*/, EventArgs^ /*e*/ ) { // Sets the AllowDrop property so that data can be dragged onto the control. richTextBox1->AllowDrop = true; // Add code here to populate the ListBox1 with paths to text files. } void listBox1_MouseDown( Object^ sender, System::Windows::Forms::MouseEventArgs^ e ) { // Determines which item was selected. ListBox^ lb = (dynamic_cast<ListBox^>(sender)); Point pt = Point(e->X,e->Y); int index = lb->IndexFromPoint( pt ); // Starts a drag-and-drop operation with that item. if ( index >= 0 ) { lb->DoDragDrop( lb->Items[ index ], DragDropEffects::Link ); } } void richTextBox1_DragEnter( Object^ /*sender*/, DragEventArgs^ e ) { // If the data is text, copy the data to the RichTextBox control. if ( e->Data->GetDataPresent( "Text" ) ) e->Effect = DragDropEffects::Copy; } void richTextBox1_DragDrop( Object^ /*sender*/, DragEventArgs^ e ) { // Loads the file into the control. richTextBox1->LoadFile( dynamic_cast<String^>(e->Data->GetData( "Text" )), System::Windows::Forms::RichTextBoxStreamType::RichText ); }

System.EventArgs
System.Windows.Forms.DragEventArgs


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DragEventArgs コンストラクタ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Public Sub New ( _ data As IDataObject, _ keyState As Integer, _ x As Integer, _ y As Integer, _ allowedEffect As DragDropEffects, _ effect As DragDropEffects _ )
Dim data As IDataObject Dim keyState As Integer Dim x As Integer Dim y As Integer Dim allowedEffect As DragDropEffects Dim effect As DragDropEffects Dim instance As New DragEventArgs(data, keyState, x, y, allowedEffect, effect)
public DragEventArgs ( IDataObject data, int keyState, int x, int y, DragDropEffects allowedEffect, DragDropEffects effect )
public: DragEventArgs ( IDataObject^ data, int keyState, int x, int y, DragDropEffects allowedEffect, DragDropEffects effect )
public DragEventArgs ( IDataObject data, int keyState, int x, int y, DragDropEffects allowedEffect, DragDropEffects effect )
public function DragEventArgs ( data : IDataObject, keyState : int, x : int, y : int, allowedEffect : DragDropEffects, effect : DragDropEffects )
- allowedEffect
DragDropEffects 値の 1 つ。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DragEventArgs プロパティ

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

DragEventArgs メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

DragEventArgs メンバ
DragDrop イベント、DragEnter イベント、または DragOver イベントのデータを提供します。
DragEventArgs データ型で公開されるメンバを以下の表に示します。


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

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

- DragEventArgsのページへのリンク