How to get real value of Screen Size in C# WPF Application

I am trying to get Screen width and Height of my System on SizeChanged Event.

It works fine in all cases excluding:

  1. Change Screen Size to 1024 X 765 then Run my Application its works fine.

  2. Change Screen Size Again to Another Resolution while my application running got Width and height of my Previous Resolution not Current.

I have used screen.primaryscreen.bounds.width on Window_SizeChanged Event.

Upvotes: 2

Views: 2781

Answers (1)

Chris Fannin
Chris Fannin

Reputation: 1294

You can monitor the SystemEvents for changes.

using Microsoft.Win32;

Add this to your constructor or the like:

SystemEvents.DisplaySettingsChanged += 
    new EventHandler(SystemEvents_DisplaySettingsChanged);

Here's the event handler. I have the data bound to 2 TextBoxes so I could see the values change as I messed with the resolution, including telling it to revert.

  void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
  {
     data.ScreenHeight = Screen.PrimaryScreen.Bounds.Height;
     data.ScreenWidth = Screen.PrimaryScreen.Bounds.Width;
  }

In your case, just change the assignment to point to where you want to store it.

Upvotes: 2

Related Questions