Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.2K
Visualizações
How to Maximize, Minimize, Restore WinUI window from code

I'm creating a demo app using WinUI 3 prerelease 0.5 (project reunion).

I would like to maximize, minimize, and restore the window state from c# managed code. Are there any examples available?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Apparently this is not possible using managed code at this time. PInvoke is the only option currenlty. I used PInvoke.User32 nuget package.

over 4 years ago · Santiago Trujillo Relatório

0

Use AppWindow class from Windows App SDK

// Retrieve the window handle (HWND) of the current (XAML) WinUI 3 window.
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

// Retrieve the WindowId that corresponds to hWnd.
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);

// Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.Closing += AppWindow_Closing;
private void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args)
{
    args.Cancel = true;
}

https://docs.microsoft.com/en-us/windows/apps/windows-app-sdk/windowing/windowing-overview

over 4 years ago · Santiago Trujillo Relatório

0

Here is my implementation based on the PInvoke.User32 nuget package.

Version using Window parameter

    public void MaximizeWindow(Window window)
    {
        var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);

        PInvoke.User32.ShowWindow(windowHandle, PInvoke.User32.WindowShowStyle.SW_MAXIMIZE);
    }

Version using extension method

This previous code can also be transformed into an extension method for the Window class, so I can just call this.Maximize() from within the Window:

public static class WindowExtensions
{
    public static void Maximize(this Window window)
    {
        var windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(window);

        PInvoke.User32.ShowWindow(windowHandle, PInvoke.User32.WindowShowStyle.SW_MAXIMIZE);
    }
}

Usage in your Window derived class:

this.Maximize();


Minize and Restore

The "PInvoke.User32.WindowShowStyle" definition used in the examples above contains also constants for SW_MINIMIZE and SW_RESTORE, but I did not test this.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda