简体   繁体   中英

Keep WinUI 3 (Windows App SDK) window always at bottom

I have the following window using Windows App SDK 1.1.5 and WinUI 3, I want to keep it always at the bottom, just above the desktop, m_AppWindow.MoveInZOrderAtBottom(); moves it to the very bottom as I intended, but once it is pressed it comes back to the foreground.

How can I prevent it from coming to the foreground once clicked? I suppose it may be related to the HWND handle.

using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using WinRT.Interop;

namespace Widgets {
  public sealed partial class MainWindow : Window {

    private AppWindow m_AppWindow;

    public MainWindow() {
      this.InitializeComponent();

      m_AppWindow = GetAppWindowForCurrentWindow();
      m_AppWindow.MoveInZOrderAtBottom();
    }

    private AppWindow GetAppWindowForCurrentWindow() {
      IntPtr hWnd = WindowNative.GetWindowHandle(this);
      WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
      return AppWindow.GetFromWindowId(wndId);
    }
  }
}

This is just a slightly modified MainWindow.cs from the visual studio Blank App, Packaged (WinUI 3 in Desktop) C# template.

Thanks in advance.

You can try it this way.

public sealed partial class MainWindow : Window
{
    private AppWindow m_AppWindow;

    public MainWindow()
    {
        this.InitializeComponent();

        m_AppWindow = GetAppWindowForCurrentWindow();
        m_AppWindow.Changed += M_AppWindow_Changed;
    }

    private void M_AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args)
    {
        if (args.DidZOrderChange is true)
        {
            m_AppWindow.MoveInZOrderAtBottom();
        }
    }

    private AppWindow GetAppWindowForCurrentWindow()
    {
        IntPtr hWnd = WindowNative.GetWindowHandle(this);
        WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
        return AppWindow.GetFromWindowId(wndId);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM