ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C#에서 바탕화면 해상도 얻기
    카테고리 없음 2019. 2. 22. 19:13
    반응형

    현재 바탕화면의 해상도는 3840 x 2160 입니다.




           [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int Left;        // x position of upper-left corner
                public int Top;         // y position of upper-left corner
                public int Right;       // x position of lower-right corner
                public int Bottom;      // y position of lower-right corner
            }
     
            [DllImport("user32.dll", SetLastError = true)]
            static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
     
            [DllImport("user32.dll", SetLastError = false)]
            public static extern IntPtr GetDesktopWindow();
     
            [DllImport("gdi32.dll")]
            public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
     
            [DllImport("user32.dll")]
            public static extern IntPtr GetDC(IntPtr hWnd);
    
            public const int DESKTOPVERTRES = 117;
            public const int DESKTOPHORZRES = 118;
    
            IntPtr handle = GetDesktopWindow();
            GetWindowRect(handle, out RECT windowRect);
            // 2560 x 1440
    
            IntPtr hdc = GetDC(IntPtr.Zero);
            var width = GetDeviceCaps(hdc, DESKTOPHORZRES);
            var height = GetDeviceCaps(hdc, DESKTOPVERTRES);
            // 3840 x 2160
    

     바탕화면의 핸들을 얻어서 GetWindowRect로 영역을 구하면 2560 x 1440이라는 값이 나옵니다. 이유는 디스플레이 설정에서 스케일 값을 150%로 하였는데 해당 값만큼 축소된 값이 적용되서 구해집니다.

    스케일이 적용되지 않은 해상도를 구하려면 GetDeviceCaps를 이용하면 됩니다.

     


    반응형
Designed by Tistory.