C#WPF带有边框的透明窗 [英] C# WPF transparent window with a border

查看:399
本文介绍了C#WPF带有边框的透明窗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作一个简单的应用程序,它是透明的,但保留了正常的边界,关闭按钮,最小化和最大化按钮。

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,

透明窗口带边框

哪种类型的解决方案提供了,但说真的,我只是希望能够保持标准的边界,如果我没有,这将是有ŧ使窗口透明。该方法我可以移动窗口,调整,关闭,等...这可能吗?

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?

推荐答案

我扔一起快速基于此教程Microsoft.com上您可以使用此目的Transparancy Converter类

I threw together a quick Transparancy Converter class based on this tutorial on Microsoft.com you can use for this purpose:

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方式:

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:

<Window etc etc Background="Transparent" Loaded="Window_Loaded">

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var transparancyConverter = new TransparancyConverter(this);
    transparancyConverter.MakeTransparent();
}

一个截图如下:

这篇关于C#WPF带有边框的透明窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