Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

Classe DragEventArgs

 

Fornece dados para o evento DragDrop, DragEnter ou DragOver.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms.dll)


[ComVisibleAttribute(true)]
public class DragEventArgs : EventArgs

NomeDescrição
System_CAPS_pubmethodDragEventArgs(IDataObject, Int32, Int32, Int32, DragDropEffects, DragDropEffects)

Inicializa uma nova instância da classe DragEventArgs.

NomeDescrição
System_CAPS_pubpropertyAllowedEffect

Obtém as operações do tipo "arrastar e soltar" permitidas pelo remetente (ou pela fonte) do evento arrastar.

System_CAPS_pubpropertyData

Obtém o IDataObject que contém os dados associados a este evento.

System_CAPS_pubpropertyEffect

Obtém ou define o efeito de soltar de destino em uma operação do tipo "arrastar e soltar".

System_CAPS_pubpropertyKeyState

Obtém o estado atual das teclas SHIFT, CTRL e ALT, bem como o estado dos botões do mouse.

System_CAPS_pubpropertyX

Obtém a coordenada x do ponteiro do mouse, em coordenadas de tela.

System_CAPS_pubpropertyY

Obtém a coordenada y do ponteiro do mouse, em coordenadas de tela.

NomeDescrição
System_CAPS_pubmethodEquals(Object)

Verifica se o objeto especificado é igual ao objeto atual. (Herdado de Object.)

System_CAPS_protmethodFinalize()

Permite que um objeto tente liberar recursos e executar outras operações de limpeza antes que ele seja recuperado pela coleta de lixo. (Herdado de Object.)

System_CAPS_pubmethodGetHashCode()

Serve como a função de hash padrão.(Herdado de Object.)

System_CAPS_pubmethodGetType()

Obtém o Type da instância atual.(Herdado de Object.)

System_CAPS_protmethodMemberwiseClone()

Cria uma cópia superficial do Object atual.(Herdado de Object.)

System_CAPS_pubmethodToString()

Retorna uma string que representa o objeto atual.(Herdado de Object.)

The E:System.Windows.Forms.Control.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. The E:System.Windows.Forms.Control.DragEnter event occurs when the user moves the mouse pointer onto the control while dragging an object with the mouse. The E:System.Windows.Forms.Control.DragOver event occurs when the user moves the mouse pointer over the control while dragging an object with the mouse.

A T:System.Windows.Forms.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.

The following example demonstrates a drag-and-drop operation between two T:System.Windows.Forms.ListBox controls. The example calls the M:System.Windows.Forms.Control.DoDragDrop(System.Object,System.Windows.Forms.DragDropEffects) method when the drag action starts. The drag action starts if the mouse has moved more than P:System.Windows.Forms.SystemInformation.DragSize from the mouse location during the E:System.Windows.Forms.Control.MouseDown event. The M:System.Windows.Forms.ListBox.IndexFromPoint(System.Drawing.Point) 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. 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. The custom cursors will be used if the UseCustomCursorsCheckT:System.Windows.Forms.CheckBox is checked. The custom cursors are set in the E:System.Windows.Forms.Control.GiveFeedback event handler.

The keyboard state is evaluated in the E:System.Windows.Forms.Control.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. The location in the ListBox where the drop would occur is also determined during the DragOver event. If the data to drop is not a String, then the P:System.Windows.Forms.DragEventArgs.Effect is set to F:System.Windows.Forms.DragDropEffects.None. Finally, the status of the drop is displayed in the DropLocationLabelT:System.Windows.Forms.Label.

The data to drop for the right ListBox is determined in the E:System.Windows.Forms.Control.DragDrop event handler and the String value is added at the appropriate place in the ListBox. If the drag operation moves outside the bounds of the form, then the drag-and-drop operation is canceled in the E:System.Windows.Forms.Control.QueryContinueDrag event handler.

This code excerpt demonstrates using the T:System.Windows.Forms.DragEventArgs class. See the M:System.Windows.Forms.Control.DoDragDrop(System.Object,System.Windows.Forms.DragDropEffects) method for the complete code example.

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

}

The following example illustrates how T:System.Windows.Forms.DragEventArgs are passed between the source and target of a drag-and-drop operation. In this example, a T:System.Windows.Forms.ListBox control is the source of the data, and the T:System.Windows.Forms.RichTextBox control is the target. The example assumes that the T:System.Windows.Forms.ListBox control has been populated with a list of valid filenames. When the user drags one of the displayed filenames from the T:System.Windows.Forms.ListBox control onto the T:System.Windows.Forms.RichTextBox control, the file referenced in the filename is opened.

The operation is initiated in the T:System.Windows.Forms.ListBox control's MouseDown event. In the E:System.Windows.Forms.RichTextBox.DragEnter event handler, the example uses the M:System.Windows.Forms.IDataObject.GetDataPresent(System.String,System.Boolean) method to verify that the data is in a format that the T:System.Windows.Forms.RichTextBox control can display and then sets the T:System.Windows.Forms.DragDropEffects property to specify that data should be copied from the source control to the target control. Finally, the T:System.Windows.Forms.RichTextBox control's DragDrop event handler uses the M:System.Windows.Forms.IDataObject.GetData(System.String,System.Boolean) 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.

}

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

.NET Framework
Disponível desde 1.1

Quaisquer membros estáticos públicos ( Compartilhado no Visual Basic) desse tipo são thread-safe. Não há garantia de que qualquer membro de instância seja thread-safe.

Retornar ao início
Mostrar: