我想创建一个透明的简单应用程序,但保留“普通”边框,关闭按钮,最小化和最大化按钮。
我知道如何使用标准
使窗口透明<Window
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent">
</Window>
但这会删除边框和右上角的按钮。我读了这个帖子,
Transparent window with a border
哪种解决方案,但实际上,我只是希望能够保持标准边框,如果我没有使窗口透明。我可以移动窗口,调整大小,关闭等等...这可能吗?
答案 0 :(得分:6)
我将一个快速的TransparencyConverter
课程based on this tutorial on 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);
}
}
进入后,将Transparent Background属性添加到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();
}
屏幕截图如下:
答案 1 :(得分:1)
我首先看一下背景颜色的rgb(a)颜色中的(a)lpha设置。 alpha设置设置对象颜色的不透明度。
虽然,我注意到,正如我发布的那样,在我之前还有另一篇文章看起来更简洁,可能更适合你。