En el siguiente ejemplo se muestra una operación de arrastrar y colocar entre dos controles ListBox. En el ejemplo se llama al método DoDragDrop cuando se inicia la acción de arrastrar. La acción de arrastrar se inicia si se ha movido el mouse más de SystemInformation..::.DragSize desde su ubicación durante el evento MouseDown. El método IndexFromPoint se utiliza para determinar el índice del elemento que se va a arrastrar durante el evento MouseDown.
En el ejemplo también se muestra la forma de utilizar cursores personalizados para la operación de arrastrar y colocar. En este ejemplo, se supone que hay dos archivos de cursor (3dwarro.cur y 3dwno.cur) en el directorio de la aplicación para los cursores de arrastrar y colocar personalizados, respectivamente. Los cursores personalizados se utilizan si se ha activado UseCustomCursorsCheckCheckBox. Los cursores personalizados se establecen en el controlador de eventos GiveFeedback.
El estado del teclado se evalúa en el controlador de eventos DragOver en el ListBox de la derecha para determinar la operación de arrastrar en función del estado de las teclas MAYÚS, CTRL, ALT o CTRL+ALT. La ubicación de ListBox donde se produciría la operación de colocar también se determina durante el evento DragOver. Si los datos que se van a colocar no son de tipo String, entonces DragEventArgs..::.Effect se establece en DragDropEffects..::.None. Por último, el estado de la operación de colocar se muestra en DropLocationLabelLabel.
Los datos que se van a colocar en el ListBox de la derecha se determinan en el controlador de eventos DragDrop y el valor String se agrega a la ubicación apropiada de ListBox. Si la operación de arrastrar se realiza fuera de los límites del formulario, la operación de arrastrar y colocar se cancelará en el controlador de eventos QueryContinueDrag.
En este fragmento de código se muestra cómo utilizar la enumeración DragAction. Para obtener el ejemplo completo del código, vea el método DoDragDrop.
Private Sub ListDragSource_QueryContinueDrag(ByVal sender As Object, ByVal e As QueryContinueDragEventArgs) Handles ListDragSource.QueryContinueDrag
' Cancel the drag if the mouse moves off the form.
Dim lb as ListBox = CType(sender, System.Windows.Forms.ListBox)
If (lb isNot nothing) Then
Dim f as Form = lb.FindForm()
' Cancel the drag if the mouse moves off the form. The screenOffset
' takes into account any desktop bands that may be at the top or left
' side of the screen.
If (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) Or _
((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) Or _
((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) Or _
((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) Then
e.Action = DragAction.Cancel
End If
End if
End Sub
private void ListDragSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e) {
// Cancel the drag if the mouse moves off the form.
ListBox lb = sender as ListBox;
if (lb != null) {
Form f = lb.FindForm();
// Cancel the drag if the mouse moves off the form. The screenOffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) ||
((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) ||
((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) ||
((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) {
e.Action = DragAction.Cancel;
}
}
}
void ListDragSource_QueryContinueDrag( Object^ sender, System::Windows::Forms::QueryContinueDragEventArgs^ e )
{
// Cancel the drag if the mouse moves off the form.
ListBox^ lb = dynamic_cast<ListBox^>(sender);
if ( lb != nullptr )
{
Form^ f = lb->FindForm();
// Cancel the drag if the mouse moves off the form. The screenOffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if ( ((Control::MousePosition.X - screenOffset.X) < f->DesktopBounds.Left) || ((Control::MousePosition.X - screenOffset.X) > f->DesktopBounds.Right) || ((Control::MousePosition.Y - screenOffset.Y) < f->DesktopBounds.Top) || ((Control::MousePosition.Y - screenOffset.Y) > f->DesktopBounds.Bottom) )
{
e->Action = DragAction::Cancel;
}
}
}
private void listDragSource_QueryContinueDrag(Object sender,
System.Windows.Forms.QueryContinueDragEventArgs e)
{
// Cancel the drag if the mouse moves off the form.
ListBox lb = (ListBox)sender;
if (lb != null) {
Form f = lb.FindForm();
// Cancel the drag if the mouse moves off the form. The
// screenOffset takes into account any desktop bands
// that may be at the top or left side of the screen.
if (Control.get_MousePosition().get_X() - screenOffset.get_X()
< f.get_DesktopBounds().get_Left()
|| Control.get_MousePosition().get_X()
- screenOffset.get_X() > f.get_DesktopBounds().get_Right()
|| Control.get_MousePosition().get_Y() - screenOffset.get_Y()
< f.get_DesktopBounds().get_Top()
|| Control.get_MousePosition().get_Y() - screenOffset.get_Y()
> f.get_DesktopBounds().get_Bottom()) {
e.set_Action(DragAction.Cancel);
}
}
} //listDragSource_QueryContinueDrag