共用方式為


HardwareButton 類別

允許覆寫 Pocket PC 硬體按鈕的功能。

命名空間:  Microsoft.WindowsCE.Forms
組件:  Microsoft.WindowsCE.Forms (在 Microsoft.WindowsCE.Forms.dll 中)

語法

'宣告
Public Class HardwareButton _
    Inherits Component
'用途
Dim instance As HardwareButton
public class HardwareButton : Component
public ref class HardwareButton : public Component
public class HardwareButton extends Component

備註

執行下列步驟,即可在 Pocket PC 上設定按鈕,以啟動 FormPanel 或應用程式中的自訂控制項:

  • 建立 HardwareButton 的執行個體。

  • AssociatedControl 屬性設定為想要啟動的表單或控制項。

  • HardwareKey 屬性設定為其中一個 HardwareKeys 列舉值。您最多可設定六個硬體按鈕。

硬體按鈕與控制項產生關聯時,如果按下按鈕,控制項會接收到 KeyDown 事件,如果放開按鈕,則會接收到 KeyUp 事件。

若要將硬體按鈕還原成其原始狀態,請將 AssociatedControl 屬性設為 nullNull 參照 (即 Visual Basic 中的 Nothing)。

請注意,有些 Pocket PC 的硬體按鈕數目並非六個。此外,作業系統可能不支援所有按鈕。Windows Mobile 2003 for Pocket PC 支援四個按鈕,而 Windows Mobile 5.0 for Pocket PC 軟體則支援五個按鈕。

在 Smartphone 和其他非 Pocket PC 的 Windows CE 裝置上,不支援此類別 (Class)。

範例

下列程式碼範例顯示如何使用 Pocket PC 上的第一個和第四個硬體按鈕來啟動表單。

Imports System
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms


Public Class Form1
   Inherits System.Windows.Forms.Form
   Private statusBar1 As StatusBar
   Private hwb1 As HardwareButton
   Private hwb4 As HardwareButton


   Public Sub New()

      InitializeComponent()

      ' Display OK button to close the application.
      Me.ControlBox = True
      Me.MinimizeBox = False

      ConfigHWButton()


   End Sub


   Protected Overrides Sub Dispose(disposing As Boolean)
      MyBase.Dispose(disposing)
   End Sub

   Private Sub InitializeComponent()
      Me.statusBar1 = New System.Windows.Forms.StatusBar()
      Me.hwb1 = New Microsoft.WindowsCE.Forms.HardwareButton
      Me.hwb4 = New Microsoft.WindowsCE.Forms.HardwareButton
      Me.SuspendLayout()
      '
      ' statusBar1
      '
      Me.statusBar1.Location = New System.Drawing.Point(0, 246)
      Me.statusBar1.Name = "statusBar1"
      Me.statusBar1.Size = New System.Drawing.Size(240, 22)
      '
      ' Form1
      '
      Me.ClientSize = New System.Drawing.Size(240, 268)
      Me.Controls.Add(statusBar1)
      Me.Name = "Form1"
      Me.Text = "HW Button Test"
      Me.ResumeLayout(False)
      statusBar1.Text = "From another app, press button 1 or 4."
   End Sub

   Shared Sub Main()
      Application.Run(New Form1())
   End Sub
   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
End Class 
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace HardwareButtonTest
{
    public class Form1 : System.Windows.Forms.Form
    {
        private StatusBar statusBar1;
        private HardwareButton hwb1, hwb4;

        public Form1()
        {

            InitializeComponent();

            // Display OK button to close the application.
            this.ControlBox = true;
            this.MinimizeBox = false;

            // Create event-handler delegate for the KeyUp
            // event for this form. This form is associated
            // with each of the hardware buttons, and the
            // event occurs when a hardware button is pressed.
            // Note that you could also use the KeyDown event
            // instead of the KeyUp event.
            this.KeyPreview = true;
            this.KeyUp += new KeyEventHandler(this.OnKeyUp);

            // Call the method to configure
            // the hardware button.
            HBConfig();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.SuspendLayout();
//
// statusBar1
//
            this.statusBar1.Location = new System.Drawing.Point(0, 246);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Size = new System.Drawing.Size(240, 22);
//
// Form1
//
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.statusBar1);
            this.Name = "Form1";
            this.Text = "HW Button Test";
            this.ResumeLayout(false);
            statusBar1.Text = "Press hardware button 1 or 4.";

        }

        static void Main()
        {
            Application.Run(new Form1());
        }


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

繼承階層架構

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      Microsoft.WindowsCE.Forms.HardwareButton

執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。

平台

Windows Mobile for Pocket PC

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

版本資訊

.NET Compact Framework

支援版本:3.5、2.0

請參閱

參考

HardwareButton 成員

Microsoft.WindowsCE.Forms 命名空間

其他資源

HOW TO:使用 HardwareButton 元件