namespace Pitorque.Utilities { using System; using System.Windows; using System.Windows.Browser; public class ScreenHelper { /// /// Gets the current monitor's resolution by temporarily switching to full screen mode and back. /// This method requires elevated permissions or must be used in response to user input. /// /// A tuple that contains the width and height of the current monitor, /// or (0,0) if switching to full screen did not succeed. public Tuple GetMonitorResolution() { var content = Application.Current.Host.Content; bool wasFullScreen = content.IsFullScreen; try { // switch to full screen if we're are not already if (!wasFullScreen) { content.IsFullScreen = true; } if (!content.IsFullScreen) { // the switch failed. // this might be due to insufficient rights return new Tuple(0, 0); } // get the screen size int width = (int)Math.Round(content.ActualWidth); int height = (int)Math.Round(content.ActualHeight); return new Tuple(width, height); } finally { // switch back to previous state if necessary if (content.IsFullScreen != wasFullScreen) { content.IsFullScreen = wasFullScreen; } } } /// /// Gets the monitor resolution asyncronously by temporarily switching to full screen mode and back. /// This method requires elevated permissions and also works in UI event handlers (e.g. FrameworkElement.Loaded). /// /// An action that is called when the monitor resolution was determined. /// The arguments contain the width and height of the current screen, or zero if the switch to /// full screen did not succeed. public void GetMonitorResolutionAsync(Action callback) { if (!Application.Current.HasElevatedPermissions) { throw new NotSupportedException("This method requires elevated permissions."); } var content = Application.Current.Host.Content; var dispatcher = Application.Current.MainWindow.Dispatcher; if (!content.IsFullScreen) { // do this asynchronously; that will also work // in certain event handlers like FrameworkElement.Loaded dispatcher.BeginInvoke(() => { // switch to full screen content.IsFullScreen = true; if (!content.IsFullScreen) { // something went wrong callback(0, 0); return; } int width = (int)Math.Round(content.ActualWidth); int height = (int)Math.Round(content.ActualHeight); // switch back to windowed mode content.IsFullScreen = false; // send the results callback(width, height); }); } else { // if we're already in full screen mode, we can simply // return the actual width and height values dispatcher.BeginInvoke(() => { int width = (int)Math.Round(content.ActualWidth); int height = (int)Math.Round(content.ActualHeight); callback(width, height); }); } } /// /// Uses the Window.Eval method of HtmlPage to execute JavaScript code to determine the current monitor resolution. /// This only works when the application is running in browser. /// /// A tuple that contains the width and height of the current monitor. public Tuple GetMonitorResolutionByJavaScript() { int width = (int)Math.Round((double)HtmlPage.Window.Eval("screen.width")); int height = (int)Math.Round((double)HtmlPage.Window.Eval("screen.height")); return new Tuple(width, height); } } }