如何:使用 HardwareButton 组件

更新:2007 年 11 月

您可以在 Pocket PC 上配置硬件按钮以激活 Form。本示例使用第一个和第四个硬件按钮激活某个应用程序,并在状态栏中指示按下了哪个按钮。

设置硬件按钮以激活窗体

  1. 创建一个 Pocket PC Windows 应用程序。

  2. 创建一个 HardwareButton 的实例。

  3. AssociatedControl 属性设置为窗体。

  4. HardwareKey 属性设置为由 HardwareKeys 枚举定义的应用程序键。

  5. 对于要使用的其他硬件按钮,重复第 2 步至第 4 步。

  6. 当按下并松开某硬件按钮时,窗体既会收到 KeyDown 事件也会收到 KeyUp 事件。您可以使用任意一个事件确定是否曾按下某个硬件按钮。

示例

本示例设置第一个和第四个硬件按钮以激活窗体。若要进行演示,请按下列步骤操作:

  1. 运行该应用程序。

  2. 在设备上打开另一应用程序。

  3. 按硬件按钮 1 或 4 以激活该应用程序的窗体。状态栏指示按下了哪个硬件按钮。

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

编译代码

此示例需要引用下面的命名空间:

请参见

参考

HardwareButton

其他资源

Pocket PC 开发和 .NET Compact Framework