最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

C#制作透明水晶按钮代码

来源:动视网 责编:小OO 时间:2025-09-27 21:38:54
文档

C#制作透明水晶按钮代码

C#制作透明水晶按钮代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Drawing.Drawing2D;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;namespaceMyControls{publicpa
推荐度:
导读C#制作透明水晶按钮代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Drawing.Drawing2D;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;namespaceMyControls{publicpa
C#制作透明水晶按钮代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Imaging;

using System.Drawing.Drawing2D;

using System.Data;

using System.Text;

using System.Windows.Forms;

namespace MyControls

{

public partial class CrystalButton : Button

{

private enum MouseActionType

{

None,

Hover,

Click

}

private MouseActionType mouseAction;

private ImageAttributes imgAttr = new ImageAttributes();

private Bitmap buttonBitmap;

private Rectangle buttonBitmapRectangle;

public CrystalButton()

{

InitializeComponent();

mouseAction = MouseActionType.None;

this.SetStyle(ControlStyles.AllPaintingInWmPaint |

ControlStyles.DoubleBuffer |

ControlStyles.UserPaint, true);

//The following defaults are better suited to draw the text outline

this.Font = new Font("Arial Black

钮阴影

///

Rectangle rc2 = rc;

rc2.Offset(shadowOffset, shadowOffset);

GraphicsPath path2 = this.GetGraphicsPath(rc2, 20);

PathGradientBrush br2 = new PathGradientBrush(path2);

br2.CenterColor = Color.Black;

br2.SurroundColors = new Color[] {SystemColors.ButtonFace};

//为了更逼真,我们将渐变结束颜色设定为窗体前景颜色,可以根据窗口的前景颜色适当调整

///

/// 创建按钮顶部白色渐变

///

Rectangle rc3 = rc;

rc3.Inflate(-5, -5);

rc3.Height = 15;

GraphicsPath path3 = GetGraphicsPath(rc3, 20);

LinearGradientBrush br3 = new LinearGradientBrush(rc3, Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical);

///

/// 绘制图形

///

g.FillPath(br2, path2);//绘制阴影

g.FillPath(br1, path1); //绘制按钮

g.FillPath(br3, path3); //绘制顶部白色泡泡

///

///设定内存位图对象,进行二级缓存绘图操作

///

buttonBitmapRectangle = new Rectangle(rc.Location, rc.Size);

buttonBitmap = new Bitmap(buttonBitmapRectangle.Width, buttonBitmapRectangle.Height);

Graphics g_bmp = Graphics.FromImage(buttonBitmap);

g_bmp.SmoothingMode = SmoothingMode.AntiAlias;

g_bmp.FillPath(br1, path1);

g_bmp.FillPath(br3, path3);

///

///将region赋值给button

Region rgn = new Region(path1);

rgn.Union(path2);

this.Region = rgn;

///

/// 绘制按钮的文本

///

GraphicsPath path4 = new GraphicsPath();

RectangleF path1bounds = path1.GetBounds();

Rectangle rcText = new Rectangle((int)path1bounds.X + btnOffset, (int)path1bounds.Y + btnOffset, (int)path1bounds.Width, (int)path1bounds.Height);

StringFormat strformat = new StringFormat();

strformat.Alignment = StringAlignment.Center;

strformat.LineAlignment = StringAlignment.Center;

path4.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size, rcText, strformat);

Pen txtPen = new Pen(this.ForeColor , 1);

g.DrawPath(txtPen, path4);

g_bmp.DrawPath(txtPen, path4);

}

protected override void OnMouseDown(MouseEventArgs e)

{

if (e.Button == MouseButtons.Left)

{

this.mouseAction = MouseActionType.Click;

this.Invalidate();

}

base.OnMouseDown(e);

}

protected override void OnMouseUp(MouseEventArgs e)

{

this.mouseAction = MouseActionType.Hover;

this.Invalidate();

base.OnMouseUp(e);

}

protected override void OnMouseHover(EventArgs e)

{

this.mouseAction = MouseActionType.Hover;

this.Invalidate();

base.OnMouseHover(e);

}

protected override void OnMouseEnter(EventArgs e)

{

this.mouseAction = MouseActionType.Hover;

this.Invalidate();

base.OnMouseEnter(e);

}

protected override void OnMouseLeave(EventArgs e)

{

this.mouseAction = MouseActionType.None;

this.Invalidate();

base.OnMouseLeave(e);

}

}

}

文档

C#制作透明水晶按钮代码

C#制作透明水晶按钮代码usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Drawing.Drawing2D;usingSystem.Data;usingSystem.Text;usingSystem.Windows.Forms;namespaceMyControls{publicpa
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top