XAML 使WinUI 3(Windows应用程序SDK)窗口始终位于底部

oxcyiej7  于 2022-12-07  发布在  Windows
关注(0)|答案(1)|浏览(124)

我有以下窗口使用Windows应用程序SDK 1.1.5和WinUI 3,我想保持它总是在底部,就在桌面上方,m_AppWindow.MoveInZOrderAtBottom();移动到非常底部,因为我打算,但一旦按下它回到前台.
我怎样才能防止它在被点击后出现在前台呢?我想这可能和HWND句柄有关。

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);
    }
  }
}

这只是对visual studio Blank App, Packaged (WinUI 3 in Desktop) C#模板进行了轻微修改的MainWindow.cs
先谢谢你。

ckx4rj1h

ckx4rj1h1#

你可以这样试试。

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);
    }
}

相关问题