C# WPF窗口在主屏幕内设置自定义启动位置/位置

C# WPF窗口在主屏幕内设置自定义启动位置/位置,c#,wpf,xaml,window,wpf-controls,C#,Wpf,Xaml,Window,Wpf Controls,我有一个自定义的messagebox在wpf中完成 自定义messagebox视图xaml: <Window x:Class="My.XAML.Controls.Windows.WpfMessageBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我有一个自定义的messagebox在wpf中完成

自定义messagebox视图xaml

<Window x:Class="My.XAML.Controls.Windows.WpfMessageBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WpfMessageBox"  MinHeight="160" 
        MinWidth="420" MaxHeight="750" MaxWidth="750" 
        Background="Transparent" 
        SizeToContent="WidthAndHeight" 
        WindowStartupLocation="CenterScreen"
        ShowInTaskbar="False" ResizeMode="NoResize" 
        WindowStyle="None" Topmost="True">

</Window>
*xaml.cs后面的自定义messagebox代码:

public partial class WpfMessageBox : Window
{
    private WpfMessageBox()
    {
        InitializeComponent();
    }

    public static MessageBoxResult Show(string caption, string text, MessageBoxButton button, MessageBoxImage image, EnumLocation location)
    {
          switch (location)
          {
               case EnumLocation.TopLeft:
                     // Locates at top left
                     break;
               case EnumLocation.TopCenter:
                     // Locates at top center
                     break;
               case EnumLocation.TopRight:
                     // Locates at top right
                     break;

               // and so on with the rest of cases: middle left, middle center, middle right, bottom left, bottom center and bottom right.

          }
    }
}
默认情况下,此自定义消息框在主屏幕中央打开

我现在要做的是将一个参数(枚举)传递给我的WpfMessageBox.Show方法,以指示我的自定义消息框在主屏幕中的位置。参数如下:

  • 左上角
  • 上中锋
  • 右上角
  • 中左派
  • 中心
  • 中右翼
  • 波顿左
  • 底部中心
  • 右下角
我该怎么做

我该怎么做

将窗口的
WindowStartupLocation
属性设置为
Manual
,然后设置窗口的
Left
Top
属性以确定其初始位置

//top-left:
WindowStartupLocation = WindowStartupLocation.Manual;
Left = 0;
Top = 0;

您需要计算要使用的值,例如使用
SystemParameters.PrimaryScreenWidth
SystemParameters.PrimaryScreenHeight
属性。

在xaml中设置手动后,转到c#并相应地设置位置

 // "TL", "TR", "BL", "BR" };
            switch (position)
            {
                case "TL":
                    {
                        this.Left = 0;
                        this.Top = 0;
                        break;
                    }
                case "TR":
                    {
                        this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
                        this.Top = 0;
                        break;
                    }
                case "BL":
                    {
                        this.Left = 0;
                        this.Top = SystemParameters.WorkArea.Height - Height;
                        break;
                    }
                case "BR":
                    {
                        this.Left = SystemParameters.WorkArea.Width - Width;
                        this.Top = SystemParameters.WorkArea.Height - Height;
                        break;
                    }
            }

您必须首先调查它是否“在主屏幕的中心打开”。在这之后,它变得有点复杂,但是。在打开窗口之前设置窗口的Left和Top属性。我将父窗口(窗口)以及枚举TopLeft、topmidle等作为参数传递给WpfMessageBox中的“Show”方法。在“Show”方法中,我使用子窗口(WpfMessageBox)的顶部和左侧属性。但是我在设置其余位置时遇到了问题,例如,我设置了TopRight:WpfMessageBox.Top=parent.Top;WpfMessageBox.Left=(parent.Left+parent.Width)-WpfMessageBox.Width;但在这种情况下,它不起作用。另外,您能帮助我如何设置其余位置TopCenter、TopRight、MiddleLeft、MiddleCenter(WindowsStartupLocation.CenterScreen)、MiddleRight、BottomLeft、,BottomCenter和BottomRight?所以不允许其他人为您编写代码…您询问如何定位主窗口,我已经回答了这个问题。是的,您已经回答了,但只是部分回答了。显示如何仅设置左上角。剩下的我要安排。无论如何,我接受你的答案,打开一个新的,因为我有问题,以设置子窗口(messagebox)内的主窗口的家长为其余的情况。
 // "TL", "TR", "BL", "BR" };
            switch (position)
            {
                case "TL":
                    {
                        this.Left = 0;
                        this.Top = 0;
                        break;
                    }
                case "TR":
                    {
                        this.Left = SystemParameters.PrimaryScreenWidth - this.Width;
                        this.Top = 0;
                        break;
                    }
                case "BL":
                    {
                        this.Left = 0;
                        this.Top = SystemParameters.WorkArea.Height - Height;
                        break;
                    }
                case "BR":
                    {
                        this.Left = SystemParameters.WorkArea.Width - Width;
                        this.Top = SystemParameters.WorkArea.Height - Height;
                        break;
                    }
            }