2009-08-22 7 views
65

전체 데스크탑의 크기는 어떻게 알 수 있습니까? "작업 영역"및 아니요 "화면 해상도"둘 다 하나의 화면 만 참조하십시오. 각 모니터가 일부만 보여주고있는 가상 데스크톱의 전체 너비와 높이를 찾고 싶습니다.C# : 전체 데스크톱 크기를 얻으시겠습니까?

+2

"전체 데스크톱 크기"는 화면의 배치 방식에 따라 달라질 수 있습니다. 계산 방법을 모르지만 System.Windows.Forms.Screen.AllScreens를 사용하여 각 화면의 수와 해상도를 파악할 수 있습니다. – Havenard

답변

119

당신은 두 가지 옵션이 있습니다

  1. 을 PresentationFramework.dll

    SystemParameters.VirtualScreenWidth 
    SystemParameters.VirtualScreenHeight 
    
  2. 시스템.Windows.Forms.dll

    SystemInformation.VirtualScreen.Width 
    SystemInformation.VirtualScreen.Height 
    

사용 첫 번째 옵션 당신이 WPF 응용 프로그램을 개발합니다.

+0

더 많은 답변보기 짧고, 정확하며, 심미적으로 즐겁다! – Bitterblue

26

이 답변을 최신 LINQ로 가져와 한 번 표현으로 전체 데스크톱 크기를 쉽게 가져올 시간이라고 생각합니다.

Console.WriteLine(
    Screen.AllScreens.Select(screen=>screen.Bounds) 
    .Aggregate(Rectangle.Union) 
    .Size 
); 

내 원래의 대답은 다음과 같습니다

int minx, miny, maxx, maxy; 
minx = miny = int.MaxValue; 
maxx = maxy = int.MinValue; 

foreach(Screen screen in Screen.AllScreens){ 
    var bounds = screen.Bounds; 
    minx = Math.Min(minx, bounds.X); 
    miny = Math.Min(miny, bounds.Y); 
    maxx = Math.Max(maxx, bounds.Right); 
    maxy = Math.Max(maxy, bounds.Bottom); 
} 

Console.WriteLine("(width, height) = ({0}, {1})", maxx - minx, maxy - miny); 

이 전체 이야기를하지 않는다는 것을 명심하십시오 :


내가 당신이 원하는 것은이 같은 것 같다. 여러 대의 모니터를 엇갈리게 배치하거나 직사각형이 아닌 모양으로 배열 할 수 있습니다. 따라서 (minx, miny)와 (maxx, maxy) 사이의 공간이 모두 보이지 않을 수도 있습니다.

는 편집 :

난 그냥 Rectangle.Union를 사용하여 코드가 조금 더 간단 할 수 있다는 것을 깨달았다

Rectangle rect = new Rectangle(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue); 

foreach(Screen screen in Screen.AllScreens) 
    rect = Rectangle.Union(rect, screen.Bounds); 

Console.WriteLine("(width, height) = ({0}, {1})", rect.Width, rect.Height); 
+2

저는 우크라이나 프로그래머입니다. 이 코드가 상자에서 작동하는지 확인 중입니다. 복사하여 붙여 넣기 만하면됩니다. –

5

이 질문에 대답하지 않지만, 단지 윈도우의 포인트에 추가 통찰력을 추가합니다 (위치) 모든 화면 내에서).

지점 (예 : 마지막으로 알려진 창 위치)이 전체 데스크톱 범위 내에 있는지 알아 보려면 아래 코드를 사용하십시오. 그렇지 않은 경우 창의 위치를 ​​기본값 인 으로 재설정하십시오. pBaseLoc;

코드는 작업 표시 줄이나 다른 도구 모음을 고려하지 않습니다.

예제 사용 : 스테이션 A에서 데이터베이스 위치를 저장하십시오. 사용자가 스테이션 B에 2 개의 모니터로 로그인하고 창을 두 번째 모니터로 이동 한 후 새 위치를 로그 아웃합니다. 스테이션 A으로 돌아 가면 위의 코드를 사용하지 않으면 창이 표시되지 않습니다.

사용자 아이디와 스테이션의 IP (& winLoc)를 주어진 앱의 데이터베이스 또는 로컬 사용자 환경 설정 파일에 저장 한 다음 해당 스테이션 & app의 사용자 pref에로드합니다.

