如何在 Winform c# 中创建 windows 10 亚克力透明效果 [英] How to create windows 10 acrylic transparency effect in Winform c#

查看:58
本文介绍了如何在 Winform c# 中创建 windows 10 亚克力透明效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个支持亚克力材料设计的表单.正如你在微软的几乎每个应用程序中都看到的那样,我想创建一个透明效果.我想这样创建.这不是不可能做到的,因为 DevExpress 已经做到了(流利形式)

所以任何人都有在 Winform 中制作 windows 10 透明效果的代码

解决方案

我觉得在 Windows 窗体中获得亚克力效果本身并不是特别困难(请参阅 .

使用系统;使用 System.Drawing;使用 System.Windows.Forms;公共部分类 Form1 :表单{公共 Form1(){初始化组件();}protected override void OnHandleCreated(EventArgs e){//使用例如Color.FromArgb(128, Color.Lime) 用于 50% 不透明度绿色色调.WindowUtils.EnableAcrylic(this, Color.Transparent);base.OnHandleCreated(e);}protected override void OnPaintBackground(PaintEventArgs e){e.Graphics.Clear(颜色.透明);}}

WindowUtils.cs:

使用系统;使用 System.Drawing;使用 System.Runtime.InteropServices;使用 System.Windows.Forms;公共静态类 WindowUtils{public static void EnableAcrylic(IWin32Window window, Color blurColor){if (window is null) throw new ArgumentNullException(nameof(window));var AccentPolicy = 新的 AccentPolicy{AccentState = ACCENT.ENABLE_ACRYLICBLURBEHIND,GradientColor = ToAbgr(blurColor)};不安全{设置窗口组合属性(新的 HandleRef(window, window.Handle),新的 WindowCompositionAttributeData{属性 = WCA.ACCENT_POLICY,数据 = &accentPolicy,DataLength = Marshal.SizeOf()});}}私有静态 uint ToAbgr(颜色颜色){返回 ((uint)color.A <<24)|((uint)color.B<<16)|((uint)color.G<<8)|颜色.R;}//ReSharper 禁用 InconsistentNaming、UnusedMember.Local、NotAccessedField.Local#pragma 警告禁用 649//通过以下方式发现://https://withinrafael.com/2015/07/08/adding-the-aero-glass-blur-to-your-windows-10-apps///https://github.com/riverar/sample-win32-acrylicblur/blob/917adc277c7258307799327d12262ebd47fd0308/MainWindow.xaml.cs[DllImport("user32.dll")]私有静态外部 int SetWindowCompositionAttribute(HandleRef hWnd,在 WindowCompositionAttributeData 数据中);私有不安全结构 WindowCompositionAttributeData{公共 WCA 属性;公共无效*数据;公共整数数据长度;}私人枚举 WCA{ACCENT_POLICY = 19}私人枚举 ACCENT{禁用 = 0,ENABLE_GRADIENT = 1,ENABLE_TRANSPARENTGRADIENT = 2,ENABLE_BLURBEHIND = 3,ENABLE_ACRYLICBLURBEHIND = 4,无效状态 = 5}私有结构 AccentPolicy{公共 ACCENT 口音状态;公共单位重音标志;公共 uint GradientColor;公共 uint 动画 ID;}}

I want to create a form with support of acrylic material design.as you can see in the almost every apps of Microsoft has a transparency effect.I want to create like that.it's not impossible to do because DevExpress is already did (Fluent Form)

So anyone have the code for make windows 10 transparency effect in Winform

解决方案

I didn't find it particularly hard to get the acrylic effect itself in Windows Forms (see https://withinrafael.com/2015/07/08/adding-the-aero-glass-blur-to-your-windows-10-apps/), but the really hard part would be getting Win32 itself to work well with it:

Resizing the form lags the mouse cursor itself really badly, too. I'd be interested to take a look at what DevExpress does, but it looks like there's a lot more to do than to just enable acrylic. You most likely will have to custom-draw anything that goes on top of it, similar to how Aero glass was but worse because how you'll have to custom-draw the title bar and borders now, too.

Sample code

Warning! These are undocumented APIs. See Raymond Chen's When programs grovel into undocumented structures… before proceeding.

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        // Use e.g. Color.FromArgb(128, Color.Lime) for a 50% opacity green tint.
        WindowUtils.EnableAcrylic(this, Color.Transparent);

        base.OnHandleCreated(e);
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        e.Graphics.Clear(Color.Transparent);
    }
}

WindowUtils.cs:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public static class WindowUtils
{
    public static void EnableAcrylic(IWin32Window window, Color blurColor)
    {
        if (window is null) throw new ArgumentNullException(nameof(window));

        var accentPolicy = new AccentPolicy
        {
            AccentState = ACCENT.ENABLE_ACRYLICBLURBEHIND,
            GradientColor = ToAbgr(blurColor)
        };

        unsafe
        {
            SetWindowCompositionAttribute(
                new HandleRef(window, window.Handle),
                new WindowCompositionAttributeData
                {
                    Attribute = WCA.ACCENT_POLICY,
                    Data = &accentPolicy,
                    DataLength = Marshal.SizeOf<AccentPolicy>()
                });
        }
    }

    private static uint ToAbgr(Color color)
    {
        return ((uint)color.A << 24)
            | ((uint)color.B << 16)
            | ((uint)color.G << 8)
            | color.R;
    }

    // ReSharper disable InconsistentNaming, UnusedMember.Local, NotAccessedField.Local
#pragma warning disable 649

    // Discovered via:
    // https://withinrafael.com/2015/07/08/adding-the-aero-glass-blur-to-your-windows-10-apps/
    // https://github.com/riverar/sample-win32-acrylicblur/blob/917adc277c7258307799327d12262ebd47fd0308/MainWindow.xaml.cs

    [DllImport("user32.dll")]
    private static extern int SetWindowCompositionAttribute(HandleRef hWnd, in WindowCompositionAttributeData data);

    private unsafe struct WindowCompositionAttributeData
    {
        public WCA Attribute;
        public void* Data;
        public int DataLength;
    }

    private enum WCA
    {
        ACCENT_POLICY = 19
    }

    private enum ACCENT
    {
        DISABLED = 0,
        ENABLE_GRADIENT = 1,
        ENABLE_TRANSPARENTGRADIENT = 2,
        ENABLE_BLURBEHIND = 3,
        ENABLE_ACRYLICBLURBEHIND = 4,
        INVALID_STATE = 5
    }

    private struct AccentPolicy
    {
        public ACCENT AccentState;
        public uint AccentFlags;
        public uint GradientColor;
        public uint AnimationId;
    }
}

这篇关于如何在 Winform c# 中创建 windows 10 亚克力透明效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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