https://www.cnblogs.com/mqxs/p/9466218.html

public class FormShadow : Form
{ public FormShadow()
{
Initialize(); }
/// <summary>
/// 界面加载
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
dwmInitialize();
base.OnLoad(e); } protected bool dwmEnabled;
protected int dwmleft;
protected int dwmtop;
protected int dwmright;
protected int dwmbottom;
private const int WS_MINIMIZEBOX = 0x00020000;
/// <summary>
/// 界面绘制
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
if (dwmEnabled)
{
e.Graphics.Clear(Color.FromArgb(0, 0, 0, 0));
DrawShadow(this.Size, dwmleft, e.Graphics);
}
RectangleF rect = new RectangleF(dwmleft - 0.5f, dwmtop - 0.5f, this.Width - dwmleft - dwmright + 0.5f, this.Height - dwmtop - dwmbottom + 0.5f);
SolidBrush brush = new SolidBrush(this.BackColor);
e.Graphics.FillRectangle(brush, rect);
brush.Dispose(); brush = null;
}
/// <summary>
///
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style = cp.Style | WS_MINIMIZEBOX;
return cp;
}
}
/// <summary>
/// 绘制阴影效果
/// </summary>
/// <param name="size">控件尺寸</param>
/// <param name="radius">阴影半径</param>
/// <param name="g">绘图区</param>
private void DrawShadow(Size size, int radius, Graphics g)
{
if (radius <= 0) return;
int d = 2 * radius;
#region[LinearGradientBrush]
ColorBlend blend = new ColorBlend();
blend.Colors = new Color[] { Color.FromArgb(100, Color.Black), Color.FromArgb(40, Color.Black), Color.FromArgb(0, Color.Black) };
blend.Positions = new float[] { 0, 0.4f, 1 };
LinearGradientBrush brushleft = new LinearGradientBrush(new Point(radius, 0), new Point(0, 0), Color.FromArgb(60, Color.Black), Color.FromArgb(0, Color.Black));
brushleft.InterpolationColors = blend;
LinearGradientBrush brushright = new LinearGradientBrush(new Point(size.Width - radius - 1, 0), new Point(size.Width, 0), Color.FromArgb(80, Color.Black), Color.FromArgb(0, Color.Black));
brushright.InterpolationColors = blend;
LinearGradientBrush brushtop = new LinearGradientBrush(new Point(0, radius), new Point(0, 0), Color.FromArgb(60, Color.Black), Color.FromArgb(0, Color.Black));
brushtop.InterpolationColors = blend;
LinearGradientBrush brushbottom = new LinearGradientBrush(new Point(0, size.Height - radius - 1), new Point(0, size.Height), Color.FromArgb(80, Color.Black), Color.FromArgb(0, Color.Black));
brushbottom.InterpolationColors = blend;
#endregion
#region[path]
GraphicsPath pathlefttop = new GraphicsPath();
pathlefttop.AddPie(new Rectangle(0, 0, d, d), 180, 90);
GraphicsPath pathrighttop = new GraphicsPath();
pathrighttop.AddPie(new Rectangle(this.Width - d, 0, d, d), 270, 90);
GraphicsPath pathleftbottom = new GraphicsPath();
pathleftbottom.AddPie(new Rectangle(0, this.Height - d, d, d), 90, 90);
GraphicsPath pathrightbottom = new GraphicsPath();
pathrightbottom.AddPie(new Rectangle(this.Width - d, this.Height - d, d, d), 0, 90);
#endregion
#region[PathGradientBrush]
PathGradientBrush brushlefttop = new PathGradientBrush(pathlefttop);
brushlefttop.CenterPoint = new Point(radius, radius);
brushlefttop.CenterColor = Color.FromArgb(80, Color.Black);
brushlefttop.SurroundColors = new Color[] { Color.FromArgb(0, Color.Black) };
//brushlefttop.InterpolationColors = blend;
PathGradientBrush brushrighttop = new PathGradientBrush(pathrighttop);
brushrighttop.CenterPoint = new Point(this.Width - radius, radius);
brushrighttop.CenterColor = Color.FromArgb(80, Color.Black);
brushrighttop.SurroundColors = new Color[] { Color.FromArgb(0, Color.Black) };
//brushrighttop.InterpolationColors = blend;
PathGradientBrush brushleftbottom = new PathGradientBrush(pathleftbottom);
brushleftbottom.CenterPoint = new Point(radius, this.Height - radius);
brushleftbottom.CenterColor = Color.FromArgb(80, Color.Black);
brushleftbottom.SurroundColors = new Color[] { Color.FromArgb(0, Color.Black) };
//brushleftbottom.InterpolationColors = blend;
PathGradientBrush brushrightbottom = new PathGradientBrush(pathrightbottom);
brushrightbottom.CenterPoint = new Point(this.Width - radius, this.Height - radius);
brushrightbottom.CenterColor = Color.FromArgb(80, Color.Black);
brushrightbottom.SurroundColors = new Color[] { Color.FromArgb(0, Color.Black) };
//brushrightbottom.InterpolationColors = blend;
#endregion
#region[draw]
g.FillRectangle(brushleft, new RectangleF(1, radius - 0.5f, radius, this.Height - d + 0.5f));
g.FillRectangle(brushright, new RectangleF(this.Width - radius - 1, radius - 0.5f, radius, this.Height - d + 0.5f));
g.FillRectangle(brushtop, new RectangleF(radius - 0.5f, 0, this.Width - d + 0.5f, radius));
g.FillRectangle(brushbottom, new RectangleF(radius - 0.5f, this.Height - radius - 1, this.Width - d + 0.5f, radius));
g.FillPath(brushlefttop, pathlefttop);
g.FillPath(brushrighttop, pathrighttop);
g.FillPath(brushleftbottom, pathleftbottom);
g.FillPath(brushrightbottom, pathrightbottom);
#endregion
#region[dispose]
brushleft.Dispose(); brushleft = null;
brushright.Dispose(); brushright = null;
brushtop.Dispose(); brushtop = null;
brushbottom.Dispose(); brushbottom = null;
pathlefttop.Dispose(); pathlefttop = null;
pathrighttop.Dispose(); pathrighttop = null;
pathleftbottom.Dispose(); pathleftbottom = null;
pathrightbottom.Dispose(); pathrightbottom = null;
brushlefttop.Dispose(); brushlefttop = null;
brushrighttop.Dispose(); brushrighttop = null;
brushleftbottom.Dispose(); brushleftbottom = null;
brushrightbottom.Dispose(); brushrightbottom = null;
#endregion
}
/// <summary>
/// 初始化
/// </summary>
private void Initialize()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.CenterScreen; }
/// <summary>
/// dwm初始化
/// </summary>
private void dwmInitialize()
{
#region[dwmInitialize]
this.dwmEnabled = true;
dwmleft = this.Padding.Left;
dwmtop = this.Padding.Top;
dwmright = this.Padding.Right;
dwmbottom = this.Padding.Bottom;
int flag = 0;
MARGINS mg = new MARGINS();
mg.m_Left = dwmleft;
mg.m_Top = dwmtop;
mg.m_Right = dwmright;
mg.m_Bottom = dwmbottom;
//判断Vista系统
if (System.Environment.OSVersion.Version.Major >= 6)
{
DwmIsCompositionEnabled(ref flag); //检测Aero是否为打开
if (flag > 0)
{
DwmExtendFrameIntoClientArea(this.Handle, ref mg);
}
else
{
dwmEnabled = false;
dwmleft = 0;
dwmtop = 0;
dwmright = 0;
dwmbottom = 0;
//MessageBox.Show("Desktop Composition is Disabled!");
}
}
else
{
dwmEnabled = false;
dwmleft = 0;
dwmtop = 0;
dwmright = 0;
dwmbottom = 0;
//MessageBox.Show("Please run this on Windows Vista.");
}
GC.Collect();
#endregion
} /// <summary>
///
/// </summary>
public struct MARGINS
{
public int m_Left;
public int m_Right;
public int m_Top;
public int m_Bottom;
};
[DllImport("dwmapi.dll")]
private static extern void DwmIsCompositionEnabled(ref int enabledptr);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
[DllImport("dwmapi.dll")]
private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margin);
}

