1

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:

  • Top Left
  • Top Center
  • Top Right
  • Middle Left
  • Middle Center
  • Middle Right
  • Botton Left
  • Bottom Center
  • Bottom Right

How can I do this?

Willy
  • 9,848
  • 22
  • 141
  • 284
  • You will have to investigate [why](https://msdn.microsoft.com/en-us/library/system.windows.windowstartuplocation(v=vs.110).aspx) is it *"opened at center of main screen"* first. After that it become a bit more complicated, but [doable](https://stackoverflow.com/q/1927540/1997232). – Sinatr Nov 10 '17 at 13:49
  • Set the Left and Top properties of the window before you open it. – mm8 Nov 10 '17 at 14:00

2 Answers2

3

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I pass as an argument the parent Window (Window) and also the enumeration TopLeft, TopMiddle, etc. to the 'Show' method in WpfMessageBox. Once in 'Show' method I play with Top and Left properties of child window (WpfMessageBox). But i have problems to set the rest of locations, for exemple for TopRight I set: WpfMessageBox.Top = parent.Top;WpfMessageBox.Left = (parent.Left + parent.Width) - WpfMessageBox.Width; but in this case it is not working. – Willy Nov 10 '17 at 14:48
  • Also could you help me how can I set the rest of locations TopCenter, TopRight, MiddleLeft, MiddleCenter(WindowStartupLocation.CenterScreen), MiddleRight, BottomLeft, BottomCenter and BottomRight please? – Willy Nov 10 '17 at 14:48
  • SO is not for anyone else to write code for you...You asked on how to position the main window and I have answered that question. – mm8 Nov 10 '17 at 14:51
  • Yes, you have answered but partially. showing how to set top-left only. I need to set the rest. Anyway I accept your answer an open a new one because I am having problems to set child window (messagebox) within the main window parent for the rest of cases. – Willy Nov 10 '17 at 14:57
0

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;
                    }
            }
Olorunfemi Davis
  • 1,001
  • 10
  • 23