如何在Winform c#

时间:2019-06-06 15:58:00

标签: c# .net winforms

我想创建一个支持丙烯酸材料设计的表单。正如您在Microsoft几乎所有应用中所看到的那样,我都希望具有透明效果。我想那样创建。由于DevExpress已经完成了,所以这并非没有可能(流利的形式)

所以任何人都有在Winform中使Windows 10透明效果的代码

2 个答案:

答案 0 :(得分:1)

在Windows窗体中获得丙烯酸效果本身并不特别困难(请参阅https://withinrafael.com/2015/07/08/adding-the-aero-glass-blur-to-your-windows-10-apps/),但真正困难的部分是使Win32本身能够很好地工作:

调整表格的大小也严重滞后于鼠标光标本身。我很想看一下DevExpress的功能,但是看起来除了要做丙烯酸之外还有很多事情要做。您很有可能将不得不自定义其上的所有内容,这与Aero glass相似,但更糟糕的是,因为您现在也必须自定义标题栏和边框。

示例代码

警告!这些是未公开的API。在继续之前,请参阅Raymond Chen的When programs grovel into undocumented structures…

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;
    }
}

答案 1 :(得分:0)

这很难做到。在DevExpress中,有大量的专业C#程序员。

尝试切换到通用Windows应用程序。在那里更容易。

有一份UWP指南:https://docs.microsoft.com/en-US/windows/uwp/get-started/universal-application-platform-guide