-1

I am able to find all the screens dimensions using below code but how can I find which screen my application is about to run?

public partial class MainWindow : Window
{
    public MainWindow()
    {
       int length = System.Windows.Forms.Screen.AllScreens.Length;
    }
}
Alyesh
  • 19
  • 1
  • 4
  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.screen.primaryscreen?view=netcore-3.1#System_Windows_Forms_Screen_PrimaryScreen – Bruno Jul 13 '20 at 14:29

1 Answers1

0

You can use WindowInteropHelper to get a handle of your window. Then use the Screen.FromHandle method to obtain the Screen which offers more information like Bounds, BitsPerPixel, etc..

The Screen.FromHandle method works as stated by the docs:

A Screen for the display that contains the largest region of the object. In multiple display environments where no display contains any portion of the specified window, the display closest to the object is returned.

    using System;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Interop;
    
    namespace WpfApp2
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
    
            private readonly WindowInteropHelper _windowInteropHelper;
    
            public MainWindow()
            {
                InitializeComponent();
                _windowInteropHelper = new WindowInteropHelper(this);
    
                LocationChanged += OnLocationChanged;
            }
    
            private void OnLocationChanged(object sender, EventArgs e)
            {
                Screen screen = Screen.FromHandle(_windowInteropHelper.Handle);
                Debug.WriteLine($"IsPrimary: {screen.Primary}, Bounds: {screen.Bounds}");
            }
    
        }
    }

If you want to get rid of System.Windows.Forms you need to write your own Wrapper class around the Multiple Display Monitors Functions

Quergo
  • 888
  • 1
  • 8
  • 21