C# WinForm 窗体阴影的更多相关文章

  1. winform 移动窗体,和窗体阴影(引用)

    无边框窗体移动://窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [D ...

  2. winform 窗体移动API、窗体阴影API

    //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport ...

  3. winform窗体 小程序【移动窗体和阴影】

    窗体无边框设置后无法移动,引用API 使其获得功能 移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ...

  4. WINFORM 无边框窗体 阴影与移动

    //窗体移动API[DllImport("user32.dll")]public static extern bool ReleaseCapture();[DllImport(&q ...

  5. WinForm 窗体API移动 API阴影

    窗体移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllI ...

  6. WinForm 窗体属性 窗体美化

    WinForm是·Net开发平台中对Windows Form的一种称谓. Windows窗体的一些重要特点如下: 功能强大:Windows窗体可用于设计窗体和可视控件,以创建丰富的基于Windows的 ...

  7. 重绘Winform窗体

    本文转载自:http://www.cnblogs.com/encoding/p/5603080.html 按照惯例,先来几张样例图(注:为了展示窗口阴影效果,截图范围向外扩展了些,各位凭想象吧). 还 ...

  8. 【开源】做了一个WinForm窗体的投影组件,能够为窗口添加影子效果

    最近手头上的项目终于忙得差不多了,想起好久没有更新了的NanUI,再看着每天QQ群未读消息闪烁的标志,突然才发现似乎愧对了群里各位喜爱NanUI的朋友们.于是乎,就想趁这几天有时间,好好的修复一下Na ...

  9. winform 窗体圆角设计

    网上看到的很多winform窗体圆角设计代码都比较累赘,这里分享一个少量代码就可以实现的圆角.主要运用了System.Drawing.Drawing2D. 效果图 代码如下. private void ...

  10. winform窗体置顶

    winform窗体置顶 金刚 winform 置顶 今天做了一个winform小工具.需要设置置顶功能. 网上找了下,发现百度真的很垃圾... 还是必应靠谱些. 找到一个可以链接. https://s ...

