WPF和控制台应用程序之间的PInvoke GetMonitorInfo的区别

时间:2015-01-22 21:45:36

标签: c# wpf console-application pinvoke

我正在开发一个应用程序(WPF),它可以获取所有连接的显示器的监视器信息。为了进行一些单元测试,快速的结果,我在同一个解决方案中启动了一个控制台应用程序项目。这是我用来获取MonitorInfo的代码(两个项目都称为完全相同的方式,代码位于类库中)。

    [DllImport("user32.dll")]
    static extern bool GetMonitorInfo(IntPtr hmon, ref MonitorInfo mi);

    [DllImport("user32.dll")]
    static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);

public DisplayInfoCollection GetDisplays()
    {
        DisplayInfoCollection col = new DisplayInfoCollection();

        EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
            delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
            {
                MonitorInfo mi = new MonitorInfo();
                mi.size = (uint)Marshal.SizeOf(mi);
                bool success = GetMonitorInfo(hMonitor, ref mi);

                if (success)
                {
                    DisplayInfo di = new DisplayInfo();
                    di.ScreenWidth = (mi.monitor.right - mi.monitor.left);
                    di.ScreenHeight = (mi.monitor.bottom - mi.monitor.top);
                    di.MonitorArea = new Rectangle(mi.monitor.left, mi.monitor.top, di.ScreenWidth, di.ScreenHeight);
                    di.Availability = mi.flags.ToString();
                    col.Add(di);
                }
                return true;
            }, IntPtr.Zero);
        return col;
    }

我注意到我的笔记本电脑屏幕(我有一台笔记本电脑和一个辅助显示器)的显示器尺寸输出在WPF和控制台应用程序之间是不同的。

  

笔记本电脑决议

  • WPF - 1920x1080
  • 控制台应用程序 - 1536x864

我通过“显示” - >“屏幕分辨率”设置获得的分辨率与WPF分辨率相同。我想这与DPI有关,但我很困惑为什么会这样呢?我没有在任何一个应用程序上设置任何DPI意识。默认情况下,WPF应用程序是否具有DPI意识?如果没有,无论消费者是什么,我该怎么做才能使输出相同?

0 个答案:

没有答案
相关问题