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. Fluent UDF【5】:第一个UDF

    这里以一个简单的初始化案例来描述UDF的使用过程. 0 Fluent中的Patch Fluent中提供了全域初始化以及局部Patch功能.对于整体区域的全局初始化可以采用starndard及hybri ...

  2. [Windows Azure] Developing Multi-Tenant Web Applications with Windows Azure AD

    Developing Multi-Tenant Web Applications with Windows Azure AD 2 out of 3 rated this helpful - Rate ...

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

    使用asio之前要先对它的设计思想有所了解,了解设计思想将有助于我们理解和应用asio.asio是基于proactor模式的,asio的proactor模式隐藏于大量的细节当中,要找到它的踪迹,往往有 ...

  4. vue.js中请求数据v-for循环使用数据

    1.效果图 2.cart.json { "message":"", "status":"1", "result ...

  5. iOS应用管理(优化)

    // //  ViewController.m //  01-应用管理 //  Created by apple on 17-5-14. //  Copyright (c) 2017年  All ri ...

  6. SecurityError: Blocked a frame with origin from accessing a cross-origin frame

    问题描述:浏览器报错I am loading an <iframe> in my HTML page and trying to access the elements within it ...

  7. Eigen教程(2)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...

  8. python json (loads(),load(),jump(),jumps())

    # loads() str to json data# jumps() json to str# jump() json to filedef ladstest(): data = '{"n ...

  9. 记一次从git@osc导入Android项目到Eclipse的过程

    . . . . . 之前写了一个Android的小项目,放在了git@osc上面托管代码.第一次开发完之后直接用git bash提交上去,然后每次修改都是手工通过git bash往上面合并代码.感觉很 ...

  10. JDBC插入数据超长时无法自动截断问题

    问题 JDBC操作MySQL数据库,当进行插入或更新操作的数据长度超过表字段的声明最大长度时,会报出以下错误,导致不能正常插入: 但是当直接在MySQL客户端操作时,发现确实可以的,只不过会自动对插入 ...