DragEventArgs.KeyState Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá aktuální stav kláves SHIFT, CTRL a ALT a také stav tlačítek myši.
public:
property int KeyState { int get(); };
public int KeyState { get; }
member this.KeyState : int
Public ReadOnly Property KeyState As Integer
Hodnota vlastnosti
Aktuální stav kláves SHIFT, CTRL a ALT a tlačítek myši.
Příklady
Následující příklad ukazuje operaci přetažení mezi dvěma ListBox ovládacími prvky. Příklad volá metodu DoDragDrop při spuštění akce přetažení. Akce přetažení se spustí, pokud se myš během události přesunula více než SystemInformation.DragSize z umístění MouseDown myši. Metoda IndexFromPoint slouží k určení indexu položky, která se má během MouseDown události přetáhnout.
Příklad také ukazuje použití vlastních kurzorů pro operaci přetažení. Příklad předpokládá, 3dwarro.cur že dva soubory kurzoru a 3dwno.cur, existují v adresáři aplikace, pro vlastní přetažení a bez-drop kurzory, v uvedeném pořadí. Vlastní kurzory se použijí, pokud je zaškrtnuté UseCustomCursorsCheckCheckBox . Vlastní kurzory jsou nastaveny v obslužné rutině GiveFeedback události.
Stav klávesnice se vyhodnotí v DragOver obslužné rutině události vpravo ListBox, aby bylo možné určit, jaké operace přetažení bude založená na stavu kláves SHIFT, CTRL, ALT nebo CTRL+ALT. Umístění v místě, kde ListBox by došlo k poklesu, je také určeno během DragOver události. Pokud data, která se mají vypustit, nejsou String, je nastavena DragEventArgs.Effect na DragDropEffects.Nonehodnotu . Nakonec se stav poklesu zobrazí v sadě DropLocationLabelLabel.
Data, která se mají zahodit pro právo ListBox , jsou určena v DragDrop obslužné rutině události a String hodnota se přidá na příslušné místo v bodě ListBox. Pokud se operace přetažení přesune mimo hranice formuláře, operace přetažení se zruší v obslužné rutině QueryContinueDrag události.
Tento výňatek kódu ukazuje použití DragEventArgs třídy. Podívejte se na DoDragDrop metodu kompletního příkladu kódu.
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
Poznámky
Efekt operace přetažení můžete nastavit tak, aby závisel na stavu konkrétního klíče. Můžete se například rozhodnout kopírovat nebo přesouvat data v závislosti na tom, jestli se během operace přetažení stisknou klávesy CTRL nebo SHIFT.
Bity nastavené ve KeyState vlastnosti identifikují klávesy nebo tlačítka myši, která byla během operace stisknuta. Pokud je například stisknuto levé tlačítko myši, nastaví se první bit ve KeyState vlastnosti. Pomocí bitového operátoru AND můžete testovat stav daného klíče.
Následující tabulka uvádí hodnoty, které se používají pro zadanou událost.
| Hodnota | Klíč |
|---|---|
| 1 (bit 0) | Levé tlačítko myši. |
| 2 (bit 1) | Pravé tlačítko myši. |
| 4 (bit 2) | Klávesa SHIFT. |
| 8 (bit 3) | Klávesa CTRL. |
| 16 (bit 4) | Prostřední tlačítko myši. |
| 32 (bit 5) | Klávesa ALT. |