WPF 多屏时子窗口的屏幕位置问题
2017-08-23 23:16
441 查看
设置多屏时窗口居中显示
问题:
在多个显示屏运行的情况下,如果将主窗口从当前显示屏移动到另一显示屏。
设置子窗口单例模式,在当前显示屏时弹出后,在主窗口移动到另一显示屏后,再弹出子窗口时,你会发现子窗口跑到原来显示屏去了。
----这是WPF的锅
因为已经设置了WindowStartupLocation="CenterOwner",也加了Owner的情况下,窗口每次弹出,理论上就该和主窗口保持在同一屏幕的。
解决:
通过窗口的Activated添加委托,每次窗口唤醒,都重新设置窗口的Location
获取当前主窗口所在的屏幕。
详细:
通过给Window添加一个附加属性即可
PS:
当窗口状态为最大化时,即WindowState=“Maximized”,只需要设置位置即可
问题:
在多个显示屏运行的情况下,如果将主窗口从当前显示屏移动到另一显示屏。
设置子窗口单例模式,在当前显示屏时弹出后,在主窗口移动到另一显示屏后,再弹出子窗口时,你会发现子窗口跑到原来显示屏去了。
----这是WPF的锅
因为已经设置了WindowStartupLocation="CenterOwner",也加了Owner的情况下,窗口每次弹出,理论上就该和主窗口保持在同一屏幕的。
解决:
通过窗口的Activated添加委托,每次窗口唤醒,都重新设置窗口的Location
subWindow.Left = screen.Bounds.Left + (screen.Bounds.Width - subWindow.Width) / 2; subWindow.Top = screen.Bounds.Top + (screen.Bounds.Height - subWindow.Height) / 2;
获取当前主窗口所在的屏幕。
var screen = Screen.FromHandle(new WindowInteropHelper(mainWindow).Handle);
详细:
通过给Window添加一个附加属性即可
helper:WindowScreenHelper.IsWindowShowInCurrentScreen="True"
public class WindowScreenHelper { public static readonly DependencyProperty IsWindowShowInCurrentScreenProperty = DependencyProperty.RegisterAttached( "IsWindowShowInCurrentScreen", typeof(bool), typeof(WindowScreenHelper), new PropertyMetadata(default(bool), ShowWindowInCurrentScreen)); private static void ShowWindowInCurrentScreen(DependencyObject d, DependencyPropertyChangedEventArgs e) { var window = d as Window; if (window != null) { window.Activated += ShowInCurrentScreenWindow_Activated; } } private static void ShowInCurrentScreenWindow_Activated(object sender, EventArgs e) { var subWindow = sender as Window; if (subWindow == null) return; if (GetIsWindowShowInCurrentScreen(subWindow)) { var mainWindow = App.Current.MainWindow; if (mainWindow == null) return; var screen = Screen.FromHandle(new WindowInteropHelper(mainWindow).Handle); if (subWindow.WindowState== WindowState.Maximized) { //最大化窗口,固定的弹出到主屏幕,因此需额外处理 subWindow.Left = screen.Bounds.Left / Dpi.System.FactorX; subWindow.Top = screen.Bounds.Top / Dpi.System.FactorY; } else { //窗口居中显示 subWindow.Left = screen.Bounds.Left / Dpi.System.FactorX + (screen.Bounds.Width / Dpi.System.FactorX - subWindow.Width) / 2; subWindow.Top = screen.Bounds.Top / Dpi.System.FactorY + (screen.Bounds.Height / Dpi.System.FactorY - subWindow.Height) / 2; } } } public static void SetIsWindowShowInCurrentScreen(DependencyObject element, bool value) { element.SetValue(IsWindowShowInCurrentScreenProperty, value); } public static bool GetIsWindowShowInCurrentScreen(DependencyObject element) { return (bool)element.GetValue(IsWindowShowInCurrentScreenProperty); } }
PS:
当窗口状态为最大化时,即WindowState=“Maximized”,只需要设置位置即可
相关文章推荐
- WPF 获取鼠标屏幕位置、窗口位置、控件位置
- 屏幕变小后,wpf窗口被截掉的问题。
- 重设PowerDesigner软件启动时窗口显示的位置,解决PowerDesigner 多屏幕显示不见了问题
- WPF疑难杂症之二(全屏幕窗口)
- 《Applications=Code+Markup》读书札记(2)——创建一个简单的 WPF 程序的代码结构及关于 Window 实例位置设置问题
- vtkBorderWidget设置窗口位置的问题
- Android中如何指定SnackBar在屏幕的位置及小问题解决
- WPF中获取鼠标相对于屏幕的位置
- js获取窗口相对于屏幕左边和上边的位置坐标
- Android:解决打开activity之后 屏幕初始位置不是顶部问题
- javascript:window.open弹出窗口的位置问题
- java swing窗口放置屏幕中央问题思考
- winform中坐标系转换的问题,获取某点在屏幕中的绝对位置等
- 当前位置: 银光首页 > WPF > WPF学习教程 > WPF: ShowDialog() 切换到其他应用窗口后,再切换回来无法让子窗口总在最上方
- android中获取控件在屏幕和窗口的位置。
- windows10 双屏幕扩展后,窗口拖动方向的问题。
- 设置WPF窗口相对于非WPF窗口的位置
- WPF C# 多屏情况下,实现窗体显示到指定的屏幕内
- WPF中控制窗口显示位置的三种方式
- WPF中获取鼠标相对于屏幕的位置