WPF: Detect Screen Resolution Change (without constantly polling SystemParameters)?

J

Jules Winfield

Hello Friend,

I need to take a particular non-UI-related action when the user changes the
screen resolution. How can I detect when the screen resolution is changed by
the user?

Here's my first approach. I created an internal class called ScreenSize that
exposes a dependency property called ScreenWidth:

internal class ScreenSizeependencyObject{

public DependencyProperty
ScreenWidthProperty=DependencyProperty.Register("ScreenWidth",typeof(double),typeof(ScreenSize),new
PropertyMetadata(new PropertyChangedCallback(OnScreenWidthChanged)));

public double ScreenWidth{
get{return (double)this.GetValue(ScreenWidthProperty);}
set{this.SetValue(ScreenWidthProperty,value);}
}

private static void OnScreenWidthChanged(DependencyObject
d,DependencyPropertyChangedEventArgs e){
Debug.Print("Changed");
}

}

...and then I defined an instance of this class in the resources section of
the XAML file for my application:

<ResourceDictionary>
<aiacreenSize x:Key="ScreenSize" ScreenWidth="{DynamicResource {xtatic
SystemParameters.PrimaryScreenWidthKey}}"/>
</ResourceDictionary>

My expectation was that OnScreenWidthChanged() would be called whenever the
user manually changes the resolution. The reality, however, is that
OnScreenWidthChanged() is called once at startup and then never again --
even if the screen resolution is manually changed. Do you have an alternate
approach?

Thanks,


Jules
 
L

Linda Liu

Hi Jules,

Based on my understanding,you'd like to get notified in your WPF application
when the user changes the screen resolution. If I'm off base, please feel
free to let me know.

In your scenario, you use an instance of the ScreenSize class in the
resource dictionary within a window and bind the
SystemParameters.PrimaryScreenWidthKey to the instance's ScreenWidth
dependency property. When the application starts up, this ScreenWidth
property is assigned. However, the ScreenSize instance used in the resource
section doesn't have a resource tree, so when the system parameter changes,
the ScreenWidth property won't be updated immediately.

If what you really want is only to get notified in the application when the
screen resolution is changed, I suggest that you subscribe and handle the
DisplaySettingsChanged event of SystemEvents in the window.

For example:

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
SystemEvents.DisplaySettingsChanged += new
EventHandler(SystemEvents_DisplaySettingsChanged);
}

void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
Debug.WriteLine("display settings changed");
}
}

Hope this hleps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support Engineer
within 1 business day is acceptable. Please note that each follow up
response may take approximately 2 business days as the support professional
working with you may need further investigation to reach the most efficient
resolution. The offering is not appropriate for situations that require
urgent, real-time or phone-based interactions or complex project analysis
and dump analysis issues. Issues of this nature are best handled working
with a dedicated Microsoft Support Engineer by contacting Microsoft Customer
Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jules Winfield

If what you really want is only to get notified in the application when
the screen resolution is changed, I suggest that you subscribe and handle
the DisplaySettingsChanged event of SystemEvents in the window.

Thanks. That's exactly the answer I needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top