Tuesday, September 23, 2008

Handling Resize of Form on SIP event in the Compact Framework

I have learned (a while back) that the bug (I think pre CF 2.0 SP2) when showing or hiding the SIP didn’t send the WM_RESIZE message to the form has been fixed.

Usually you would typically derive from a base form which derived from Form. In this class you would handle the SIP EnabledChanged event and resize the tab control or another other control that wasn’t using any type of anchoring/docking.
You would normally have coded something like this:
private void sip_EnabledChanged(object sender, EventArgs e)
{
Resize();
}

private void Resize()
{
try
{
if (Visible)
{
foreach (Control ctl in Controls)
{
if (ctl.GetType() == typeof(TabControl))
{
//Set the height of the tab control.
Rectangle rect;
rect = this.ClientRectangle;
ctl.Height = rect.Height + 2 * 96 /
CurrentAutoScaleDimentions.Width;
ctl.Width = rect.Width;
}
}
}
}
catch { }
}
The above is still relevant if you are targeting CF 1.0. However you might want to extend the Resize method to support other controls in CF 1.0 as CF 1.0 has no support for Anchoring or docking.

Note the use of property CurrentAutoScaleDimensions.Width - this gives us access to the DPI to which the form is rendered. Quite neat addition back when CF 2.0 was released.

Happy coding :)

No comments: