HardwareButton-Klasse
Assembly: Microsoft.WindowsCE.Forms (in microsoft.windowsce.forms.dll)
Sie können eine Taste auf einem Pocket PC so konfigurieren, dass sie ein Form-Steuerelement, Panel-Steuerelement oder benutzerdefiniertes Steuerelement in Ihrer Anwendung aktiviert. Gehen Sie dazu wie folgt vor:
-
Erstellen Sie eine Instanz von HardwareButton.
-
Legen Sie für die AssociatedControl-Eigenschaft das zu aktivierende Formular oder Steuerelement fest.
-
Legen Sie für die HardwareKey-Eigenschaft einen der HardwareKeys-Enumerationswerte fest. Sie können bis zu sechs Tasten konfigurieren.
Wenn eine Taste mit einem Steuerelement verknüpft ist, empfängt das Steuerelement beim Drücken der Taste ein KeyDown-Ereignis und beim Loslassen der Taste ein KeyUp-Ereignis.
Um eine Taste in ihren ursprünglichen Zustand zu versetzen, legen Sie die AssociatedControl-Eigenschaft auf NULL (Nothing in Visual Basic) fest.
Beachten Sie, dass einige Pocket PCs über eine andere Anzahl von Tasten als 6 verfügen. Außerdem werden nicht alle Tasten vom Betriebssystem unterstützt. Windows Mobile 2003 für Pocket PC unterstützt vier Tasten, und Windows Mobile, Version 5.0, für Pocket PC unterstützt fünf Tasten.
Diese Klasse wird auf dem Smartphone und anderen Windows CE-Geräten, die keine Pocket PCs sind, nicht unterstützt.
Im folgenden Codebeispiel wird gezeigt, wie durch Drücken der ersten und vierten Taste auf einem Pocket PC ein Formular aktiviert wird.
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.MarshalByRefObject
System.ComponentModel.Component
Microsoft.WindowsCE.Forms.HardwareButton
Windows Mobile für Pocket PC
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.