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 属性值确定是否继续拖动、放置数据或取消操作。
如果 DragAction 的值为 Continue,则引发 DragOver 事件来继续该操作,并引发 GiveFeedback 事件以产生新效果,从而能够设置适当的可视反馈。有关有效放置效果的列表,请参见 DragDropEffects 枚举。
提示
DragOver 和 GiveFeedback 事件成对出现,从而当鼠标从放置目标上移动时,就能够为用户提供有关鼠标位置的最新反馈。
如果 DragAction 的值为 Drop,放置效果值将恢复成源值,以便源应用程序可以对源数据执行适当的操作;例如,如果是移动操作,则剪切数据。
如果 DragAction 的值为 Cancel,将引发 DragLeave 事件。
提示
DragEventArgs 的 X 和 Y 属性在屏幕坐标中,而不是在工作区坐标中。下面的 C# 代码行将这些属性转换为工作区 Point:
有关处理事件的更多信息,请参见 使用事件。
示例
下面的代码示例演示在两个 ListBox 控件之间的拖放操作。当拖动动作启动时,该示例调用 DoDragDrop 方法。在 MouseDown 事件期间,如果从鼠标位置起鼠标移动的距离大于 SystemInformation.DragSize,则启动拖动动作。IndexFromPoint 方法用于在 MouseDown 事件期间确定要拖动的项的索引。
该示例同时演示了对拖放操作使用自定义光标。该示例要求应用程序目录中存在两个光标文件:3dwarro.cur
和 3dwno.cur
,分别用于自定义拖动光标和禁止停放光标。如果选中 UseCustomCursorsCheck
CheckBox,则使用自定义光标。自定义光标在 GiveFeedback 事件处理程序中设置。
键盘状态在右 ListBox 的 DragOver 事件处理程序中计算,以确定基于 Shift、Ctrl、Alt 或 Ctrl+Alt 键的状态将发生哪种拖动操作。放置动作在 ListBox 中发生的位置也在 DragOver 事件期间确定。如果要放置的数据不是 String,则 DragDropEffects 中将把 DragEventArgs.Effect 设置为 None。最后,停放状态在 DropLocationLabel
Label 中显示。
要放置的用于右 ListBox 的数据在 DragDrop 事件处理程序中确定,并且在 ListBox 中的适当位置添加该 String 值。如果拖动操作移动到窗体边框的外面,则 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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0