我扔一起快速Transparancy Converter類based on this tutorial on Microsoft.com你可以使用這個目的:
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace WpfApplication2
{
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_Loaded事件,並調用MakeTransparent方法:
<Window etc etc Background="Transparent" Loaded="Window_Loaded">
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var transparancyConverter = new TransparancyConverter(this);
transparancyConverter.MakeTransparent();
}
的屏幕截圖是以下:
來源
2013-06-04 21:41:41
JMK
非常感謝我會玩它 –
幾乎工作。看起來像它在窗口7中工作,但不是在窗口8中。任何建議?先謝謝你! –
@StefanVasiljevic不要以爲你會在8上有太多的運氣,上面的代碼使用Aero,它不存在於Windows 8上可悲的是 – JMK