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);//这里实现窗体半透明
}
}

这是半透明窗体

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

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

最新文章

  1. java分享第十四天(TestNG Assert详解)
  2. Ajax中Get请求与Post请求的区别
  3. js关闭子窗口,刷新父窗口
  4. [z]Oracle性能优化-读懂执行计划
  5. TCP/IP详解学习笔记(13)-TCP坚持定时器,TCP保活定时器
  6. 《DSP using MATLAB》示例Example4.10
  7. [转]Openwrt的Inittab
  8. ffmpeg 发布hls流
  9. JSOI2015 Round1——完挂
  10. JQuery 表单验证--jquery validation
  11. Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇
  12. for循环找出2到100的质数(素数)
  13. va_start
  14. 我想要革命想要解脱——bootstrap常见问题及解决方式
  15. EasyUI表格DataGrid格式化formatter用法
  16. 【OS】Heap &amp; Stack
  17. 【Selenium】【BugList2】geckodriver未安装,报:WebDriverException: Message: &#39;geckodriver&#39; executable needs to be in PATH.
  18. 转:UFLDL_Tutorial 笔记(deep learning绝佳的入门资料 )
  19. linux系统编程之文件与IO(八):文件描述符相关操作-dup,dup2,fcntl
  20. CATransform3D 使用

热门文章

  1. vue2.0 组件化及组件传值
  2. 菜鸟学Java(三)——JSTL标签之核心标签
  3. Gartner:2018人工智能技术成熟度曲线
  4. VLAN,GRE,VXLAN
  5. 利用es-checker检测当前node对ES6的支持情况
  6. 【Linux技术】BusyBox详解
  7. debian下创建新用户useradd
  8. js jquery 函数回调
  9. Python通过ssh连接服务器并执行命令
  10. s3c2440内存控制器与SDRAM基本测试