WPF 中透明窗口的实现失败
本文关键字:实现 失败 透明窗口 WPF | 更新日期: 2023-09-27 17:56:41
我在这里找到了带有边框的 WPF 透明窗口的实现 C# 带有边框的 WPF 透明窗口。我尝试实现它,但我的窗口不透明。没有错误出来。下面是 xaml 和代码隐藏。
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Extended Glass in WPF" Height="300" Width="400"
Background="Transparent" Loaded="Window_Loaded">
<Grid ShowGridLines="True" Background="Transparent">
</Grid>
</Window>
代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Drawing;
namespace WpfApplication9
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var transparancyConverter = new TransparancyConverter(this);
transparancyConverter.MakeTransparent();
}
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth; // width of left border that retains its size
public int cxRightWidth; // width of right border that retains its size
public int cyTopHeight; // height of top border that retains its size
public int cyBottomHeight; // height of bottom border that retains its size
};
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref MARGINS pMarInset);
}
class TransparancyConverter
{
private readonly Window _window;
public TransparancyConverter(Window window)
{
_window = window;
}
public void MakeTransparent()
{
var mainWindowPtr = new WindowInteropHelper(_window).Handle;
var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
if (mainWindowSrc != null)
if (mainWindowSrc.CompositionTarget != null)
mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);
var margins = new Margins
{
cxLeftWidth = 0,
cxRightWidth = Convert.ToInt32(_window.Width) * Convert.ToInt32(_window.Width),
cyTopHeight = 0,
cyBottomHeight = Convert.ToInt32(_window.Height) * Convert.ToInt32(_window.Height)
};
if (mainWindowSrc != null) DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
}
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins pMarInset);
}
}
此外,我尝试在不保留边框的情况下在代码后面设置基本透明度。人们还将在下面找到我的 xaml 和代码。创建的窗口不是透明的,只有白色背景。在这两个例子中,我做错了什么?我觉得有一个共同点。
<Window x:Class="WpfApplication10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="MainWindowGUI"
>
<Grid>
</Grid>
</Window>
代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication10
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainView_Loaded;
}
void MainView_Loaded(object sender, RoutedEventArgs e)
{
Window yourParentWindow = Window.GetWindow(MainWindowGUI);
yourParentWindow.WindowStyle = WindowStyle.None;
yourParentWindow.AllowsTransparency = true;
SolidColorBrush Transparent = new SolidColorBrush(System.Windows.Media.Colors.Transparent);
yourParentWindow.Background = Transparent;
}
}
}
你需要设置
AllowsTransparency="True"
; 要使其正常工作,您需要设置WindowStyle="None"
。 WindowStyle="None"
会丢失您的非工作区(边框、标题栏、最小/最大/关闭按钮),但这很容易解决。
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Extended Glass in WPF" Height="300" Width="400"
Background="Transparent"
AllowsTransparency="True"
WindowStyle="None"
>
<Grid ShowGridLines="True" Background="Transparent">
</Grid>
</Window>