public static void HandleSecondInstanceMessages(DpiAwareWindow window, Action <IEnumerable <string> > handler) { HwndSource hwnd = null; HwndSourceHook hook = (IntPtr handle, int message, IntPtr wParam, IntPtr lParam, ref bool handled) => { if (message == SecondInstanceMessage) { try { handler(ReceiveSomeData()); window.BringToFront(); } catch (Exception e) { Logging.Warning("Can’t handle message: " + e); } } return(IntPtr.Zero); }; RoutedEventHandler[] handlers = { null, null }; // loaded handlers[0] = (sender, args) => { window.Loaded -= handlers[0]; try { hwnd = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle); if (hwnd == null) { Logging.Warning("Can’t add one-instance hook: HwndSource is null"); return; } hwnd.AddHook(hook); window.Unloaded += handlers[1]; } catch (Exception e) { Logging.Warning("Can’t add one-instance hook: " + e); hook = null; } }; // unloaded handlers[1] = (sender, args) => { window.Unloaded -= handlers[1]; try { hwnd.RemoveHook(hook); } catch (Exception e) { Logging.Warning("Can’t remove one-instance hook: " + e); hook = null; } }; if (!window.IsLoaded) { window.Loaded += handlers[0]; } else { handlers[0](null, null); } }
private static void UnhandledExceptionFancyHandler(Exception e) { if (!Application.Current.Windows.OfType <Window>().Any(x => x.IsLoaded && x.IsVisible)) { throw new Exception(); } DpiAwareWindow.OnFatalError(e); }
public FrameworkElement GetElement([CanBeNull] DpiAwareWindow parentWindow) { if (_broken) { return(new ExceptionDetails { Title = "Failed to initialize browser engine", Exception = _exception, Suggestions = GetBrokenSuggestions() }); } return(_something.GetElement(parentWindow, _preferTransparentBackground)); }
public FrameworkElement GetElement(DpiAwareWindow parentWindow, bool preferTransparentBackground) { if (_inner == null || _wrapper == null) { DisposeHelper.Dispose(ref _inner); DisposeHelper.Dispose(ref _wrapper); CefSharpHelper.EnsureInitialized(); _downloadHandler = new DownloadHandler(); _requestHandler = new RequestHandler { UserAgent = CefSharpHelper.DefaultUserAgent }; _requestHandler.Inject += OnRequestHandlerInject; _requestHandler.Headers += OnRequestHandlerHeaders; _inner = new ChromiumWebBrowser(@"about:blank") { BrowserSettings = { FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, BackgroundColor = 0xff000000, // For SRS to work, because IsCSPBypassing somehow doesn’t work! WebSecurity = CefState.Disabled, }, DisplayHandler = this, DownloadHandler = _downloadHandler, RequestHandler = _requestHandler, KeyboardHandler = new KeyboardHandler() // MenuHandler = new MenuHandler(), // JsDialogHandler = new JsDialogHandler(), }; _inner.FrameLoadStart += OnFrameLoadStart; _inner.FrameLoadEnd += OnFrameLoadEnd; _inner.LoadingStateChanged += OnLoadingStateChanged; _inner.LoadError += OnLoadError; _wrapper = new WindowsFormsHost { Child = _inner }; } return(_wrapper); }
public FrameworkElement GetElement(DpiAwareWindow parentWindow, bool preferTransparentBackground) { if (_inner == null) { CefSharpHelper.EnsureInitialized(); Initialize(preferTransparentBackground); if (_inner == null) { throw new Exception(@"Initialization failed"); } } _zoomLevel = parentWindow?.ScaleX ?? 1d; if (_zoomLevel > 1d) { _inner.LayoutTransform = new ScaleTransform { ScaleX = 1d / _zoomLevel, ScaleY = 1d / _zoomLevel }; _inner.SetBinding(FrameworkElement.WidthProperty, new Binding { Path = new PropertyPath(FrameworkElement.ActualWidthProperty), Source = _wrapper, Converter = new MultiplyConverter(), ConverterParameter = _zoomLevel }); _inner.SetBinding(FrameworkElement.HeightProperty, new Binding { Path = new PropertyPath(FrameworkElement.ActualHeightProperty), Source = _wrapper, Converter = new MultiplyConverter(), ConverterParameter = _zoomLevel }); } else { _inner.LayoutTransform = Transform.Identity; _inner.ClearValue(FrameworkElement.WidthProperty); _inner.ClearValue(FrameworkElement.HeightProperty); } RenderOptions.SetBitmapScalingMode(_inner, _zoomLevel >= 1d ? BitmapScalingMode.NearestNeighbor : BitmapScalingMode.HighQuality); return(_wrapper); }
public FrameworkElement GetElement(DpiAwareWindow parentWindow, bool preferTransparentBackground) { if (_wrapper != null) { return(_wrapper); } WebBrowserHelper.SetUserAgent(DefaultUserAgent); _inner = new WebBrowser { ScriptErrorsSuppressed = true }; _inner.Navigated += OnNavigated; _inner.Navigating += OnNavigating; _inner.NewWindow += OnNewWindow; _inner.DocumentTitleChanged += OnTitleChanged; _wrapper = new WindowsFormsHost { Child = _inner }; return(_wrapper); }
/// <summary> /// Initializes a new instance of the <see cref="DpiAwarenessViewModel" /> class. /// </summary> public DpiAwarenessViewModel() { this.wnd = (DpiAwareWindow)App.Current.MainWindow; this.wnd.DpiChanged += OnWndDpiChanged; this.wnd.SizeChanged += OnWndSizeChanged; }
public FrameworkElement GetElement(DpiAwareWindow parentWindow, bool preferTransparentBackground) { if (_inner == null) { CefSharpHelper.EnsureInitialized(); _downloadHandler = new DownloadHandler(); _requestHandler = new RequestHandler { UserAgent = CefSharpHelper.DefaultUserAgent }; _requestHandler.Inject += OnRequestHandlerInject; _inner = new ChromiumWebBrowser { BrowserSettings = { FileAccessFromFileUrls = CefState.Enabled, UniversalAccessFromFileUrls = CefState.Enabled, BackgroundColor = preferTransparentBackground ? 0U : 0xffffffff, WindowlessFrameRate = SettingsHolder.Plugins.Cef60Fps ? 60 : 30, WebGl = CefState.Disabled, Plugins = CefState.Disabled, // For SRS to work, because IsCSPBypassing somehow doesn’t work! WebSecurity = CefState.Disabled, }, DisplayHandler = this, DownloadHandler = _downloadHandler, RequestHandler = _requestHandler, MenuHandler = new MenuHandler(), JsDialogHandler = new JsDialogHandler(), KeyboardHandler = new KeyboardHandler(), Background = new SolidColorBrush(preferTransparentBackground ? Colors.Transparent : Colors.White), }; RenderOptions.SetBitmapScalingMode(_inner, BitmapScalingMode.NearestNeighbor); _inner.FrameLoadStart += OnFrameLoadStart; _inner.FrameLoadEnd += OnFrameLoadEnd; _inner.LoadingStateChanged += OnLoadingStateChanged; _inner.LoadError += OnLoadError; _wrapper = new Border { Child = _inner }; } _zoomLevel = parentWindow?.ScaleX ?? 1d; if (_zoomLevel > 1d) { _inner.LayoutTransform = new ScaleTransform { ScaleX = 1d / _zoomLevel, ScaleY = 1d / _zoomLevel }; _inner.SetBinding(FrameworkElement.WidthProperty, new Binding { Path = new PropertyPath(FrameworkElement.ActualWidthProperty), Source = _wrapper, Converter = new MultiplyConverter(), ConverterParameter = _zoomLevel }); _inner.SetBinding(FrameworkElement.HeightProperty, new Binding { Path = new PropertyPath(FrameworkElement.ActualHeightProperty), Source = _wrapper, Converter = new MultiplyConverter(), ConverterParameter = _zoomLevel }); } else { _inner.LayoutTransform = Transform.Identity; _inner.ClearValue(FrameworkElement.WidthProperty); _inner.ClearValue(FrameworkElement.HeightProperty); } RenderOptions.SetBitmapScalingMode(_inner, _zoomLevel >= 1d ? BitmapScalingMode.NearestNeighbor : BitmapScalingMode.HighQuality); return(_wrapper); }
protected sealed override void GoToNormalMode() { if (OptionAttachedToolsVerboseMode) { Logging.Debug("Switching to normal mode…"); } _switchingInProgress = true; try { if (AppearanceManager.Instance.PreferFullscreenMode) { var screen = DpiAwareWindow.GetPreferredDeviceScreen(); Form.Width = screen.Bounds.Width; Form.Height = screen.Bounds.Height; Form.Top = screen.Bounds.Top; Form.Left = screen.Bounds.Left; FullscreenEnabled = true; } else { var size = ValuesStorage.Get(KeyNormalSize, default(Point)); var pos = ValuesStorage.Get(KeyNormalPos, default(Point)); if (size.X > 0 && size.Y > 0) { var savedScreen = pos != default(Point) ? Screen.FromPoint(new System.Drawing.Point( (int)(pos.X + size.X / 2), (int)(pos.Y + size.Y / 2))) : DpiAwareWindow.GetPreferredDeviceScreen(); var activeScreen = DpiAwareWindow.GetForcedDeviceScreen(); if (activeScreen != null && savedScreen.Bounds != activeScreen.Bounds) { SetDefaultLocation(); } else { Form.Width = size.X.RoundToInt().Clamp(320, savedScreen.Bounds.Width); Form.Height = size.Y.RoundToInt().Clamp(200, savedScreen.Bounds.Height); Form.Top = pos.Y.RoundToInt().Clamp(savedScreen.Bounds.Top, savedScreen.Bounds.Bottom - Form.Height); Form.Left = pos.X.RoundToInt().Clamp(savedScreen.Bounds.Left, savedScreen.Bounds.Right - Form.Width); } } else { SetDefaultLocation(); } if (_lastVisibleTools.HasValue) { _helper.Visible = _lastVisibleTools.Value; } FullscreenEnabled = ValuesStorage.Get(KeyNormalFullscreen, false); } void SetDefaultLocation() { var screen = DpiAwareWindow.GetPreferredDeviceScreen(); Form.Width = 1600.Clamp(320, screen.Bounds.Width); Form.Height = 900.Clamp(200, screen.Bounds.Height); Form.Top = screen.Bounds.Top + (screen.Bounds.Height - Form.Height) / 2; Form.Left = screen.Bounds.Left + (screen.Bounds.Width - Form.Width) / 2; } UpdateSize(); } finally { _switchingInProgress = false; } if (OptionAttachedToolsVerboseMode) { Logging.Here(); } }
private Rectangle GetScreenBounds() { return((Form.Visible ? Screen.FromControl(Form) : DpiAwareWindow.GetPreferredDeviceScreen()).Bounds); }
/// <summary> /// Initializes a new instance of the <see cref="DpiAwarenessViewModel" /> class. /// </summary> public DpiAwarenessViewModel() { wnd = (DpiAwareWindow)Application.Current.MainWindow; wnd.DpiChanged += OnWndDpiChanged; wnd.SizeChanged += OnWndSizeChanged; }