How do I know what monitor a WPF window is in

前端 未结 6 1681
攒了一身酷
攒了一身酷 2020-12-01 12:11

In a C# application, how can I find out if a WPF window is in the primary monitor or another monitor?

6条回答
  •  爱一瞬间的悲伤
    2020-12-01 12:16

    Other replies available so far don't address the WPF part of the question. Here's my take.

    WPF doesn't seem to expose detailed screen info as found in Windows Forms' Screen class mentioned in other replies.

    However, you can use the WinForms Screen class in your WPF program:

    Add references to System.Windows.Forms and 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));
    

    Note that if you are a nitpicker, you may have noted that this code could have right and bottom coordinates off by one pixel in some case of double to int conversions. But since you are a nitpicker, you will be more than happy to fix my code ;-)

提交回复
热议问题