Glass-прозрачность в WPF
Как сделать Glass-прозрачность в WPF-приложении?
1) Разместите следующий код в файле класса окна к которому вы хотите добавить glass:
[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);
2)свойство окна Background сделайте Transparent и добавьте следующий код к событию Loaded:
try { // Obtain the window handle for WPF application IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle; HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0); // Get System Dpi System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr); float DesktopDpiX = desktop.DpiX; float DesktopDpiY = desktop.DpiY; // Set Margins NonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS(); // Extend glass frame into client area // Note that the default desktop Dpi is 96dpi. The margins are // adjusted for the system Dpi. margins.cxLeftWidth = Convert.ToInt32(5 * (DesktopDpiX / 96)); margins.cxRightWidth = Convert.ToInt32(5 * (DesktopDpiX / 96)); margins.cyTopHeight = Convert.ToInt32(((int)topBar.ActualHeight + 5) * (DesktopDpiX / 96)); margins.cyBottomHeight = Convert.ToInt32(5 * (DesktopDpiX / 96)); int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); // if (hr < 0) { //DwmExtendFrameIntoClientArea Failed } } // If not Vista, paint background white. catch (DllNotFoundException) { Application.Current.MainWindow.Background = Brushes.White; }
3) вот и всё.