2

I am develop a new app for Windows 11 with WinUi 3 and I want when I open the app always open in the center of my screen/display. It is possible?

I am using PInvoke.User32 for set window size (if it helps).

Thank you!

Luís
  • 157
  • 2
  • 10
  • 2
    What's the question? How to calculate the position of the screen center? How to move the window to that position? What you're asking is unclear. – Ken White Mar 20 '22 at 13:41
  • 1
    I want the application to open always centered (horizontally and vertically) on my monitor. – Luís Mar 20 '22 at 15:05
  • 1
    Yes, you've said what you want to happen twice now. You didn't answer either of the questions I asked? Repeating what your end goal is repeatedly is not a question. **What is your specific question?** – Ken White Mar 20 '22 at 15:44
  • How can I open code to open my app always centered on the screen? – Luís Mar 20 '22 at 15:50
  • 1
    And again, you've ignored the two questions I asked you. And here's a third: By "center of the screen", do you mean the screen, which can extend over multiple monitors, or just the monitor your app is on? II've voted to close your question because you've not provided specific information when asked. – Ken White Mar 20 '22 at 15:52
  • Only in the monitor where app is on. I want to move the window to that position, center of the screen. – Luís Mar 20 '22 at 16:05
  • 1
    So that's at least 4 separate questions. 1) How to find out which monitor your app is on; 2) How to find the dimensions (width and height) of that monitor; 3) How to calculate where your window should be located on that monitor; and 4) How to move your window to that position. Start with the first one, search this site, and see if you can get that part working. Then pick the next one, search, and see if you can get that to work. Repeat with the next two tasks. – Ken White Mar 20 '22 at 17:56

2 Answers2

12

The following example code is simplistic but achieves what you are asking for.

Create a Windows App SDK project using the blank app template.

In the App.xaml.cs file, change the OnLaunched() method to the following:

m_window = new MainWindow();
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
if (appWindow is not null)
{
    Microsoft.UI.Windowing.DisplayArea displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(windowId, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);
    if (displayArea is not null)
    {
        var CenteredPosition = appWindow.Position;
        CenteredPosition.X = ((displayArea.WorkArea.Width - appWindow.Size.Width) / 2);
        CenteredPosition.Y = ((displayArea.WorkArea.Height - appWindow.Size.Height) / 2);
        appWindow.Move(CenteredPosition);
    }
}
m_window.Activate();

When you run the app, its main window will be centered in the display.

EddieLotter
  • 324
  • 3
  • 8
1

Works Great!!! i just copy paste your funcion under a general file and call it from every screen

public MYWindowClass
{
  this.InitializeComponent();
  var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
  FuncionesGenerales.CenterToScreen(hWnd);
}

Then in my general file

FuncionesGenerales.CenterToScreen(IntPtr hWnd)
{
 Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
        Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
        if (appWindow is not null)
        {
            Microsoft.UI.Windowing.DisplayArea displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(windowId, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);
            if (displayArea is not null)
            {
                var CenteredPosition = appWindow.Position;
                CenteredPosition.X = ((displayArea.WorkArea.Width - appWindow.Size.Width) / 2);
                CenteredPosition.Y = ((displayArea.WorkArea.Height - appWindow.Size.Height) / 2);
                appWindow.Move(CenteredPosition);
            }
        }
}

Thank You