DragEventArgs 类
为 DragDrop、DragEnter 或 DragOver 事件提供数据。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
<ComVisibleAttribute(True)> _
Public Class DragEventArgs
Inherits EventArgs
用法
Dim instance As DragEventArgs
[ComVisibleAttribute(true)]
public class DragEventArgs : EventArgs
[ComVisibleAttribute(true)]
public ref class DragEventArgs : public EventArgs
/** @attribute ComVisibleAttribute(true) */
public class DragEventArgs extends EventArgs
ComVisibleAttribute(true)
public class DragEventArgs extends EventArgs
备注
当用户通过将某个对象拖到控件上方,然后释放鼠标键将它放置在该控件上来完成拖放操作时,将发生 DragDrop 事件。当用户在使用鼠标拖动某个对象的过程中,将鼠标指针移动到控件上时,将发生 DragEnter 事件。当用户在使用鼠标拖动某个对象的过程中,将鼠标指针拖过控件时,将发生 DragOver 事件。
DragEventArgs 对象指定与该事件关联的任何数据;Shift、Ctrl 和 Alt 键的当前状态;鼠标指针的位置;以及拖动事件的源和目标所允许的拖放效果。
有关事件模型的信息,请参见 事件和委托。
示例
下面的示例演示了在两个 ListBox 控件间的拖放操作。当拖动动作启动时,该示例调用 DoDragDrop 方法。在 MouseDown 事件期间,如果从鼠标位置起鼠标移动的距离大于 SystemInformation.DragSize,则启动拖动动作。IndexFromPoint 方法用于在 MouseDown 事件期间确定要拖动的项的索引。
该示例同时演示了对拖放操作使用自定义光标。示例假定应用程序目录中存在两个光标文件:3dwarro.cur
和 3dwno.cur
,分别用于自定义拖动光标和禁止停放光标。如果选中 UseCustomCursorsCheck
CheckBox,则使用自定义光标。自定义光标在 GiveFeedback 事件处理程序中设置。
键盘状态在右 ListBox 的 DragOver 事件处理程序中计算,以根据 Shift、Ctrl、Alt 或 Ctrl+Alt 键的状态确定将发生哪种拖动操作。放置动作在 ListBox 中发生的位置也在 DragOver 事件期间确定。如果要放置的数据不是一个 String,则 DragEventArgs.Effect 将被设置为 DragDropEffects.None。最后,停放状态在 DropLocationLabel
Label 中显示。
要放置的用于右 ListBox 的数据在 DragDrop 事件处理程序中确定,并且在 ListBox 中的适当位置添加该 String 值。如果拖动操作移动到窗体边框的外面,则 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 控件已经用有效的文件名列表进行了填充。当用户将显示的某个文件名从 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.Object
System.EventArgs
System.Windows.Forms.DragEventArgs
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
DragEventArgs 成员
System.Windows.Forms 命名空间
DragDropEffects 枚举
OnDragDrop
Control.DragDrop 事件
OnDragEnter
Control.DragEnter 事件
OnDragOver
Control.DragOver 事件