Ree*_*sey 97
那么,对于启动时间,您可以设置启动位置:
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
Run Code Online (Sandbox Code Playgroud)
稍后,您需要查询它.可通过SystemParameters.PrimaryScreenWidth/Height 获取信息(至少对于主屏幕).
Nop*_*det 74
private void CenterWindowOnScreen()
{
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用此方法将窗口位置设置为屏幕中心.
nas*_*kew 70
设置不是那么简单
WindowStartupLocation="CenterScreen"
Run Code Online (Sandbox Code Playgroud)
在窗口的XAML定义中.
Wil*_*d_A 22
Rect workArea = System.Windows.SystemParameters.WorkArea;
this.Left = (workArea.Width - this.Width) / 2 + workArea.Left;
this.Top = (workArea.Height - this.Height) / 2 + workArea.Top;
Run Code Online (Sandbox Code Playgroud)
这考虑了任务栏的大小(通过使用System.Windows.SystemParameters.WorkArea
)和位置(通过添加workArea.Left
和workArea.Top
)
Jum*_*zza 15
我必须结合其中一些答案来涵盖我案例中的所有基础:
workarea
而不是screen bounds
考虑任务栏的空间.dpiScaling
如果在显示UI之前首次加载时调用该变量,则该变量为null(在此处说明)//get the current monitor
Screen currentMonitor = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow).Handle);
//find out if our app is being scaled by the monitor
PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
double dpiScaling = (source != null && source.CompositionTarget != null ? source.CompositionTarget.TransformFromDevice.M11 : 1);
//get the available area of the monitor
Rectangle workArea = currentMonitor.WorkingArea;
var workAreaWidth = (int)Math.Floor(workArea.Width*dpiScaling);
var workAreaHeight = (int)Math.Floor(workArea.Height*dpiScaling);
//move to the centre
Application.Current.MainWindow.Left = (((workAreaWidth - (myWindowWidth * dpiScaling)) / 2) + (workArea.Left * dpiScaling));
Application.Current.MainWindow.Top = (((workAreaHeight - (myWindowHeight * dpiScaling)) / 2) + (workArea.Top * dpiScaling));
Run Code Online (Sandbox Code Playgroud)
其中,myWindowWidth
和myWindowHeight
是我用手动设置的窗口前面大小的变量.
小智 7
如果您需要在多屏幕环境中绘制窗口.我已经创建了一个静态类,可以重用以下方法:
public static void PostitionWindowOnScreen(Window window, double horizontalShift = 0, double verticalShift = 0)
{
Screen screen = Screen.FromHandle(new System.Windows.Interop.WindowInteropHelper(window).Handle);
window.Left = screen.Bounds.X + ((screen.Bounds.Width - window.ActualWidth) / 2) + horizontalShift;
window.Top = screen.Bounds.Y + ((screen.Bounds.Height - window.ActualHeight) / 2) + verticalShift;
}
Run Code Online (Sandbox Code Playgroud)
在Window的构造函数中,现在只需调用方法:
this.Loaded += (s, a) => Globals.PostitionWindowOnScreen(this, 0, 0)
Run Code Online (Sandbox Code Playgroud)
小智 5
在window元素中只需添加此属性 - 值对: WindowStartupLocation ="CenterScreen"
归档时间: |
|
查看次数: |
74315 次 |
最近记录: |