Share via


Comment : utiliser le composant HardwareButton

Mise à jour : novembre 2007

Vous pouvez configurer les boutons matériel sur un Pocket PC pour activer un Form. Cet exemple active une application avec les premier et quatrième boutons matériel, et indique dans la barre d'état le bouton utilisé.

Pour définir un bouton matériel pour activer un formulaire

  1. Créez une application Windows Pocket PC.

  2. Créez une instance d'un HardwareButton.

  3. Affectez le formulaire comme valeur de la propriété AssociatedControl.

  4. Affectez à la propriété HardwareKey une valeur de clé de l'application définie par l'énumération HardwareKeys.

  5. Répétez les étapes 2 à 4 pour les autres boutons matériel que vous souhaitez utiliser.

  6. Lorsque vous appuyez sur un bouton matériel et que vous le relâchez, le formulaire reçoit à la fois les événements KeyDown et KeyUp. Vous pouvez utiliser n'importe quel événement pour déterminer si un bouton matériel a été utilisé.

Exemple

Cet exemple définit les premier et quatrième boutons matériel pour activer le formulaire. Pour effectuer la démonstration, suivez ces étapes :

  1. Exécutez l'application.

  2. Ouvrez une autre application sur le périphérique.

  3. Appuyez sur le bouton matériel 1 ou 4 pour activer le formulaire de cette application. La barre d'état indique le bouton matériel utilisé.

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;
    }
}

Compilation du code

Cet exemple nécessite des références aux espaces de noms suivants :

Voir aussi

Référence

HardwareButton

Autres ressources

Développement Pocket PC et le .NET Compact Framework