随机推荐

  1. unigui的错误delphi clientHeight:property clientheight does not exist【10】

    在unigui运行中发现这样的错误clientHeight:property clientheight does not exist. 这是啥原因.从老版本中复制过来的代码含dfm会出现这样的错误. ...

  2. 手写数字识别实战教程:从零实现MNIST分类器(完整代码示例)

    引言:数字识别的魔法世界 在人工智能的奇妙宇宙中,手写数字识别堪称经典中的经典.这个看似简单的任务--让电脑像人一样"认数字",背后蕴含着模式识别的核心思想.本文将带领你亲手实现一 ...

  3. ocr识别遇到的小问题-图片的EXIF 元数据

    背景   之前在公司通过paddleocr写了个接口,传入图片的base64编码返回识别出的文字信息.但是图片为横向时,文字行会乱序,所以准备新加一个功能通过paddleclas推理图片文字的倾斜角度 ...

  4. JDBC-增删查改操作

    使用场景:测试家族族长分成时需要批量添加家族流水记录,但手动添加和SQL语句添加较为麻烦 操作步骤 运行环境:Java8+IDEA 1.打开IDEA 点击File->New->Projec ...

  5. LVGL基础对象的框架、思维与概念,以及部分源码解析

    概念: Lvgl虽然是以C语言进行编程开发,但其中借鉴了CSS当中编程思想,引入了类与对象的概念,其中lvgl的基础单位为控件,等同于HTML5当中的标签. Lvgl当中的"类"是 ...

  6. 一、Java语言介绍

    1.硬件知识介绍 2.常用dos命令以及快捷键 1 /** 2 *@desc: 复习 3 *@Description: 4 * dir:列出当前文件目录下的所有文件; 5 * md:创建一个新目录; ...

  7. OpenEuler22.03源码编译安装nginx1.24.0

    一.环境说明 操作系统版本:OpenEuler22.03 SP2 LTS Nginx版本:1.24.0 安装位置:/app/nginx Selinux配置:关闭或设置为permissive 二.Ngi ...

  8. github项目收集

    web模块 Nginx 监控模块vts: https://github.com/vozlt/nginx-module-vts

  9. thinkphphp 计算分页 和分页总数 和sql计算分页 php

    利用page计算分页 $p=input('p')?input('p'):1; $limit=6; $res=db('points_log')->where(['p_uid'=>$uid,' ...

  10. Windows查看端口占用、相应进程、杀死进程等[netstat]

    Windows 通过cmd或powerShell查看端口占用.相应进程.杀死进程等的命令 由于一般开发环境是在windows上,相应的一些测试必然涉及到一些端口的监听与使用.当开发使用的端口被占用后, ...