2010-03-17 4 views

답변

-1

는이 같은 현재 양식의 현재 화면을 얻을 수있는 Screen.FromControl 방법을 사용할 수 있습니다 :

Screen screen = Screen.FromControl(this); 

그런 다음 현재 화면이 기본 화면입니다 있는지 확인하기 위해 Screen.Primary를 확인할 수 있습니다.

+21

WPF'Window'는 폼'Control'이 아니므로이 경우'Screen.FromControl (this) '을 사용할 수 없습니다. – cprcrack

+13

그러나 다음과 같이 사용할 수 있습니다. Screen.FromHandle (new WindowInteropHelper (this) .Handle); –

8

지금까지는 다른 대답을 사용할 수 없어 WPF 부분을 다루지 않았습니다. 내 걸릴거야.

WPF는 다른 응답에서 언급 한 Windows Forms의 Screen 클래스에있는 자세한 화면 정보를 노출하지 않는 것 같습니다.

그러나, 당신은 당신의 WPF 프로그램에서 윈폼 화면 클래스를 사용할 수 있습니다 : 당신이 nitpicker 경우,이 주목 한 수

System.Windows.Forms에 대한 참조를 추가하고 System.Drawing

var screen = System.Windows.Forms.Screen.FromRectangle(
    new System.Drawing.Rectangle(
    (int)myWindow.Left, (int)myWindow.Top, 
    (int)myWindow.Width, (int)myWindow.Height)); 

주 코드는 오른쪽에서 아래쪽으로 한 픽셀 씩 벗어나서 int에서 double로 변환되는 경우가 있습니다. 당신이 nitpicker 때문에 윈도우가 전혀 그들이 할 수 있기 때문에 window.Left 또는 window.Top에 의존 할 수없는 다음을 최대화하는 경우, 당신은 내 코드 ;-)

+1

창이 최대화되면 작동하지 않습니다. –

+0

기본이 아닌 DPI에서는 작동하지 않습니다. 특히 모니터 별 DPI가 다릅니다. – LOST

0
public static bool IsOnPrimary(Window myWindow) 
{ 
    var rect = myWindow.RestoreBounds; 
    Rectangle myWindowBounds= new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height); 
    return myWindowBounds.IntersectsWith(WinForms.Screen.PrimaryScreen.Bounds); 

    /* Where 
     using System.Drawing; 
     using System.Windows; 
     using WinForms = System.Windows.Forms; 
    */ 
} 
+0

'myWindow.RestoreBounds'는 윈도우가 스크린의 왼쪽이나 오른쪽에 고정되어 있으면 윈도우의 실제 크기와 위치를 반환하지 않습니다. (Windows 버튼 + 왼쪽 또는 오른쪽 화살표)이 경우 myWindow.Left, myWindow.Right, myWindow.Width 및 myWindow.Height를 사용해야합니다. –

23

를 해결하기 위해보다 더 행복 할 것이다 그것이 최대화되기 전에 좌표가되어야합니다. 하지만 모든 경우에이 작업을 수행 할 수 있습니다

var screen = System.Windows.Forms.Screen.FromHandle(
     new System.Windows.Interop.WindowInteropHelper(window).Handle); 
+5

현재 열지 않은 곳의 화면이 나타납니다. – Coneone

2

를 당신은 몇 가지 기본 방법을 사용할 필요가 그렇게하기 위해서.

internal static class NativeMethods 
{ 
    public const Int32 MONITOR_DEFAULTTOPRIMARY = 0x00000001; 
    public const Int32 MONITOR_DEFAULTTONEAREST = 0x00000002; 

    [DllImport("user32.dll")] 
    public static extern IntPtr MonitorFromWindow(IntPtr handle, Int32 flags); 
} 

https://msdn.microsoft.com/en-us/library/windows/desktop/dd145064(v=vs.85).aspx

그런 다음 당신은 단순히있는 창문이며, 기본 하나 인 모니터를 확인. 이와 같이 :

 var hwnd = new WindowInteropHelper(this).EnsureHandle(); 
     var currentMonitor = NativeMethods.MonitorFromWindow(hwnd, NativeMethods.MONITOR_DEFAULTTONEAREST); 
     var primaryMonitor = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTOPRIMARY); 
     var isInPrimary = currentMonitor == primaryMonitor; 
관련 문제