madagadaguda Posted May 20, 2004 Posted May 20, 2004 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. Quote
Leaders dynamic_sysop Posted May 20, 2004 Leaders Posted May 20, 2004 why don't you use the Microsoft.Win32.SystemEvents.DisplaySettingsChanged event it should work fine ( does for me here ) , eg: [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()); } Quote
madagadaguda Posted May 21, 2004 Author Posted May 21, 2004 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. Quote
OyVey Posted October 11, 2005 Posted October 11, 2005 Try System.Windows.Forms.SystemInformation.PrimaryMonitorSize in place of Screen.PrimaryScreen.Bounds. Took me about 3 hours to figure out this workaround today. Quote
tm90 Posted July 21, 2008 Posted July 21, 2008 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! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.