簡體   English   中英

從 Alt+Tab 隱藏 WPF 表單

[英]Hide a WPF form from Alt+Tab

我想從任務菜單(Alt+Tab)中隱藏一個具有WindowStyle="None"AllowTransparency="True"ShowInTaskbar="False"的 WPF 窗口。

我已經對此進行了研究,但所有結果似乎都是針對 WinForms 的,或者沒有答案。 以下是我已經研究過的一些來源:

  1. VS 社區上的相同問題沒有答案
  2. StackOverflow 上的相同問題,但針對 WinForms
  3. 同樣的問題,但對於通用站點上的 WinForms
  4. 這不符合我的要求,因為我仍然希望 WPF 窗口可見,只是在 Alt+Tab 菜單中看不到

這是我的 XAML 代碼:

<Window x:Class="DesktopInfo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DesktopInfo"
        mc:Ignorable="d"
        Title="MainWindow" SizeToContent="WidthAndHeight" WindowStyle="None" AllowsTransparency="True"  Background="Transparent" ShowInTaskbar="False" Loaded="FormLoaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Testing" Name="UsernameTextBlock" FontSize="20" FontWeight="Bold" Foreground="White"/>
        <TextBlock Name="ComputernameTextBlock" Grid.Row="1" FontSize="20" FontWeight="Bold" Foreground="White"/>
    </Grid>
</Window>

這是我的 C# 代碼:

using System;
using System.Windows;
using System.Windows.Forms;

namespace DesktopInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

無論我嘗試什么,我都無法讓 WPF 表單不顯示在 Alt+Tab 菜單中。 很感謝任何形式的幫助 :)

DUPLICATE FLAG更新在查看了提供的鏈接(以及在問這個問題之前之前查看過的)之后,我想聲明,事實上,我在這里找到了答案,並將發布我的完整代碼作為這個問題的答案: )

我在這個StackOverflow 問題的答案之后的最終代碼可以在下面看到:

using System;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace DesktopInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        private const int GWL_EX_STYLE = -20;
        private const int WS_EX_APPWINDOW = 0x00040000, WS_EX_TOOLWINDOW = 0x00000080;

        public MainWindow() => InitializeComponent();
        
        //Form loaded event handler
        void FormLoaded(object sender, RoutedEventArgs args)
        {
            //Variable to hold the handle for the form
            var helper = new WindowInteropHelper(this).Handle;
            //Performing some magic to hide the form from Alt+Tab
            SetWindowLong(helper, GWL_EX_STYLE, (GetWindowLong(helper, GWL_EX_STYLE) | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW);
            
        }
    }
}

我的表單現在作為后台任務運行,仍然可見,無法在 Alt+Tab 菜單中看到。 謝謝大家的幫助:) 我有點慚愧,我在發布問題之前沒有找到獲勝的鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM