Problems with Screen.PrimaryScreen.Bounds

madagadaguda

Newcomer
Joined
May 20, 2004
Messages
2
I'm writing a WinForms application that needs to update its user area on change of resolution by the user. I've overriden the WinProc method of my main form so that I can monitor the changes of the resolution and do what I need to do. Its body looks like this:

private const int WM_DISPLAYCHANGE = 0x007E; // this is the message notifying that the display resolution has been changed
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);​
if(m.Msg == WM_DISPLAYCHANGE)​
{​
MessageBox.Show("Width: " + Screen.PrimaryScreen.Bounds.Width + "\n" + "Height: " + Screen.PrimaryScreen.Bounds.Height);​
}​
}

The problem is that the Screen.PrimaryScreen.Bounds property doesn't work properly the first time the resolution is changed by the user after the application is started. It returns the previous resolution of the display. Here comes the strange part: all the following changes work fine.
I've tried using the Microsoft.Win32.SystemEvents.DisplaySettingsChanged event but the result is the same. Is there a way to get this right?

10x
K.
 
why don't you use the Microsoft.Win32.SystemEvents.DisplaySettingsChanged event it should work fine ( does for me here ) , eg:
C#:
		[COLOR=Blue]private void[/COLOR] Form1_Load([COLOR=Blue]object[/COLOR] sender, System.EventArgs e)
		{
			Microsoft.Win32.SystemEvents.DisplaySettingsChanged +=[COLOR=Blue]new[/COLOR] System.EventHandler(this.DisplaySettingsChanged);
		}
		[COLOR=Blue]private void[/COLOR] DisplaySettingsChanged([COLOR=Blue]object[/COLOR] sender , System.EventArgs e)
		{
			MessageBox.Show("Display Settings Changed\n" + Screen.PrimaryScreen.Bounds.Width.ToString() + "\n" + Screen.PrimaryScreen.Bounds.Height.ToString());
		}
 
Re:

I tried this already. The situation is quite confusing. It looks like this:
1. I start my app at 1024x768.
2. I change the resolution to 800x600 and the message looks OK.
3. I switch back to 1024 and the app gives me feed back of what it thinks the resolution is: 800x600.
4. This goes on and on... Every time I swtch between resolutions the message reads 800x600. (Unfortunately I work on an 15'' LCD and cannot test other resolutions)

The same thing happens when I start the app at 800x600 (this time the message shows 1024x768)

I was told however that if I used the pervious approach I could get the display dimensions by getting the high- and low- bits of Message.LParam. So I'll focus on this now.

Thanks anyway...

K.
 
Try System.Windows.Forms.SystemInformation.PrimaryMonitorSize in place of Screen.PrimaryScreen.Bounds. Took me about 3 hours to figure out this workaround today.
 
Thanks so much - I was working on a problem with screen rotation, where the Screen.PrimaryScreen.Bounds wasn't updating properly either, so I've been flipping through forums. When I saw this last post, I replaced that with your suggestion. Now, it's working, so thanks for your advice!
 
Back
Top