Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetSystemMetrics() returns different results for .NET 4.5 & .NET 4.0

During a .NET 4.0 -> .NET 4.5 application migration process I've discovered an extremely strange behavior. I've been able to track this issue down to this short code snippet:

class Program
{
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern int GetSystemMetrics(int nIndex);

    static void Main(string[] args)
    {
        const int CXFRAME = 0x20;
        const int CYFRAME = 0x21;

        var dx = GetSystemMetrics(CXFRAME);
        var dy = GetSystemMetrics(CYFRAME);

        Console.WriteLine("{0}x{1}", dx, dy);
        Console.ReadKey();
    }
}

When compiled with Target Framework = 4.0 (and also 2.0, 3.0, 3.5), it outputs 8x8

When compiled with Target Framework = 4.5, it outputs 4x4

Running this sample with MSVS2012 debugger also always outputs 4x4 (with any target framework).

Other options (target framework profile, target platform, enabling/disabling Aero) do not affect the result, the only things that change the output are target framework and running with debugger. I've been able to reproduce this issue on 3 computers, unfortunately they are almost identical in terms of installed software:

  • Windows 7 Ultmate SP1 (russian, all updates installed) with MSVS2012 (english/rusian) Update 1
  • Windows 8 (on virtual machine)

Currently I'm thinking about patching some .NET classes (SystemParameters for example), which call GetSystemMetrics() using reflection, but I'm not sure how to get correct metric values.

Questions:

  • Am I missing something? How can GetSystemMetrics() be affected by target framework?
  • Is there any way to call GetSystemMetrics() from .NET 4.5 app and get correct results?

I'm open to any suggestions on fixing this issue. Also, if you cannot reproduce the issue, please leave a short system description in comments.

like image 274
max Avatar asked Feb 06 '13 09:02

max


Video Answer


1 Answers

So, it's actually a by-design behavior, and if someone has similar issues, here is the code which always outputs the same result:

const int CXFRAME = 0x20;
const int CYFRAME = 0x21;
const int CXPADDEDBORDER = 92;

var dx = GetSystemMetrics(CXFRAME);
var dy = GetSystemMetrics(CYFRAME);
var d  = GetSystemMetrics(CXPADDEDBORDER);
dx += d;
dy += d;

Console.WriteLine("{0}x{1}", dx, dy);
Console.ReadKey();

Also note that RibbonWindow WPF control, which uses WindowChrome and now comes as a part of .NET 4.5 does not know about this changes and displays messy window borders (fortunately, I think it can be fixed using modified styles).

like image 64
max Avatar answered Nov 10 '22 21:11

max