如何改变c#button控件的形状

 我来答
地瓜说机
2017-10-04 · TA获得超过2.9万个赞
知道大有可为答主
回答量:2.3万
采纳率:91%
帮助的人:1.1亿
展开全部
ImageButton是asp.net中的控件。如果说是winform,可以用普通的button 设置背景图和按钮的显示格式。也可以用imagebox控件。

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;

namespace 自定义水晶按钮
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class TestButton : Button
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

private enum MouseActionType//定义鼠标的点击类型
{
None,
Hover,
Click
}

private MouseActionType mouseAction;
private ImageAttributes imgAttr = new ImageAttributes();
private Bitmap buttonBitmap;
private Rectangle buttonBitmapRectangle;

public TestButton()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化

mouseAction = MouseActionType.None;

//应用双缓冲技术是画面不闪烁
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer |
ControlStyles.UserPaint, true);

//修改默认的字体,背景色,大小
this.Font = new Font("宋体", 12, FontStyle.Bold);
this.BackColor = Color.DarkTurquoise;
this.Size = new Size(100, 40);

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

private GraphicsPath GetGraphicsPath(Rectangle rc, int r)
{
int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
GraphicsPath path = new GraphicsPath();
path.AddArc(x, y, r, r, 180, 90); //左上角的椭圆
path.AddArc(x + w - r, y, r, r, 270, 90); //右上角的椭圆
path.AddArc(x + w - r, y + h - r, r, r, 0, 90); //左下角的椭圆
path.AddArc(x, y + h - r, r, r, 90, 90); //右下角的椭圆
path.CloseFigure();
return path;
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//g.Clear(Color.White);
g.Clear(SystemColors.Control);//清空画布的三维颜色
Color clr = this.BackColor;
int shadowOffset = 8;
int btnOffset = 0;
switch (mouseAction)
{
case MouseActionType.Click:
shadowOffset = 4;
clr = Color.DeepSkyBlue;
btnOffset = 2;
break;
case MouseActionType.Hover:
clr = Color.DeepSkyBlue;
break;
}
g.SmoothingMode = SmoothingMode.AntiAlias;

///
/// 创建按钮本身的图形
///
Rectangle rc = new Rectangle(btnOffset, btnOffset, this.ClientSize.Width - 8 - btnOffset, this.ClientSize.Height - 8 - btnOffset);
GraphicsPath path1 = this.GetGraphicsPath(rc, 20);

//一个起始点和一个终点 用渐变的颜色来填充
LinearGradientBrush br1 = new LinearGradientBrush(new Point(0, 0), new Point(0, rc.Height + 6), clr, Color.White);

///
/// 创建按钮阴影
///
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.Control};
//为了更逼真,我们将渐变结束颜色设定为窗体前景颜色,可以根据窗口的前景颜色适当调整

///
/// 创建按钮顶部白色渐变
///
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);
}
}
}
就烦条0o
2017-10-05 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46487
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
通过设置Button.Region来修改形状。比如:
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
GraphicsPath path=new GraphicsPath();
path.AddEllipse(this.button1.ClientRectangle);
Region reg = new Region(path);
this.button1.Region=reg;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式