다음을 통해 공유


DragEventArgs.Data 속성

정의

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

public:
 property System::Windows::Forms::IDataObject ^ Data { System::Windows::Forms::IDataObject ^ get(); };
public System.Windows.Forms.IDataObject Data { get; }
public System.Windows.Forms.IDataObject? Data { get; }
member this.Data : System.Windows.Forms.IDataObject
Public ReadOnly Property Data As IDataObject

속성 값

이 이벤트와 연결된 데이터입니다.

예제

다음 예제에서는 두 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

설명

이벤트 처리기의 본문에서 GetDataPresent 메서드를 사용하여 데이터가 끌 컨트롤의 형식 요구 사항과 일치하는지 여부를 확인할 수 있습니다. 형식 요구 사항을 충족하도록 데이터를 변환할지 여부를 지정할 수도 있습니다. 끌기된 데이터가 형식 요구 사항을 충족하는 경우 GetData 메서드를 사용하여 데이터를 검색합니다.

적용 대상

추가 정보