Willy
Willy

Reputation: 10650

WPF Window set custom startup location/position within main screen

I have a custom messagebox done in wpf.

Custom messagebox view 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>

From my main window I show this custom wpf messagebox window when user clicks on a button, as an example of the call from the button when it is clicked:

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

if (messageBoxResult != MessageBoxResult.Yes) return;

*Custom messagebox code-behind 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.

          }
    }
}

By default this custom messagebox is opened at center of main screen.

What I would like to do now is to pass a parameter (enumeration) to my WpfMessageBox.Show method to indicate to my custom message box where I want it to be located within the main screen. Parameters would be these:

How can I do this?

Upvotes: 1

Views: 6101

Answers (2)

Olorunfemi Davis
Olorunfemi Davis

Reputation: 1100

After setting Manual in xaml, you goto c# and set the positions accordingly.

 // "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;
                    }
            }

Upvotes: 0

mm8
mm8

Reputation: 169380

How can I do this?

Set the WindowStartupLocation property of the window to Manual and then set the Left and Top properties of the window to determine its initial position.

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

You need to calculate the values to use, for example using the SystemParameters.PrimaryScreenWidth and SystemParameters.PrimaryScreenHeight properties.

Upvotes: 3

Related Questions