I think there is a bug in System.Windows.Forms.VisualStyles.VisualStyleRenderer.GetPartSize().
Found through this link. I created a sample console application that calls this method and prints the width and height of the Size object returned, to the console. Below are some results from our office:
- Vista: Width=20, Height=21
- XP: Width=15, Height=20
- Win7: Width=7, Height=21 <— width returned is wrong
The code is shown below:
var element = VisualStyleElement.ComboBox.DropDownButton.Normal;
var renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(element);
var control = new Control();
using (var gr = control.CreateGraphics())
{
var size = renderer.GetPartSize(gr, ThemeSizeType.True);
Console.WriteLine(String.Format("Width={0},Height={1}", size.Width, size.Height));
}
I can’t find anything on the web to indicate that this is a known problem. Any reason to suspect that it’s not?!
More importantly, do you think it’s safe to just check whether the OS is Win7 and return the correct size instead?
That’s what I get too, 7 x 21:
Yup, that arrow is exactly 7 pixels wide. If you want to measure the size to the left and the right of the arrow then you can use these elements:
Present in .NET 4 but hidden because they are not compatible across operating systems. Values are respectively (6,21) and (5,21) on my machine.