private void DisplaySettingsChanged(object sender, EventArgs e) { //resolve any screen orientation change DisplayUtil.CheckScreenOrientation(Handle); //end double oldLeft = this.Left; double oldTop = this.Top; //resolve this problem: if screen resolution change, video windows show improperly //this.ChangeWindowState(WindowState.Normal);//if window is minimized, codes below will not get right result this.RestoreWindow(); //if window is minimized, codes below will not get right result Screen screen = DpiUtil.GetScreenByHandle(Handle); double ratio = 1; try { uint dpiX = 0; uint dpiY = 0; DpiUtil.GetDpiByScreen(screen, out dpiX, out dpiY); ratio = dpiX / 96d; if (ratio <= 0) { ratio = 1; } } catch (DllNotFoundException ex) { ratio = 1; log.Warn("Cannot adjust window size for : " + ex.Message); } if (this.Left >= screen.Bounds.Left / ratio && this.Left < ((screen.Bounds.Left + screen.Bounds.Width) / ratio)) { //window is not cross screen, nothing todo } else { //reset default size when window cross screen double height = screen.Bounds.Height / 1.2d; double width = height * _initialWidth / _initialHeight; Utils.MySetWindowPos(Handle, new Rect(screen.Bounds.Left + (screen.Bounds.Width - width) / 2, screen.Bounds.Top + (screen.Bounds.Height - height) / 2, width, height)); _displaySettingChangedTime = DateTime.Now; //do this trick to refresh layout of all the video windows this.MinimizeWindow(); this.RestoreWindow(); //end } }
public void AdjustWindowSize() { if (this._maximized) { return; } try { WinInfo winInfo = GetWindowInfo(); uint dpiX = 0; uint dpiY = 0; Screen currentScreen = DpiUtil.GetScreenByHandle(Handle); DpiUtil.GetDpiByScreen(currentScreen, out dpiX, out dpiY); double ratioX = ((double)dpiX / 96d); double ratioY = ((double)dpiY / 96d); double maxCx = (double)winInfo.maxCx / ratioX; double maxCy = (double)winInfo.maxCy / ratioY; log.InfoFormat("Adjust window size, width={0}, height={1}, maxCx={2}, maxCy={3}, left={4}, top={5}, WindowState={6}, hash code={7}", this.Width, this.Height, maxCx, maxCy, this.Left, this.Top, this.WindowState, this.GetHashCode()); // when the screen size is less than the specified size of main window, then set the size of main window // to a proper size. if (_initialWidth >= maxCx || _initialHeight >= maxCy) { double ratio = _initialWidth / (_initialHeight - _titlebarHeight); if ((maxCx / (maxCy - _titlebarHeight)) > ratio) { this.Height = maxCy; this.Width = (maxCy - _titlebarHeight) * ratio; } else { this.Width = maxCx; this.Height = maxCx / ratio + _titlebarHeight; } Utils.MySetWindowPos(Handle, new Rect(currentScreen.Bounds.Left + (currentScreen.Bounds.Width - this.Width * ratioX) / 2, currentScreen.Bounds.Top + (currentScreen.Bounds.Height - this.Height * ratioY) / 2, this.Width * ratioX, this.Height * ratioY)); log.InfoFormat("window size is adjusted, width={0}, height={1}, hash code={2}", this.Width, this.Height, this.GetHashCode()); //_maxWinWhenLoaded = true; //FullScreenStatus = false; //ChangeWindowState(WindowState.Maximized); } } catch (DllNotFoundException e) { log.Debug("Cannot adjust window size for dll not found."); } }