Condividi tramite


DragEventArgs.KeyState Proprietà

Definizione

Ottiene lo stato corrente dei tasti MAIUSC, CTRL e ALT, nonché lo stato dei pulsanti del mouse.

public:
 property int KeyState { int get(); };
public int KeyState { get; }
member this.KeyState : int
Public ReadOnly Property KeyState As Integer

Valore della proprietà

Stato corrente dei tasti MAIUSC, CTRL e ALT e dei pulsanti del mouse.

Esempio

Nell'esempio seguente viene illustrata un'operazione di trascinamento della selezione tra due controlli ListBox. Nell'esempio viene chiamato il metodo DoDragDrop all'avvio dell'azione di trascinamento. L'azione di trascinamento inizia se il mouse è stato spostato più di SystemInformation.DragSize dalla posizione del mouse durante l'evento MouseDown. Il metodo IndexFromPoint viene utilizzato per determinare l'indice dell'elemento da trascinare durante l'evento MouseDown.

L'esempio illustra anche l'uso di cursori personalizzati per l'operazione di trascinamento della selezione. Nell'esempio si presuppone che nella directory dell'applicazione esistano due file di cursore, 3dwarro.cur e 3dwno.cur, rispettivamente per i cursori di trascinamento e senza rilascio personalizzati. I cursori personalizzati verranno utilizzati se è selezionata la UseCustomCursorsCheckCheckBox. I cursori personalizzati vengono impostati nel gestore eventi GiveFeedback.

Lo stato della tastiera viene valutato nel gestore eventi DragOver per il ListBoxdestro per determinare quale operazione di trascinamento si baserà sullo stato dei tasti MAIUSC, CTRL, ALT o CTRL+ALT. La posizione nel ListBox in cui si verifica l'eliminazione viene determinata anche durante l'evento DragOver. Se i dati da eliminare non sono un String, il DragEventArgs.Effect è impostato su DragDropEffects.None. Infine, lo stato del rilascio viene visualizzato nel DropLocationLabelLabel.

I dati da eliminare per il ListBox corretto sono determinati nel gestore eventi DragDrop e il valore String viene aggiunto nella posizione appropriata nel ListBox. Se l'operazione di trascinamento si sposta all'esterno dei limiti del form, l'operazione di trascinamento della selezione viene annullata nel gestore eventi QueryContinueDrag.

Questo estratto di codice illustra l'uso della classe DragEventArgs. Vedere il metodo DoDragDrop per l'esempio di codice completo.

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

Commenti

È possibile rendere l'effetto di un'operazione di trascinamento della selezione in base allo stato di una chiave specifica. Ad esempio, è possibile decidere di copiare o spostare i dati a seconda che i tasti CTRL o MAIUSC vengano premuti durante l'operazione di trascinamento della selezione.

I bit impostati nella proprietà KeyState identificano i tasti o i pulsanti del mouse premuti durante l'operazione. Ad esempio, se viene premuto il pulsante sinistro del mouse, viene impostato il primo bit nella proprietà KeyState. È possibile usare l'operatore AND bit per bit per testare lo stato di una determinata chiave.

Nella tabella seguente sono elencati i valori utilizzati per un evento specificato.

Valore Chiave
1 (bit 0) Pulsante sinistro del mouse.
2 (bit 1) Pulsante destro del mouse.
4 (bit 2) Tasto MAIUSC.
8 (bit 3) Tasto CTRL.
16 (bit 4) Pulsante centrale del mouse.
32 (bit 5) Tasto ALT.

Si applica a