首页 技术 正文
技术 2022年11月16日
0 收藏 411 点赞 4,517 浏览 7271 个字

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);
}

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

【C#】使用DWM实现无边框窗体阴影或全透窗体

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实现无边框窗体阴影或全透窗体

这是半透明窗体

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892