Point pBaseLoc = new Point(40, 40) 
int x = -500, y = 140; 
Point pLoc = new Point(x, y); 
bool bIsInsideBounds = false; 

foreach (Screen s in Screen.AllScreens) 
{ 
    bIsInsideBounds = s.Bounds.Contains(pLoc); 
    if (bIsInsideBounds) { break; } 
}//foreach (Screen s in Screen.AllScreens) 

if (!bIsInsideBounds) { pLoc = pBaseLoc; } 

this.Location = pLoc; 
+6

이 질문에 답할 수 없다는 것을 알고 계시다면 왜 그 질문에 대한 답변으로 게시하고 있습니까? 첫 번째 문장이 경고음을 울려서는 안됩니까? – Timwi

+1

이 다른 한편으로, 이것은 정확히 제가이 주제에 관해서 찾아 왔을 때 찾고 있었던 것입니다. –

14

확인 :

SystemInformation.VirtualScreen.Width 
SystemInformation.VirtualScreen.Height 
0

범위는 System.Drawing입니다.

당신이

public System.Windows.Form.Screen[] GetScreens(){ 
    Screen[] screens = Screen.AllScreens; 
    return screens; 
} 

같은 함수를 만들 수 있습니다

이 같은 변수 등 화면 한 개, 두 개를 얻을 수있는 것보다 :

System.Windows.Form.Screen[] screens = func.GetScreens(); 
System.Windows.Form.Screen screen1 = screens[0]; 

당신이 얻을 수있는 경계 화면의 :

System.Drawing.Rectangle screen1Bounds = screen1.Bounds; 

이 코드를 사용하면 Width 같은 모든 속성을 얻을 것이다 , Height 등이 방법은 왼쪽과 위쪽, 가장 낮은 값과 오른쪽과 아래쪽 가장 높은 값을 사용하여 화면 '경계를 모두 포함하는 사각형을 반환

0

...

static Rectangle GetDesktopBounds() { 
    var l = int.MaxValue; 
    var t = int.MaxValue; 
    var r = int.MinValue; 
    var b = int.MinValue; 
    foreach(var screen in Screen.AllScreens) { 
     if(screen.Bounds.Left < l) l = screen.Bounds.Left ; 
     if(screen.Bounds.Top < t) t = screen.Bounds.Top ; 
     if(screen.Bounds.Right > r) r = screen.Bounds.Right ; 
     if(screen.Bounds.Bottom > b) b = screen.Bounds.Bottom; 
    } 
    return Rectangle.FromLTRB(l, t, r, b); 
} 
+0

조금 설명해주세요. –

+0

모든 화면을 반복합니다. 왼쪽 가장자리가 변수 'l'보다 작 으면 'l'을이 새 값으로 설정하고, 아래 가장자리가 'b'보다 큰 경우이를 설정합니다. 최종 결과는 l, t, r, b가 전체 보이는 바탕 화면의 왼쪽, 위쪽, 오른쪽 및 아래쪽 가장자리로 설정됩니다. – dynamichael

0

"실제"화면 크기를 얻는 가장 좋은 방법은 비디오 컨트롤러에서 직접 값을 가져 오는 것입니다.


using System; 
    using System.Management; 
    using System.Windows.Forms; 

    namespace MOS 
    { 
     public class ClassMOS 
     { 
      public static void Main() 
      { 
       try 
       { 
        ManagementObjectSearcher searcher = 
         new ManagementObjectSearcher("root\\CIMV2", 
         "SELECT * FROM Win32_VideoController"); 

        foreach (ManagementObject queryObj in searcher.Get()) 
        { 
         Console.WriteLine("CurrentHorizontalResolution: {0}", queryObj["CurrentHorizontalResolution"]); 
         Console.WriteLine("-----------------------------------"); 
         Console.WriteLine("CurrentVerticalResolution: {0}", queryObj["CurrentVerticalResolution"]); 
        } 
       } 
       catch (ManagementException e) 
       { 
        MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); 
       } 
      } 
     } 
} 

이 일을해야

) 인사 ...

4

은 당신이 사용할 수있는 모니터의 실제 픽셀 크기를 얻으려면.

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); 
    } 


} 
관련 문제