HardwareButton (Clase)
Actualización: noviembre 2007
Permite reemplazar la funcionalidad de los botones de hardware de Pocket PC.
Espacio de nombres: Microsoft.WindowsCE.Forms
Ensamblado: Microsoft.WindowsCE.Forms (en Microsoft.WindowsCE.Forms.dll)
Sintaxis
'Declaración
Public Class HardwareButton _
Inherits Component
'Uso
Dim instance As HardwareButton
public class HardwareButton : Component
public ref class HardwareButton : public Component
public class HardwareButton extends Component
Comentarios
Puede configurar un botón en un Pocket PC para activar Form, Panel o un control personalizado en su aplicación; para ello:
Cree una instancia de HardwareButton.
Establezca la propiedad AssociatedControl en el formulario o control que desea activar.
Establezca la propiedad HardwareKey en uno de los valores de enumeración HardwareKeys. Puede configurar hasta seis botones de hardware.
Cuando un botón de hardware está asociado a un control, el control recibe un evento KeyDown al presionar el botón y un evento KeyUp al soltarlo.
Para devolver un botón de hardware a su estado original, establezca la propiedad AssociatedControl en nullreferencia null (Nothing en Visual Basic).
Tenga en cuenta que algunos Pocket PC tienen un número diferente de botones de hardware distinto de seis. Igualmente, tenga presente que el sistema operativo no admite todos los botones. Windows Mobile 2003 para Pocket PC admite cuatro botones y el software de la versión 5.0 de Windows Mobile para Pocket PC admite cinco botones.
Esta clase no es compatible con Smartphone ni con otros dispositivos Windows CE que no sean Pocket PC.
Ejemplos
El ejemplo de código siguiente muestra cómo activar un formulario con el primer y el cuarto botón de hardware en un 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;
}
}
}
}
Jerarquía de herencia
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
Microsoft.WindowsCE.Forms.HardwareButton
Seguridad para subprocesos
Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.
Plataformas
Windows Mobile para Pocket PC
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.
Información de versión
.NET Compact Framework
Compatible con: 3.5, 2.0
Vea también
Referencia
Microsoft.WindowsCE.Forms (Espacio de nombres)