如何在无边框窗体上创建模糊玻璃效果?

5
我如何在无边框窗体上绘制平滑模糊的玻璃效果?我已经尝试了C和GDI+图像处理入门页面上列出的代码,但我确定这不是我应该使用的。无论我怎么摆弄它,都没有产生我想要的任何结果。
基本上,这就是我想要实现的效果: enter image description here

很奇怪,你最初的问题是关于实体框架的,现在却变成了一个带有400美元赏金的GUI界面问题。为什么不把它写成一个新问题呢? - LarsTech
不管其他事情如何,请不要在将来再完全更改问题 - 这会造成一些混乱并且不必要地混淆事情。 - Flexo
如何在无边框窗体上创建模糊玻璃效果。 - Flexo
1
你关心语言吗?我有一个C语言示例。 - γηράσκω δ' αεί πολλά διδασκόμε
@valter 我有点在意语言,但是因为我更熟悉C/C++,所以我可以从C的代码中学习并用C#重写它。如果你有任何C的代码,那将非常有帮助! - jay_t55
显示剩余3条评论
2个回答

2

Winform

使用像DwmEnableBlurBehindWindow这样的Win API

    [DllImport("gdi32")]
    private static extern IntPtr CreateEllipticRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
    [DllImport("dwmapi")]
    private static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DwmBlurbehind pBlurBehind);
    public struct DwmBlurbehind
    {
        public int DwFlags;
        public bool FEnable;
        public IntPtr HRgnBlur;
        public bool FTransitionOnMaximized;
    }
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        var hr = CreateEllipticRgn(0, 0, Width, Height);
        var dbb = new DwmBlurbehind {FEnable = true, DwFlags = 1, HRgnBlur = hr, FTransitionOnMaximized = false};
        DwmEnableBlurBehindWindow(Handle, ref dbb);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, Width, Height));
    }

示例1:

enter image description here

示例2:您可以更改画笔颜色(例如:DarkRed)以获得良好的效果

enter image description here

注意的是,您可以选择适用于的区域,并且可以有多个。

Wpf

您可以使用 相同的方法

Wpf 对着色器有更好的支持,您可以添加像模糊之类的效果,但它们的性能较重。此外,您的图像很可能来自由 WPF 和 Blend 创建的程序。

