다음을 통해 공유


DragEventArgs 클래스

정의

DragDrop, DragEnter또는 DragOver 이벤트에 대한 데이터를 제공합니다.

public ref class DragEventArgs : EventArgs
[System.Runtime.InteropServices.ComVisible(true)]
public class DragEventArgs : EventArgs
public class DragEventArgs : EventArgs
[<System.Runtime.InteropServices.ComVisible(true)>]
type DragEventArgs = class
    inherit EventArgs
type DragEventArgs = class
    inherit EventArgs
Public Class DragEventArgs
Inherits EventArgs
상속
DragEventArgs
파생
특성

예제

다음 예제에서는 두 ListBox 컨트롤 간의 끌어서 놓기 작업을 보여 줍니다. 이 예제에서는 끌기 작업이 시작될 때 DoDragDrop 메서드를 호출합니다. 마우스가 MouseDown 이벤트 중에 마우스 위치에서 SystemInformation.DragSize 이상 이동한 경우 끌기 작업이 시작됩니다. IndexFromPoint 메서드는 MouseDown 이벤트 중에 끌 항목의 인덱스 확인에 사용됩니다.

또한 이 예제에서는 끌어서 놓기 작업에 사용자 지정 커서를 사용하는 방법을 보여 줍니다. 이 예제에서는 3dwarro.cur3dwno.cur두 개의 커서 파일이 각각 사용자 지정 끌어서 놓기 커서에 대해 애플리케이션 디렉터리에 있다고 가정합니다. UseCustomCursorsCheck CheckBox 선택하면 사용자 지정 커서가 사용됩니다. 사용자 지정 커서는 GiveFeedback 이벤트 처리기에 설정됩니다.

키보드 상태는 오른쪽 ListBox대한 DragOver 이벤트 처리기에서 평가되어 Shift, Ctrl, Alt 또는 Ctrl+Alt 키의 상태에 따라 끌기 작업을 결정합니다. 드롭이 발생하는 ListBox 위치도 DragOver 이벤트 중에 결정됩니다. 삭제할 데이터가 String아닌 경우 DragEventArgs.EffectDragDropEffects.None. 마지막으로 드롭 상태가 DropLocationLabelLabel표시됩니다.

오른쪽 ListBox 대해 삭제할 데이터는 DragDrop 이벤트 처리기에서 결정되며 String 값은 ListBox적절한 위치에 추가됩니다. 끌기 작업이 폼의 범위 밖으로 이동하면 QueryContinueDrag 이벤트 처리기에서 끌어서 놓기 작업이 취소됩니다.

이 코드 발췌에서는 DragEventArgs 클래스를 사용하는 방법을 보여 줍니다. 전체 코드 예제는 DoDragDrop 메서드를 참조하세요.

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 = CTRL + 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) )
   {
      // CTRL 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, 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 = CTRL + 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)
    {
        // CTRL 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 = CTRL + 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

        ' CTRL 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 전달되는 방법을 보여 줍니다. 이 예제에서 ListBox 컨트롤은 데이터의 원본이며 RichTextBox 컨트롤이 대상입니다. 이 예제에서는 ListBox 컨트롤이 유효한 파일 이름 목록으로 채워져 있다고 가정합니다. 사용자가 표시된 파일 이름 중 하나를 ListBox 컨트롤에서 RichTextBox 컨트롤로 끌면 파일 이름에서 참조되는 파일이 열립니다.

작업은 ListBox 컨트롤의 MouseDown 이벤트에서 시작됩니다. DragEnter 이벤트 처리기에서 예제에서는 GetDataPresent 메서드를 사용하여 데이터가 RichTextBox 컨트롤이 표시할 수 있는 형식인지 확인한 다음 DragDropEffects 속성을 설정하여 원본 컨트롤에서 대상 컨트롤로 데이터를 복사하도록 지정합니다. 마지막으로 RichTextBox 컨트롤의 DragDrop 이벤트 처리기는 GetData 메서드를 사용하여 열 파일 이름을 검색합니다.

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 이벤트는 사용자가 컨트롤 위로 개체를 끌어서 놓은 다음 마우스 단추를 놓아 컨트롤에 놓아 끌어서 놓는 작업을 완료할 때 발생합니다. DragEnter 이벤트는 마우스를 사용하여 개체를 끌 때 마우스 포인터를 컨트롤로 이동할 때 발생합니다. DragOver 이벤트는 사용자가 마우스로 개체를 끌 때 마우스 포인터를 컨트롤 위로 이동할 때 발생합니다.

DragEventArgs 개체는 이 이벤트와 연결된 모든 데이터를 지정합니다. Shift, Ctrl 및 Alt 키의 현재 상태입니다. 마우스 포인터의 위치입니다. 및 끌기 이벤트의 원본 및 대상에서 허용하는 끌어서 놓기 효과입니다.

이벤트 모델에 대한 자세한 내용은 이벤트처리 및 발생을 참조하세요.

생성자

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

DragEventArgs 클래스의 새 인스턴스를 초기화합니다.

DragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects, DropImageType, String, String)

DragEventArgs 클래스의 새 인스턴스를 초기화합니다.

속성

AllowedEffect

끌기 이벤트의 시작자(또는 원본)가 허용하는 끌어서 놓기 작업을 가져옵니다.

Data

이 이벤트와 연결된 데이터가 포함된 IDataObject 가져옵니다.

DropImageType

놓기 설명 이미지 유형을 가져오거나 설정합니다.

Effect

끌어서 놓기 작업의 대상 놓기 효과를 가져오거나 설정합니다.

KeyState

Shift, Ctrl 및 Alt 키의 현재 상태와 마우스 단추의 상태를 가져옵니다.

Message

"%1이동"과 같은 드롭 설명 텍스트를 가져오거나 설정합니다.

MessageReplacementToken

Message%1 지정한 경우 "문서"와 같은 드롭 설명 텍스트를 가져오거나 설정합니다.

X

마우스 포인터의 x 좌표를 화면 좌표로 가져옵니다.

Y

마우스 포인터의 y 좌표를 화면 좌표로 가져옵니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 여부를 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 사용됩니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보