DragEventArgs.AllowedEffect Właściwość

Definicja

Pobiera operacje przeciągania i upuszczania dozwolone przez obiekt źródłowy (lub źródło) zdarzenia przeciągania.

public:
 property System::Windows::Forms::DragDropEffects AllowedEffect { System::Windows::Forms::DragDropEffects get(); };
public System.Windows.Forms.DragDropEffects AllowedEffect { get; }
member this.AllowedEffect : System.Windows.Forms.DragDropEffects
Public ReadOnly Property AllowedEffect As DragDropEffects

Wartość właściwości

DragDropEffects Jedna z wartości.

Przykłady

W poniższym przykładzie pokazano operację przeciągania i upuszczania między dwoma ListBox kontrolkami. Przykład wywołuje metodę DoDragDrop po uruchomieniu akcji przeciągania. Akcja przeciągania rozpoczyna się, jeśli mysz przeniosła się więcej niż SystemInformation.DragSize z lokalizacji myszy podczas MouseDown zdarzenia. Metoda IndexFromPoint służy do określania indeksu elementu do przeciągania podczas MouseDown zdarzenia.

W przykładzie pokazano również użycie niestandardowych kursorów dla operacji przeciągania i upuszczania. W przykładzie przyjęto założenie, że dwa pliki 3dwarro.cur kursorów i 3dwno.cur, istnieją w katalogu aplikacji, odpowiednio dla niestandardowego przeciągania i bez upuszczania kursorów. Kursory niestandardowe będą używane, jeśli element jest zaznaczony UseCustomCursorsCheckCheckBox . Kursory niestandardowe są ustawiane w procedurze obsługi zdarzeń GiveFeedback .

Stan klawiatury jest oceniany w procedurze DragOver obsługi zdarzeń po prawej stronie ListBox, aby określić, co operacja przeciągania będzie oparta na stanie klawiszy SHIFT, CTRL, ALT lub CTRL+ALT. Lokalizacja, w ListBox której występuje spadek, jest również określana podczas DragOver zdarzenia. Jeśli dane do porzucania nie są wartością String, DragEventArgs.Effect parametr jest ustawiony na DragDropEffects.Nonewartość . Na koniec stan upuszczania jest wyświetlany w elemecie DropLocationLabelLabel.

Dane do porzucania po prawej stronie ListBox są określane w procedurze DragDrop obsługi zdarzeń, a String wartość jest dodawana w odpowiednim miejscu w ListBoxobiekcie . Jeśli operacja przeciągania przesuwa się poza granice formularza, operacja przeciągania i upuszczania zostanie anulowana w procedurze QueryContinueDrag obsługi zdarzeń.

Ten fragment kodu przedstawia użycie DragEventArgs klasy . Zobacz metodę DoDragDrop kompletnego przykładu kodu.

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

Uwagi

Gdy kontrolka inicjuje operację przeciągania i upuszczania przez wywołanie Control.DoDragDrop metody, określa dopuszczalne skutki operacji. Na przykład podczas przeciągania pliku ze źródła, jeśli plik jest tylko do odczytu (lub z nośnika magazynu tylko do odczytu, takiego jak dysk CD), źródło wskaże, że plik można skopiować, ale nie przenieść, do miejsca docelowego.

Przed podjęciem próby wykonania operacji na przeciąganych danych należy zbadać tę właściwość, aby upewnić się, że operacja jest dozwolona.

Dotyczy

Zobacz też