I have a project that I am working on where a popup window will appear asking for some parameters and then it will kick off a process which may take a while to return. The information on creating a new window, and centering it isn’t readily available, so I thought I’d do a posting on some of the testing code that I have written which will end up added to the project.

So, first off, I want to spawn a window that is in the Smart Office Style – this needs to be a modaldialog box – the user should be bound and focused only on MY script 😉

Then I want to add a standard status bar so we can provide updates on the progress of our long running process. I wanted the Close/Previous and Next buttons and I wanted to center the pop up on the existing window it was launched from.

You’d think that none of these things would be terribly difficult 🙂

Retrieving the Parent Window Position

Access to the window comes via the Host property on the controller object that is passed to the Init() method. It doesn’t give us a Window object, nor does it given us the EmbeddedHostWindow object, however it does give us access to a number of functions which will allow us to get what we need.

We can retrieve our Windows Width and Height directly from controller.Host.Width and controller.Host.Height

However getting the actual left and top positions of the window proved to be more challenging. I ended up using a tool called WPF Inspector and after some investigation I discovered that the position is determined by the Margins, not the Left and Top properties

But this presented another issue. None of the Host properties provide a direct path to the EmbeddedHostWindow so we can then retrieve the Margins.

It turned out that the controller.Host.VisualElement.Name property was “PART_Window” that we can see in the screenshot above – this meant that the VisualElement was a child of the EmbeddedHostWindow

So, we can use the VisualTreeHelper.GetParent() method to go up a level. As we are probably entering in to a world that is subject to change, in my code I actually run a loop iterating up the tree until I find an EmbeddedHostWindow.

Now that we have the EmbeddedHostWindow we can extract the margin values and save those for later analysis.

If you have a better way to achieve the same results, feel free to use the comments section!

Setting Our Child Windows Position

So we use the controller.Host.CreateDialog() function to create our new window, and we assign values to the .Left and .Top properties – however when we do a .ShowDialog() the values are totally ignored. <sigh>…

We can change this by subscribing to the Window.Loaded() event, and then set our .Left and .Top values there.

StatusBar and the Standard Close, Previous, Next buttons

These ones I’ve already done most of the leg work on in my SDK projects. The StatusBar is under the Mango.UI.Controls namespace and the standard Close, Previous and Next buttons are from the Mango.DesignSystem namespace.

The code will give you an idea of how to use them. I did have to hard-code the height of the Previous – I couldn’t find anything that would define the standard width when it didn’t have an icon so I had a look at the width of one of the existing previous buttons.