【问题标题】:Hide a WPF form from Alt+Tab从 Alt+Tab 隐藏 WPF 表单
【发布时间】:2019-06-18 08:53:05
【问题描述】:

我想从任务菜单 (Alt+Tab) 中隐藏一个包含 WindowStyle="None"AllowTransparency="True"ShowInTaskbar="False" 的 WPF 窗口。

我已经对此进行了研究,但所有结果似乎都是针对 WinForms 的,或者没有答案。以下是我已经研究过的一些来源:

  1. Same question on VS community WITHOUT an answer
  2. Same question on StackOverflow but for WinForms
  3. Same question but for WinForms on generic site
  4. This doesn't meet my requirements because I still want the WPF window to be visible, just not seen in the Alt+Tab menu

这是我的 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 菜单中。非常感谢任何帮助:)

重复标志后更新 在查看了提供的链接(以及在提出这个问题之前查看过)之后,我想声明我实际上找到了answer here,并将发布我的完整代码作为这个问题的答案:)

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:

我在StackOverflow question 的回答之后的最终代码如下所示:

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 菜单中看不到。谢谢大家的帮助:)我有点惭愧,在发布问题之前我没有找到获奖链接。

【讨论】:

  • 你是圣人!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2022-01-21
  • 2010-12-28
相关资源
最近更新 更多