Portrait vs Landscape, CRF Design for a Tablet PC
How do you systematically make use of the varied Tablet PC screen resolution dimensions using C#?
Designing a CRF specifically for a Tablet PC can be a challenging experience, but it can also be one of the most rewarding.
The Tablet PC is very similar to a traditional desktop computer or a laptop. As I write this entry, most Tablet PCs are running some variant of Windows. Whether it’s Windows XP Tablet PC Edition or Vista (which has built-in support for Tablet PC specific functionality), you’re basically dealing with a Windows machine and you can pretty much treat it as such… almost.
Maybe it’s not so much the variety. One of my biggest obstacles was that the dimensions change. They don’t change just once in a while (like when the resolution changes when you connect your laptop to a TV), they can change many times during a single session!
For example, convertible Tablet PCs (like the one in the image above) provide the ability to change from portrait to landscape and back again all in seconds. In addition, most Tablet PCs come in screen dimensions that are either wider or taller depending on whether it’s in slate mode or the more traditional notebook mode. Taking advantage of a taller or wider screen is going to be the topic of a future post, but let’s say you have a layout for making use of the added height or width, how do you know when to use which?
The first step is determining whether the screen dimensions are in portrait (slate) mode or landscape (notebook) mode. Here are two straight-forward methods for determining which mode the Tablet PC is using:
public bool IsPortrait()
{
return Screen.PrimaryScreen.Bounds.Height >
Screen.PrimaryScreen.Bounds.Width;
}
public bool IsLandscape()
{
return !this.IsPortrait();
}
We can setup a thread that calls one of these methods at specified intervals (polling solution), or we can leverage the .NET event architecture so that our application responds to dimension changes. Leveraging the event architecture is intuitively more efficient and more reliable, so add this to your Form_Load() method:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged +=
new EventHandler(SystemEvents_DisplaySettingsChanged);
Now, define the event handler:
void SystemEvents_DisplaySettingsChanged(
object sender, EventArgs e)
{
if (ScreenProperties.IsPortrait())
// do portrait handling
else
// do landscape handling
}
And just like that, your form is now responding to screen resolution dimension changes!
To receive updates on new articles, subscribe to CRF Design today!












