C#: Get complete desktop size?

前端 未结 9 2129
梦谈多话
梦谈多话 2020-11-27 16:40

How do I find out the size of the entire desktop? Not the \"working area\" and not the \"screen resolution\", both of which refer to only o

9条回答
  •  醉话见心
    2020-11-27 16:58

    To get the physical pixel size of the monitor you can use this.

    static class DisplayTools
    {
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
    
        private enum DeviceCap
        {
            Desktopvertres = 117,
            Desktophorzres = 118
        }
    
        public static Size GetPhysicalDisplaySize()
        {
            Graphics g = Graphics.FromHwnd(IntPtr.Zero);
            IntPtr desktop = g.GetHdc();
    
            int physicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.Desktopvertres);
            int physicalScreenWidth = GetDeviceCaps(desktop, (int)DeviceCap.Desktophorzres);
    
            return new Size(physicalScreenWidth, physicalScreenHeight);
        }
    
    
    }
    

提交回复
热议问题