Determining Font Size in Second Edition
Windows Mobile 2003 Second Edition adds a “Text Size” tab to the Screen control panel which lets you choose how large or small text should be on high-resolution screens. The setting you select here does not directly effect all text shown on the device though. Each application must decided if/when they want to respect the value. For example, text on the Start Menu is always 9pt while Today screen items adjust based on your preference.
So how does your application find out what the chosen size is? There is a new API in Second Edition called SHGetUIMetrics that gives you the answer. To learn more about this function, download the Second Edition Resource Kit and read the DPI Awareness whitepaper. Sample code for native developers can be found in “Crossword” sample (see shguim.h). That's great, but what about .NET CF developers? Well here's my managed wrapper:
[DllImport("aygshell.dll")]
private extern static int SHGetUIMetrics( UIMetric metric, ref int result, int size, int required );
public enum UIMetric
{
/// <summary>
/// Font size in points
/// </summary>
Point = 1,
/// <summary>
/// Font size in pixels
/// </summary>
Pixel = 2,
/// <summary>
/// Font size as a percentage of normal
/// </summary>
Scale = 3
}
/// <summary>
/// Queries the specified user interface metric.
/// NOTE: This only works under Windows Mobile 2003 Second Edition and later
/// </summary>
public static float GetUIMetric( UIMetric metric )
{
int result = 0;
SHGetUIMetrics( metric, ref result, 4, 0 );
if( metric == UIMetric.Point || metric == UIMetric.Scale )
{
return result / 100f;
}
else
{
return result;
}
}
After dropping that your code, using it is simple. For example, here's how you can change the font of a label to take on the selected size:
label1.Font = new Font( label1.Font.Name, GetUIMetric( UIMetric.Point ) , label1.Font.Style );
The other values in the UIMetric enum can come in handy if you need to reposition things based on the font size.
Note: if you are trying to use this code and GetUIMetric returns the same value no matter what is set in the control panel, that means you didn't mark your executable as resolution-aware. See the whitepaper for info on how to do this or use the VS.NET macro I posted last week to automate the process.
[Author: Robert Levy]
Comments
- Anonymous
May 17, 2009
Resize of font is working fine and alignment is not setting propely for ex: I am using 1 img and some text. if i resize the font then the gap between img and text is not constant. and i am not able to set the system background color to my todayscreenitem onSelect or OnClick event. Please help to fix these issues. My ID: sudhir.i@hastentechnologies.com Thanks in Advance