Figure 1.
Introduction
At a minimum, a Mobile PC application is expected to efficiently manage the power needs, communication environment, inking and form factor constraints of the platforms it supports. This article shows how to easily incorporate ink awareness, power awareness, power awareness and size awareness into otherwise Mobile PC agnostic Windows Form applications, bringing then much closer to conformity as Mobile PC applications. The Mobile PC agnostic Windows Form application used for this demonstration is the VB.NET version of the Black Jack Card Game Starter Kit sample application with either Visual Basic 2005 Express Edition or Visual Studio 2005 as the development environment. The idea is to combine several distinct classes that manage screen size, ink, power and network connectivity separately into one Windows Form class that is easily inherited by a Mobile PC agnostic application. This article highlights some of the benefits of this approach!
Background
A Mobile PC can be defined as a small, mobile, network aware computer running a complete OS version of Windows (Windows XP, Windows XP Tablet Edition 2005 or Windows Vista), i.e., a Laptop/Notebook, Tablet PC or Ultra Mobile PC. A Mobile PC application is any application designed to run on a Mobile PC. Todd Landstad's article Optimizing your Mobile .NET 3.0 WPF Application for a UMPC or Touch-enabled PC shows one way to incorporate power and network awareness into a WPF application. Using Visual Inheritance in a Windows Form application is another way! The MobilePCAwareForm provided with this sample is a port of the C# class PowerNetworkAware.cs (documented in Todd's article) to Visual Basic.NET and much more! For example, power awareness supports the disabling/enabling of CPU-intensive functionality depending on the percent of the machine's remaining battery life.
Understanding The MobilePCAwareForm
The idea is to wrap all of the Mobile PC aware calls to the Win32 API into a managed Windows Form class (MobilePCAwareForm.vb) so that other Windows Form applications can easily inherit from it. The MobilePCAwareForm.vb contains two PictureBox controls, named the PowerStausPictureBox and NetworkStatusPictureBox, respectively, which are self-contained components that only need to be appropriately sized and positioned at design-time. Screen size awareness is also built in and discussed in more detail later. The MobilePCAwareForm is flexible, extensible and provides for a clean separation between application code and Mobile PC aware code.
Inheriting From The MobilePCAwareForm
The Black Jack Card Game consists of three Windows forms. To keep this demonstration simple, the BlackJackForm.vb is set as the project's startup form, while the remaining two other forms are excluded from the project. Be warned that the game "straight out of the box" is a bit flawed! In this demonstration only BlackJackForm.vb is made Mobile PC aware by letting it inherit from the MobilePCAwareForm. This is done by making a simple change to the BlackJackForm.vb file, as shown in the code snippet below.
Public Class BlackJackForm
Inherits MobilePCAwareForm
.
.
.
End Class
Power Awareness In The Black Jack Card Game
Consider an application that has CPU-intensive, power-consuming code that might need to temporarily shut down at some weak battery level and otherwise be restored when the power level is once again strong. In the Black Jack Card game that CPU-intensive code is "modeled" by a call to a graphics intensive method written by this author called AnimatePlayerCard()
. It is used to animate the dealing of a card from the deck to the player. Whenever the battery is strong (50% or better) or on AC power, player card animation is invoked. During periods of weak battery power, the animation is suspended, unless over-ridden by the Player. See Figure 1. To keep things simple, cards dealt to the Dealer are NEVER animated! A weak to strong or strong to weak power level transition triggers the display of an appropriately formatted notification balloon. Since the user is able to over-ride the suspension of weak battery notification request to conserve power and remain in animation mode, a critical battery notification message, asking the Player to shut down the application, is also included. The code that controls the animation is detailed in the snippet below in the UpdateUIPlayerCards()
method.
Private Sub UpdateUIPlayerCards()
.
.
.
If (pcards.Count = 2) Then
If (BatteryIsOK()) Then AnimatePlayerCard(i)
.
.
.
End If
If (pcards.Count > 2 And i >= pcards.Count - 1) Then
If (BatteryIsOK()) Then AnimatePlayerCard(i)
.
.
.
End If
.
.
.
End Sub
Here is the design-time code snippet that sets the weak battery level for suspending a CPU-intensive task that is less than or equal to 50% of battery life.
#Region "Design-time Constant(s)"
.
.
.
Private Const WEAK_BATTERY_LEVEL As Single = 50.0
.
.
#End Region
Ink Awareness In The Black Jack Card Game
Ink awareness is showcased by allowing the Player to choose his/her name by writing it on the betting table before dealing. Clicking the Deal button invokes name recognition and that text placed in the playerNameLabel replacing the default name, "Player1". See Figure 1. Two inherited methods are used to achieve this as shown in the code snippet below.
NOTE: For ink support your project must include at least a reference to the Microsoft.Ink DLL to run on Windows Vista and Windows Tablet PC 2005!
Private Sub BlackJackForm_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
.
.
.
StartCollectingInk(Me.Handle)
End Sub
Private Sub DealBtn_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles dealButton.Click
UpdatePlayerName()
.
.
.
End Sub
Private Sub UpdatePlayerNameLabelText()
RecognizeInk()
If (not RecognizedInk() = String.Empty) Then playerNameLabel.Text =
RecognizedInk()
End Sub
Screen Size Awareness
Screen size awareness is built into the MobilePCAwareForm. It uses the following algorithm. When the form loads, the design-time width, height, top co-ordinate, left co-ordinate and name of each control are saved along with the form's design-time width and height. When the form re-sizes, this information is consumed to re-size all its controls proportionately, based on the form's design-time size. In order to support the UMPC, the BlackJackForm's design-time size is set to 800 x 480 pixels. In this example the form's minimum size is also set to 800 x 480 pixels. The form's controls were re-sized and re-located at design-time to optimize touch screen input.
Testing Out The Application
The Black Jack Card Game runs well on Windows Vista and Windows XP Tablet Edition 2005 with only the insertion of a reference to the Microsoft.Ink DLL, either version 1.7 or version 6.0. However, testing out the application on Windows XP Home Edition generated a Message Box on installation warning that the application will nor run without the Microsoft.Ink DLL installed by an administrator in the machine's Global Assembly Cache (GAC). To overcome this difficulty a copy of the Microsoft.Ink (both version 1.7 or version 6.0 work) is placed in the project inside the DLL folder with a reference pointing to that file location. Copy Local was enabled in the References tab of the Project's Properties as shown in Figure 2 below. This allows the application to install and gracefully run correctly under Windows XP Home without ink support!
Figure 2.
One final point to note is that the NetworkChange_NetworkAvailabilityChanged()
event handler and the OnPowerModeChange()
event handler don't work as advertised in the port of the PowerNetworkAware.cs code. A hint that this is the case is given by running the Code Analyser (only available in Visual Studio 2005), which warns of this. The code is corrected in this project and tests out just fine!