Condividi tramite


Procedura: utilizzare il componente HardwareButton

Aggiornamento: novembre 2007

È possibile configurare i pulsanti hardware di un Pocket PC per attivare un oggetto Form. Nell'esempio riportato di seguito viene attivata un'applicazione con il primo e il quarto pulsante hardware e nella barra di stato viene indicato quale pulsante è stato premuto.

Per impostare un pulsante hardware per attivare un form

  1. Creare un'applicazione Windows Pocket PC.

  2. Creare un'istanza dell'oggetto HardwareButton.

  3. Impostare la proprietà AssociatedControl sul form.

  4. Impostare la proprietà HardwareKey su una chiave applicazione definita dall'enumerazione HardwareKeys.

  5. Ripetere i passaggi da 2 a 4 per aggiungere altri pulsanti hardware da utilizzare.

  6. Quando si preme e si rilascia un pulsante hardware, il form riceve entrambi gli eventi KeyDown e KeyUp. È possibile utilizzare questi eventi per determinare se un pulsante hardware è stato premuto.

Esempio

Nell'esempio riportato di seguito per attivare il form vengono impostati il primo e il quarto pulsante hardware. Per la dimostrazione, seguire questi passaggi:

  1. Eseguire l'applicazione.

  2. Aprire un'altra applicazione sul dispositivo.

  3. Premere il pulsante hardware 1 o 4 per attivare il form di questa applicazione. Nella barra di stato verrà indicato quale pulsante hardware è stato premuto.

Private Sub ConfigHWButton()
    ' Set KeyPreview to true so that the form 
    ' will receive key events before they 
    ' are passed to the control that has focus. 

    Me.KeyPreview = True

    hwb1 = New HardwareButton()
    hwb4 = New HardwareButton()

    ' Set the AssociatedControl property
    ' to the current form and configure the
    ' first and fourth buttons to activate the form.
    Try
        hwb1.AssociatedControl = Me
        hwb4.AssociatedControl = Me
        hwb1.HardwareKey = HardwareKeys.ApplicationKey1
        hwb4.HardwareKey = HardwareKeys.ApplicationKey4
    Catch exc As Exception
        MessageBox.Show(exc.Message & " Check if the hardware button is " & _
            "physically available on this device.")
    End Try
End Sub

Private Overloads Sub OnKeyUp(sender As Object, e As KeyEventArgs) _
    Handles MyBase.KeyUp
    ' When a hardware button is pressed and released,
    ' this form receives the KeyUp event. The OnKeyUp
    ' method is used to determine which hardware
    ' button was pressed, because the event data
    ' specifies a member of the HardwareKeys enumeration.
    Select Case CType(e.KeyCode, HardwareKeys)
        Case HardwareKeys.ApplicationKey1
            statusBar1.Text = "Button 1 pressed."

        Case HardwareKeys.ApplicationKey4
            statusBar1.Text = "Button 4 pressed."

        Case Else
    End Select
End Sub
// Configure hardware buttons
// 1 and 4 to activate the current form.
private void HBConfig()
    {
        try 
        {
            hwb1 = new HardwareButton();
            hwb4 = new HardwareButton();
            hwb1.AssociatedControl = this;
            hwb4.AssociatedControl = this;
            hwb1.HardwareKey = HardwareKeys.ApplicationKey1;
            hwb4.HardwareKey = HardwareKeys.ApplicationKey4;
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message + " Check if the hardware " +
                "button is physically available on this device.");
        }
}

// When a hardware button is pressed and released,
// this form receives the KeyUp event. The OnKeyUp
// method is used to determine which hardware
// button was pressed, because the event data
// specifies a member of the HardwareKeys enumeration.
private void OnKeyUp(object sender, KeyEventArgs e)
{
    switch ((HardwareKeys)e.KeyCode)
    {
        case HardwareKeys.ApplicationKey1:
            statusBar1.Text = "Button 1 pressed.";
            break;

        case HardwareKeys.ApplicationKey4:
            statusBar1.Text = "Button 4 pressed.";
            break;

        default:
            break;
    }
}

Compilazione del codice

Questo esempio richiede riferimenti ai seguenti spazi dei nomi:

Vedere anche

Riferimenti

HardwareButton

Altre risorse

Sviluppo per Pocket PC e .NET Compact Framework