DragEventArgs DragEventArgs DragEventArgs DragEventArgs Class

定義

提供 DragDropDragEnterDragOver 事件的資料。Provides data for the DragDrop, DragEnter, or DragOver event.

public ref class DragEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class DragEventArgs : EventArgs
type DragEventArgs = class
    inherit EventArgs
Public Class DragEventArgs
Inherits EventArgs
繼承
DragEventArgsDragEventArgsDragEventArgsDragEventArgs
衍生
屬性

範例

下列範例示範兩個ListBox控制項之間的拖放作業。The following example demonstrates a drag-and-drop operation between two ListBox controls. 此範例會在DoDragDrop拖曳動作開始時呼叫方法。The example calls the DoDragDrop method when the drag action starts. 如果滑鼠在SystemInformation.DragSize MouseDown事件期間移動了滑鼠的位置, 則會開始拖曳動作。The drag action starts if the mouse has moved more than SystemInformation.DragSize from the mouse location during the MouseDown event. 方法是用來判斷MouseDown事件期間要拖曳的專案索引。 IndexFromPointThe IndexFromPoint method is used to determine the index of the item to drag during the MouseDown event.

此範例也會示範如何針對拖放作業使用自訂資料指標。The example also demonstrates using custom cursors for the drag-and-drop operation. 此範例假設應用程式目錄中有3dwarro.cur3dwno.cur個數據指標檔案, 分別用於自訂拖放和不放入游標。The example assumes that two cursor files, 3dwarro.cur and 3dwno.cur, exist in the application directory, for the custom drag and no-drop cursors, respectively. UseCustomCursorsCheck 如果CheckBox已核取, 則會使用自訂資料指標。The custom cursors will be used if the UseCustomCursorsCheckCheckBox is checked. 自訂資料指標是在GiveFeedback事件處理常式中設定。The custom cursors are set in the GiveFeedback event handler.

鍵盤狀態會在右側DragOver ListBox的事件處理常式中進行評估, 以根據 SHIFT、ctrl、ALT 或 CTRL + ALT 鍵的狀態來判斷拖曳作業的依據。The keyboard state is evaluated in the DragOver event handler for the right ListBox, to determine what the drag operation will be based upon state of the SHIFT, CTRL, ALT, or CTRL+ALT keys. 在中ListBox , 放置的位置也會在DragOver事件期間決定。The location in the ListBox where the drop would occur is also determined during the DragOver event. 如果要捨棄的資料不String是, DragEventArgs.Effect則會設定為DragDropEffects.NoneIf the data to drop is not a String, then the DragEventArgs.Effect is set to DragDropEffects.None. 最後, 放置的狀態會顯示在中DropLocationLabelLabelFinally, the status of the drop is displayed in the DropLocationLabelLabel.

ListBoxString ListBox在事件處理常式中決定要卸載的資料, 並將值加入至中的適當位置。 DragDropThe data to drop for the right ListBox is determined in the DragDrop event handler and the String value is added at the appropriate place in the ListBox. 如果拖曳作業移到表單的範圍外, 則會在QueryContinueDrag事件處理常式中取消拖放作業。If the drag operation moves outside the bounds of the form, then the drag-and-drop operation is canceled in the QueryContinueDrag event handler.

這段程式碼摘錄會DragEventArgs示範如何使用類別。This code excerpt demonstrates using the DragEventArgs class. 如需完整的程式碼範例, 請參閱方法。DoDragDropSee the DoDragDrop method for the complete code example.

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

}
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

下列範例說明如何DragEventArgs在拖放作業的來源和目標之間傳遞。The following example illustrates how DragEventArgs are passed between the source and target of a drag-and-drop operation. 在此範例中, ListBox控制項是資料的來源, RichTextBox而控制項則是目標。In this example, a ListBox control is the source of the data, and the RichTextBox control is the target. 此範例假設ListBox控制項已填入有效檔案名的清單。The example assumes that the ListBox control has been populated with a list of valid filenames. 當使用者將其中一個顯示的檔案名從ListBox控制項RichTextBox拖曳至控制項時, 檔案名中所參考的檔案就會開啟。When the user drags one of the displayed filenames from the ListBox control onto the RichTextBox control, the file referenced in the filename is opened.

作業會在ListBox控制項的 MouseDown 事件中起始。The operation is initiated in the ListBox control's MouseDown event. 在事件處理常式中, 此範例會GetDataPresent使用方法來驗證資料是否為RichTextBox控制項可以顯示的格式, 然後設定DragDropEffects屬性以指定應該從原始檔控制複製資料。 DragEnter目標控制項。In the DragEnter event handler, the example uses the GetDataPresent method to verify that the data is in a format that the RichTextBox control can display and then sets the DragDropEffects property to specify that data should be copied from the source control to the target control. 最後, RichTextBox控制項的 system.windows.dragdrop.drop> 事件處理常式會GetData使用方法來取出要開啟的檔案名。Finally, the RichTextBox control's DragDrop event handler uses the GetData method to retrieve the filename to open.

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 );
   }
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 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

備註

DragDrop使用者完成拖放作業時, 將物件拖曳至控制項上, 然後放開滑鼠按鍵, 將其拖放到控制項上時, 就會發生此事件。The DragDrop event occurs when the user completes a drag-and-drop operation by dragging an object over the control and then dropping it onto the control by releasing the mouse button. DragEnter使用者將滑鼠指標移至控制項, 同時使用滑鼠拖曳物件時, 就會發生此事件。The DragEnter event occurs when the user moves the mouse pointer onto the control while dragging an object with the mouse. DragOver使用者將滑鼠指標移到控制項上方, 同時使用滑鼠拖曳物件時, 就會發生此事件。The DragOver event occurs when the user moves the mouse pointer over the control while dragging an object with the mouse.

DragEventArgs物件會指定與這個事件相關聯的任何資料、SHIFT、CTRL 和 ALT 鍵的目前狀態、滑鼠指標的位置, 以及拖曳事件的來源和目標所允許的拖放效果。A DragEventArgs object specifies any data associated with this event; the current state of the SHIFT, CTRL, and ALT keys; the location of the mouse pointer; and the drag-and-drop effects allowed by the source and target of the drag event.

如需事件模型的詳細資訊, 請參閱處理和引發事件For information about the event model, see Handling and Raising Events.

建構函式

DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects) DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects) DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects) DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects)

初始化 DragEventArgs 類別的新執行個體。Initializes a new instance of the DragEventArgs class.

屬性

AllowedEffect AllowedEffect AllowedEffect AllowedEffect

取得原始 (或來源) 拖曳事件所允許的拖放作業。Gets which drag-and-drop operations are allowed by the originator (or source) of the drag event.

Data Data Data Data

取得包含與這個事件相關聯的 IDataObject 資料。Gets the IDataObject that contains the data associated with this event.

Effect Effect Effect Effect

取得或設定拖放作業中的目標置放效果。Gets or sets the target drop effect in a drag-and-drop operation.

KeyState KeyState KeyState KeyState

取得 SHIFT、CTRL 和 ALT 鍵的目前狀態,以及滑鼠按鍵的狀態。Gets the current state of the SHIFT, CTRL, and ALT keys, as well as the state of the mouse buttons.

X X X X

取得滑鼠指標的 X 座標 (在螢幕座標中)。Gets the x-coordinate of the mouse pointer, in screen coordinates.

Y Y Y Y

取得滑鼠指標的 Y 座標 (在螢幕座標中)。Gets the y-coordinate of the mouse pointer, in screen coordinates.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