C# WPF 透明窗口带边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16927699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WPF transparent window with a border
提问by Muckle_ewe
I would like to make a simple application which is transparent but retains the 'normal' borders, close button, minimize and maximize button.
我想做一个简单的应用程序,它是透明的,但保留了“正常”边框、关闭按钮、最小化和最大化按钮。
I know how to make the window transparent using the standard
我知道如何使用标准使窗口透明
<Window
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent">
</Window>
but this removes the borders and top right buttons. I read this thread,
但这会删除边框和右上角的按钮。我读了这个线程,
Transparent window with a border
which sort of gives solution, but really, I just want to be able to keep the standard borders that would be there if I didn't make the window transparent. The means I can move the window, resize, close, etc... Is this possible?
哪种提供了解决方案,但实际上,如果我不使窗口透明,我只是希望能够保留标准边框。我可以移动窗口、调整大小、关闭等的方式......这可能吗?
采纳答案by JMK
I threw together a quick TransparencyConverterclass based on this tutorial on Microsoft.comyou can use for this purpose:
我扔一起快速TransparencyConverter类基于本教程Microsoft.com上,你可以使用这个目的:
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace WpfApplication2
{
class TransparencyConverter
{
private readonly Window _window;
public TransparencyConverter(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);
}
}
Once you have this in, add the Transparent Background attribute to your XAML and subscribe to the Window_Loaded event and call the MakeTransparent method:
完成后,将透明背景属性添加到 XAML 并订阅 Window_Loaded 事件并调用 MakeTransparent 方法:
<Window etc etc Background="Transparent" Loaded="Window_Loaded">
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var transparencyConverter = new TransparencyConverter(this);
transparencyConverter.MakeTransparent();
}
A screenshot is below:
屏幕截图如下:


回答by nocturns2
I would first look at the (a)lpha setting in the rgb(a) color of the background color. The alpha setting sets the opacity of the object color.
我将首先查看背景颜色的 rgb(a) 颜色中的 (a)lpha 设置。alpha 设置设置对象颜色的不透明度。
Although, I notice that as I'm posting this, there is another post before mine that looks more concise and would probably be more appropriate for you.
虽然,我注意到在我发布这篇文章时,在我之前还有另一篇文章看起来更简洁,可能更适合你。

![如何将二维数组 bool[][] 绑定到 WPF DataGrid(单向)?](/res/img/loading.gif)