1.无边框窗体阴影,win7(需要开启Aero效果)及以上系统

public class LdwmForm : Form
{
public LdwmForm()
{
Initialize();
}
/// <summary>
/// 界面加载
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
dwmInitialize();
base.OnLoad(e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseDown(MouseEventArgs e)
{
_IsMouseDown = true;
_location = this.Location;
_startPoint = Control.MousePosition;
base.OnMouseDown(e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
if (_IsMouseDown)
{
Point p = Control.MousePosition;
this.Location = new Point(_location.X + p.X - _startPoint.X, _location.Y + p.Y - _startPoint.Y);
}
base.OnMouseMove(e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
{
_IsMouseDown = false;
base.OnMouseUp(e);
}
/// <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(, , , ));
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 <= ) return;
int d = * radius;
#region[LinearGradientBrush]
ColorBlend blend = new ColorBlend();
blend.Colors = new Color[] { Color.FromArgb(, Color.Black), Color.FromArgb(, Color.Black), Color.FromArgb(, Color.Black) };
blend.Positions = new float[] { , 0.4f, };
LinearGradientBrush brushleft = new LinearGradientBrush(new Point(radius, ), new Point(, ), Color.FromArgb(, Color.Black), Color.FromArgb(, Color.Black));
brushleft.InterpolationColors = blend;
LinearGradientBrush brushright = new LinearGradientBrush(new Point(size.Width - radius - , ), new Point(size.Width, ), Color.FromArgb(, Color.Black), Color.FromArgb(, Color.Black));
brushright.InterpolationColors = blend;
LinearGradientBrush brushtop = new LinearGradientBrush(new Point(, radius), new Point(, ), Color.FromArgb(, Color.Black), Color.FromArgb(, Color.Black));
brushtop.InterpolationColors = blend;
LinearGradientBrush brushbottom = new LinearGradientBrush(new Point(, size.Height - radius - ), new Point(, size.Height), Color.FromArgb(, Color.Black), Color.FromArgb(, Color.Black));
brushbottom.InterpolationColors = blend;
#endregion
#region[path]
GraphicsPath pathlefttop = new GraphicsPath();
pathlefttop.AddPie(new Rectangle(, , d, d), , );
GraphicsPath pathrighttop = new GraphicsPath();
pathrighttop.AddPie(new Rectangle(this.Width - d, , d, d), , );
GraphicsPath pathleftbottom = new GraphicsPath();
pathleftbottom.AddPie(new Rectangle(, this.Height - d, d, d), , );
GraphicsPath pathrightbottom = new GraphicsPath();
pathrightbottom.AddPie(new Rectangle(this.Width - d, this.Height - d, d, d), , );
#endregion
#region[PathGradientBrush]
PathGradientBrush brushlefttop = new PathGradientBrush(pathlefttop);
brushlefttop.CenterPoint = new Point(radius, radius);
brushlefttop.CenterColor = Color.FromArgb(, Color.Black);
brushlefttop.SurroundColors = new Color[] { Color.FromArgb(, Color.Black) };
//brushlefttop.InterpolationColors = blend;
PathGradientBrush brushrighttop = new PathGradientBrush(pathrighttop);
brushrighttop.CenterPoint = new Point(this.Width - radius, radius);
brushrighttop.CenterColor = Color.FromArgb(, Color.Black);
brushrighttop.SurroundColors = new Color[] { Color.FromArgb(, Color.Black) };
//brushrighttop.InterpolationColors = blend;
PathGradientBrush brushleftbottom = new PathGradientBrush(pathleftbottom);
brushleftbottom.CenterPoint = new Point(radius, this.Height - radius);
brushleftbottom.CenterColor = Color.FromArgb(, Color.Black);
brushleftbottom.SurroundColors = new Color[] { Color.FromArgb(, Color.Black) };
//brushleftbottom.InterpolationColors = blend;
PathGradientBrush brushrightbottom = new PathGradientBrush(pathrightbottom);
brushrightbottom.CenterPoint = new Point(this.Width - radius, this.Height - radius);
brushrightbottom.CenterColor = Color.FromArgb(, Color.Black);
brushrightbottom.SurroundColors = new Color[] { Color.FromArgb(, Color.Black) };
//brushrightbottom.InterpolationColors = blend;
#endregion
#region[draw]
g.FillRectangle(brushleft, new RectangleF(, radius - 0.5f, radius, this.Height - d + 0.5f));
g.FillRectangle(brushright, new RectangleF(this.Width - radius - , radius - 0.5f, radius, this.Height - d + 0.5f));
g.FillRectangle(brushtop, new RectangleF(radius - 0.5f, , this.Width - d + 0.5f, radius));
g.FillRectangle(brushbottom, new RectangleF(radius - 0.5f, this.Height - radius - , 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;
dwmleft = ;
dwmtop = ;
dwmright = ;
dwmbottom = ;
}
/// <summary>
/// dwm初始化
/// </summary>
private void dwmInitialize()
{
#region[dwmInitialize]
this.dwmEnabled = true;
int flag = ;
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 >= )
{
DwmIsCompositionEnabled(ref flag); //检测Aero是否为打开
if (flag > )
{
DwmExtendFrameIntoClientArea(this.Handle, ref mg);
}
else
{
dwmEnabled = false;
dwmleft = ;
dwmtop = ;
dwmright = ;
dwmbottom = ;
//MessageBox.Show("Desktop Composition is Disabled!");
}
}
else
{
dwmEnabled = false;
dwmleft = ;
dwmtop = ;
dwmright = ;
dwmbottom = ;
//MessageBox.Show("Please run this on Windows Vista.");
}
GC.Collect();
#endregion
} protected bool dwmEnabled;
protected int dwmleft;
protected int dwmtop;
protected int dwmright;
protected int dwmbottom;
private bool _IsMouseDown;
private Point _location;
private Point _startPoint;
private const int WS_MINIMIZEBOX = 0x00020000; /// <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);
}

继承以上窗体自动实现无边框窗体阴影功能

2、全透明或半透明窗体实现

public partial class Form1 : LdwmForm
{
public Form1()
{
InitializeComponent();
dwmleft = ;//这个是调节窗体阴影宽度,设置一个很大的值,可以使整个窗体全透
dwmtop = ;
dwmright = ;
dwmbottom = ;
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(, , , )), this.ClientRectangle);//这里实现窗体半透明
}
}

这是半透明窗体

无边框窗体改变窗体尺寸问题

无边框窗体最大化最小化问题

【C#】使用DWM实现无边框窗体阴影或全透窗体的更多相关文章