如果我记得正确,您无法在纯色画刷上使用模糊等着色器。 因此,以下不会产生相同的效果。设置背景 50% 透明度(Window 属性 AllowsTransparency="True"WindowStyle="None"

<Window.Background>
    <SolidColorBrush Opacity="0.5" Color="White"/>
</Window.Background>

本地玻璃窗户

WPF支持已在此处描述:http://www.paulrohde.com/native-glass-with-wpf/

我使用稍微不同的方法得到了以下结果: enter image description here


如何创建类似的元素:http://blogs.msdn.com/b/wpfsdk/archive/2008/09/08/custom-window-chrome-in-wpf.aspx - Margus

1
我假设您是在谈论Windows 7 / Vista,并且希望以同样的方式实现一些MS程序中具有模糊透明区域的效果。对于一般情况,您需要进行一些图像处理,我不会涉及这方面的内容。
对于我上面提到的情况,您不应该自己实现 - 这有点重新发明轮子。基本上,您可以使用窗口管理器通过使用本文所述方法(称为aero glass)来实现此效果:http://msdn.microsoft.com/en-us/magazine/cc163435.aspx 我目前只有一台Windows 8机器(其中默认取消了这种模糊和透明度),因此我没有测试环境来检查这一点。我将在本周晚些时候获得一个并设置示例代码来执行此操作,
一旦您使用桌面窗口管理器,如果您只想模糊顶部部分(如您的图像中所示),请使用DwmExtendFrameIntoClientArea将框架(默认情况下为aero模糊)扩展到您的窗口中。对于自定义区域,请使用DwmEnableBlurBehindWindow。
所以,如果这确实是你要找的内容(Windows 7 / Vista的解决方案与现有的MS程序相同),请告诉我,我稍后会更新代码。否则,如果你正在寻找一般解决方案(不仅限于Windows Vista / 7),请告诉我以节省编码的工作量...
编辑:鉴于您选择手动制作效果,这里提供一些基本代码供您开始使用。
// Code was burrowed from:
//   https://dev59.com/jGIj5IYBdhLWcg3wzIFo
//   http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/CommonUI/System/Drawing/NativeMethods@cs/1305376/NativeMethods@cs
//   https://dev59.com/5mw05IYBdhLWcg3wXAiF
//   https://dev59.com/aEfRa4cB1Zd3GeqP8mAa */

public static class WinAPI
{
   public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    };

    public enum GW
    {
        GW_HWNDFIRST = 0,
        GW_HWNDLAST = 1,
        GW_HWNDNEXT = 2,
        GW_HWNDPREV = 3,
        GW_OWNER = 4,
        GW_CHILD = 5,
        GW_ENABLEDPOPUP = 6
    }

    [DllImport("User32.dll")]
    public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

    [DllImport("User32.dll", CharSet = CharSet.Unicode)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

    [DllImport("User32.dll")]
    public static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern bool IsIconic(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern bool GetClientRect(IntPtr hWnd, ref RECT lpRect);

    [DllImport("User32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

    [DllImport("User32.dll")]
    public static extern IntPtr GetDesktopWindow();

    [DllImport("User32.dll")]
    public static extern IntPtr GetTopWindow(IntPtr hWnd);

    public static IntPtr GetNextWindow(IntPtr hWnd, GW wCmd)
    {
        return GetWindow(hWnd, wCmd);
    }

    public static IntPtr GetWindow(IntPtr hWnd, GW wCmd)
    {
        return GetWindow(hWnd, (uint)wCmd);
    }

    [DllImport("User32.dll")]
    private static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);

    [DllImport("User32.dll")]
    public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, uint nFlags);

    [DllImport("User32.dll")]
    public static extern IntPtr GetDC(IntPtr hWnd);

    [DllImport("Gdi32.dll")]
    public static extern bool DeleteDC(IntPtr hdc);

    [DllImport("User32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);

    [DllImport("User32.dll")]
    public static extern int ReleaseDC(IntPtr hWnd, IntPtr hdc);

    [DllImport("User32.dll")]
    public static extern int GetWindowRgn(IntPtr hWnd, IntPtr hRgn);

    [DllImport("Gdi32.dll")]
    public static extern IntPtr CreateCompatibleDC(IntPtr hWnd);

    [DllImport("Gdi32.dll")]
    public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int width, int height);

    [DllImport("Gdi32.dll")]
    public static extern IntPtr CreateBitmap(int width, int height, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits);

    [DllImport("Gdi32.dll")]
    public static extern bool DeleteObject(IntPtr hGdiObj);

    [DllImport("Gdi32.dll")]
    public static extern bool SelectObject(IntPtr hdc, IntPtr hGdiObj);

    [DllImport("Gdi32.dll")]
    public static extern IntPtr CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);

    public static StringBuilder GetWindowText(IntPtr hWnd)
    {
        int length = GetWindowTextLength(hWnd);
        // Add another place to allow null terminator
        StringBuilder text = new StringBuilder(length + 1);
        GetWindowText(hWnd, text, length + 1);
        return text;    
    }

    public static bool IsWindowReallyVisible(IntPtr hWnd)
    {
        if (!IsWindowVisible(hWnd) || IsIconic(hWnd))
            return false;
        RECT area = new RECT();
        if (!GetWindowRect(hWnd, ref area))
            return true;
        if (area.left == area.right || area.bottom == area.top)
            return false;
        return true;
    }

    public enum RegionFlags
    {
        ERROR = 0,
        NULLREGION = 1,
        SIMPLEREGION = 2,
        COMPLEXREGION = 3,
    } 

}

public class ScreenShot
{
    public static List<IntPtr> GetAllWindows(Func<IntPtr, bool> filter, Func<IntPtr, bool> stop)
    {
        List<IntPtr> result = new List<IntPtr>();

        WinAPI.EnumWindows((wnd, param) =>
        {
            bool relevant = filter(wnd);
            if (relevant)
                result.Add(wnd);
            bool shouldStop = stop(wnd);
            if (shouldStop)
                Console.WriteLine("Stop");
            return !shouldStop;
        }, IntPtr.Zero);

        return result;
    }

    public static IEnumerable<IntPtr> GetWindowsByOrder(Func<IntPtr, bool> filter)
    {
        List<IntPtr> skip = new List<IntPtr>();
        List<IntPtr> result = new List<IntPtr>();
        IntPtr desktop = WinAPI.GetDesktopWindow();

        for (IntPtr wnd = WinAPI.GetTopWindow(IntPtr.Zero); wnd != IntPtr.Zero; wnd = WinAPI.GetNextWindow(wnd, WinAPI.GW.GW_HWNDNEXT))
        {
            if (result.Contains(wnd) || skip.Contains(wnd))
                break;
            else if (filter(wnd))
                result.Add(wnd);
            else
                skip.Add(wnd);
        }

        result.Add(desktop);
        return result;
    }

    public static Image GetScreenshot(IntPtr hWnd)
    {
        IntPtr hdcScreen = WinAPI.GetDC(hWnd);
        IntPtr hdc = WinAPI.CreateCompatibleDC(hdcScreen);
        WinAPI.RECT area = new WinAPI.RECT();

        WinAPI.GetWindowRect(hWnd, ref area);

        IntPtr hBitmap = WinAPI.CreateCompatibleBitmap(hdcScreen, area.right - area.left, area.bottom - area.top);
        WinAPI.SelectObject(hdc, hBitmap);

        WinAPI.PrintWindow(hWnd, hdc, 0);

        Image resultOpaque = Image.FromHbitmap(hBitmap);

        WinAPI.DeleteObject(hBitmap);
        WinAPI.DeleteDC(hdc);

        IntPtr hRegion = WinAPI.CreateRectRgn(0, 0, 0, 0);
        WinAPI.RegionFlags f = (WinAPI.RegionFlags) WinAPI.GetWindowRgn(hWnd, hRegion);
        Region region = Region.FromHrgn(hRegion);
        WinAPI.DeleteObject(hRegion);

        Bitmap result = new Bitmap(resultOpaque.Width, resultOpaque.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(result);
        g.Clear(Color.Transparent);
        if (f != WinAPI.RegionFlags.ERROR)
            g.SetClip(region, System.Drawing.Drawing2D.CombineMode.Replace);
        if (f != WinAPI.RegionFlags.NULLREGION)
            g.DrawImageUnscaled(resultOpaque, 0, 0);

        g.Dispose();
        region.Dispose();
        resultOpaque.Dispose();

        return result;
    }

/* And now for the actual code of getting screenshots of windows */
var windows = ScreenShot.GetWindowsByOrder(this.WindowFilter).Intersect(ScreenShot.GetAllWindows(this.WindowFilter, (wnd) => false)).ToList();

int index = windows.IndexOf((IntPtr)this.Handle); /* Remove all the windows behind your windows */
if (index != -1)
    windows.RemoveRange(index, windows.Count - index + 1);

windows.Reverse();

/* Get the images of all the windows */
for (int i = 0; i < windows.Count; ++i )
{
    var window = windows[i];
    using (var img = ScreenShot.GetScreenshot(window))
    {
        // Get the actual position of the window
        // Draw it's image overlayed on the other windows 9have an accumulating image)
    }
}

这个测试套件在Windows 8.1上还存在哪些漏洞?

  • 我仍然遇到透明度问题 - 在其裁剪区域内半透明的窗口仍会被绘制为黑色
  • 我有一些未知的黑色窗口(可能是透明的)仍然会出现
  • 任务栏无法正确绘制

谢谢@Lightning,我很感激你的努力。我不想卖弄学问,但那真的不是我要找的。我不想要“Aero”…我想要我的自定义玻璃效果。在Windows 8和8.1中,您无法获得Aero玻璃效果,它仍然是不透明的,这就是我提出此问题的原因。我想要一个自定义绘制的玻璃效果。 - jay_t55
1
@baeltazor:好的,我就有点怀疑400多的赏金不会那么容易。在这种情况下,你基本上需要收集你自己窗口下所有窗口的屏幕截图,并将它们组合成位图。完成后,模糊位图并将其作为您自己窗口的背景。这确实更难 - 我会尝试一下,但我怀疑我是否足够了解如何做到这一点。 - Barak Itkin
非常感谢@Lightning,我正在考虑直接截取窗口下方桌面区域的“屏幕截图”,但我不确定它是否只会抓取壁纸,还是也会包括其他所有内容(如您所说的其他窗口等)。 - jay_t55
1
@baeltazor:我已经添加了所有的代码,供您进行实验。我已经将截图概念的70%实现了。希望这能帮到您 ;) - Barak Itkin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接