WPF Window 在主屏幕中设置自定义启动 location/position

WPF Window set custom startup location/position within main screen

我有一个用 wpf 完成的自定义消息框。

自定义消息框视图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>

来自我的主要 window 我在用户单击按钮时显示此自定义 wpf 消息框 window,作为单击按钮时从按钮调用的示例:

var messageBoxResult = WpfMessageBox.Show("Title", "MyMessage",
    MessageBoxButton.YesNo, WpfMessageBox.MessageBoxImage.Warning);

if (messageBoxResult != MessageBoxResult.Yes) return;

*自定义消息框代码隐藏 xaml.cs:

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 方法,以向我的自定义消息框指示我希望它在主屏幕中的位置。参数将是这些:

我该怎么做?

How can I do this?

设置window的WindowStartupLocation属性为Manual然后设置LeftTop的属性22=]确定其初始位置。

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

您需要计算要使用的值,例如使用 SystemParameters.PrimaryScreenWidthSystemParameters.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;
                    }
            }