  1. 初学c# -- 学习笔记(五) winfrom无边框四周阴影

    刚用到这个功能,网上扯淡的东西太多了,都是2边阴影,还什么窗口叠加.ps作图啥的,什么玩意.还是老外实在,google找的,无边框窗体,四边透明阴影. public partial class For ...

  2. C# 无边框窗体移动和改变大小的实现

    自己给软件做的皮肤,将窗体设为无边框后,想要其具有正常窗体的移动和改变大小功能,以下代 码可以实现.... //需添加using System.Runtime.InteropServices; [Dl ...

  3. Winform 无边框窗口移动自定义边框粗细颜色

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 如何在pyqt中给无边框窗口添加DWM环绕阴影

    前言 在之前的博客<如何在pyqt中通过调用SetWindowCompositionAttribute实现Win10亚克力效果>中,我们实现了窗口的亚克力效果,同时也用SetWindowC ...

  5. winfrom 无边框窗体移动和阴影

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

  6. 2017年11月20日 WinForm窗体 窗口无边框可移动&&窗口阴影 控制窗口关闭/最小化

    弹框 MessageBox.Show(); 清空 clear() 字符串拼接 string 公共控件 button 按钮 checkbox 复选框 checklistbox 多个复选框 combobo ...

  7. Winform无边框窗体的移动和阴影

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

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

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

  9. 无边框窗体和timer控件

    一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要放置的控件可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入(pictureBox1_MouseE ...

随机推荐

  1. fpm制做mysql-5.6.33 rpm包

    增加用户: # groupadd -r mysql # useradd -g mysql -r -s /sbin/nologin -M -d /data/my_db mysql 源码安装mysql-5 ...

  2. 为什么我们做分布式要使用Redis

    绝大部分写业务的程序员,在实际开发中使用 Redis 的时候,只会 Set Value 和 Get Value 两个操作,对 Redis 整体缺乏一个认知.这里对 Redis 常见问题做一个总结,解决 ...

  3. redhat 6、7配置yum源

    # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # sed -i  ' ...

  4. (原创)拨开迷雾见月明-剖析asio中的proactor模式(二)

    在上一篇博文中我们提到异步请求是从上层开始,一层一层转发到最下面的服务层的对象win_iocp_socket_service,由它将请求转发到操作系统(调用windows api),操作系统处理完异步 ...

  5. Linux内核分析:recv、recvfrom、recvmsg函数实现

    先看一下这三个函数的声明: #include <sys/types.h> #include <sys/socket.h> ssize_t recv(int sockfd, vo ...

  6. $.ajax使用总结(一):Form提交与Payload提交

    http://blog.csdn.net/yiifaa/article/details/73468001 *********************************************** ...

  7. 【转】解决Lost connection to MySQL server during query错误方法

    初步判断是MySQL可能挂掉了,在系统服务里面查看MySQL的进程并没有停止. 最开始考虑是数据库结构不对,但是我是通过Navicat for MySQL的备份和恢复备份导入数据,应该表结构都在备份文 ...

  8. 06-老马jQuery教程-jQuery高级

    1.jQuery原型对象解密 jQuery里面的大部分API都是在jQuery的原型对象上定义的.jQuery源码中对原型对象做了简写的处理.也就是说:jQuery.fn === jQuery.pro ...

  9. WPF学习笔记(3)——style

    http://www.cnblogs.com/Zhouyongh/archive/2011/08/01/2123610.html Style 用来在类型的不同实例之间共享属性.资源和事件处理程序,您可 ...

  10. 4. Stacked AutoEncoder(堆栈自动编码器)

    1. AutoEncoder介绍 2. Applications of AutoEncoder in NLP 3. Recursive Autoencoder(递归自动编码器) 4. Stacked ...