Monday, 18 June 2012

Using the System.Windows.Forms.Form ClientSize property to fix its different sizes on different windows 7 themes

Here is a really annoying C#/Windows GUI behaviour created by the fact that current Windows 7 theme affects the height of a control (namely is main Panel).

Using O2, here is a script that creates a simple form:


The idea is that this should create a popup window with a Form with width of 400 and height of 40 and a panel inside it that always have the same size  (ie the width and height values of the inner panel should be consistent across multiple versions of Windows).

But that doesn't happen

This is what originally happened in Windows XP (or Windows 7 when we have the 'Windows Classic' Theme):



And this is what happens when we use the Windows 7 Basic theme (note the difference in size of the inner panel).



If you don't think that this is a problem, look at the differences between these two O2 GUIs (which is the script Util- Stop all (but current) O2_XRules_Database processes.h2)



and


The solution is to use the ClientSize property to ensure that we have the right size

Which will give us a consistent  panel size on both Windows Themes:


and


This fix has been added to the latest version of O2 Platform (with some O2 GUIs showing in a slightly bigger window, but at least the size of the main panel is consistent across windows themes)

There is also a new extension method that allows the easy modification of a Form's ClientSide value (in a thread safe way):



example of using this extension method: