开发人员技术 | C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AcrylicNativeDemo
{
public class MainForm : Form
{
private enum ACCENT_STATE
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4
}
[StructLayout(LayoutKind.Sequential)]
private struct ACCENT_POLICY
{
public ACCENT_STATE AccentState;
public uint AccentFlags;
public uint GradientColor; // 0xAARRGGBB
public uint AnimationId;
}
[StructLayout(LayoutKind.Sequential)]
private struct WindowCompositionAttributeData
{
public int Attribute;
public IntPtr Data;
public int SizeOfData;
}
[DllImport("user32.dll")]
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
private const int WCA_ACCENT_POLICY = 19;
[DllImport("dwmapi.dll")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
private const int DWMWA_CORNER_PREFERENCE = 33;
private const int DWMWCP_ROUND = 2;
[DllImport("user32.dll")]
private static extern bool ReleaseCapture();
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x02;
public MainForm()
{
FormBorderStyle = FormBorderStyle.None;
// ✅ 重点:A=255,不透明纯色!不再带Alpha,杜绝报错
BackColor = Color.FromArgb(255, 0, 0, 0);
Size = new Size(500, 240);
StartPosition = FormStartPosition.CenterScreen;
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
int corner = DWMWCP_ROUND;
DwmSetWindowAttribute(this.Handle, DWMWA_CORNER_PREFERENCE, ref corner, sizeof(int));
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
EnableAcrylic(this.Handle, 0x404c6f14);
}
private void EnableAcrylic(IntPtr hWnd, uint gradientColor)
{
ACCENT_POLICY policy = new ACCENT_POLICY
{
AccentState = ACCENT_STATE.ACCENT_ENABLE_BLURBEHIND,
AccentFlags = 0,
GradientColor = gradientColor,
AnimationId = 0
};
int size = Marshal.SizeOf(policy);
IntPtr pPolicy = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(policy, pPolicy, false);
WindowCompositionAttributeData wcd = new WindowCompositionAttributeData
{
Attribute = WCA_ACCENT_POLICY,
Data = pPolicy,
SizeOfData = size
};
SetWindowCompositionAttribute(hWnd, ref wcd);
Marshal.FreeHGlobal(pPolicy);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
using (Font font = new Font("微软雅黑", 24, FontStyle.Bold))
using (SolidBrush wb = new SolidBrush(Color.White))
{
string text = ""Text1文字示例;
SizeF sz = g.MeasureString(text, font);
g.DrawString(text, font, wb, (Width - sz.Width) / 2, (Height - sz.Height) / 2);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。