改变button按钮的形状(如何改变c#button控件的形状)

本文目录
- 如何改变c#button控件的形状
- java 怎么改变button的形状
- 怎样把button按钮设置成圆形按钮
- C#中改变按钮的形状!
- vb.net中 如何改变button控件的形状
- 怎么把button变成圆形
- web中如何改变button的形状
- C#如何改button形状
如何改变c#button控件的形状
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 path3 = this.GetGraphicsPath(rc2, 20);
PathGradientBrush br2 = new PathGradientBrush(path3);
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, path3);//绘制阴影
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(path3);
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);
}
}
}
java 怎么改变button的形状
1,首先明确button是安卓的一个控件,是用java语言写的。
2,设置大小的方法:btn.setbounds(x,y,width,height);//设置大小并定位
或者btn.setsize(width,height);//设置大小btn.setlocation(x,y);//定位
3,也可以在布局文件上直接给定大小
比如:
这个button控件高度和宽带都是100px
怎样把button按钮设置成圆形按钮
设置button完全为圆形: Button.layer.cornerRadius = 34.0;(该值到一定的程度,就为圆形了。) Button.layer.borderWidth = 1.0; Button.layer.borderColor =.CGColor; Button.clipsToBounds = TRUE;//去除边界
C#中改变按钮的形状!
使用Region属性。
先创建GraphicsPath对像,在其中实现一个形状,然后应用于控件的region属性.
例如创建圆形按钮:
private void roundButton_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Drawing2D.GraphicsPath buttonPath = new System.Drawing.Drawing2D.GraphicsPath();
System.Drawing.Rectangle newRectangle = roundButton.ClientRectangle;
newRectangle.Inflate(-10, -10);
e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);
newRectangle.Inflate( 1, 1);
buttonPath.AddEllipse(newRectangle);.
roundButton.Region = new System.Drawing.Region(buttonPath);
}
vb.net中 如何改变button控件的形状
vb.net中控件都是矩形的,如果一定要看起来是其他形状的,只能用背景图片,一般要三个背景图片,正常时候、鼠标移上时候、按下时候的。分别在按钮的四个事件:MouseHover MouseLeave MouseDown MouseUp的时候更换成相应的背景图片
怎么把button变成圆形
使用shape,请看下面截图,例子来自于android学习手册,360手机助手中下载,里面有108个例子、源码还有文档。
《?xml version="1.0" encoding="utf-8"?》
《shape
xmlns:Android="
android:shape="oval"》
《!-- 填充的颜色 --》
《solid android:color="#FFFFFF"/》
《!-- 设置按钮的四个角为弧形 --》
《!-- android:radius 弧形的半径 --》
《corners android:radius="360dip"/》
《!-- padding: Button 里面的文字与Button边界的间隔 --》
《padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/》
《/shape》
-----Main layout文件
《?xml version="1.0" encoding="utf-8"?》
《LinearLayout xmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
》
《TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/soft_info"
/》
《!—直接设置背景 --》
《Button
android:id="@+id/roundBtn1"
android:background="@drawable/btn_oval"
android:layout_width="50dip"
android:layout_height="50dip"
/》
《!— 调用shape自定义xml文件 --》
《Button
android:id="@+id/roundBtn"
android:text="椭圆按钮"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/main_menu_btnshape"
/》
《/LinearLayout》
----acitivity文件
public class MyLifeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
web中如何改变button的形状
2种办法:
1. 使用Map
可以通过HTML的MAP标签,把图片上的任意形状规定为按钮。
2. 对按钮使用样式
《input type="button" value="aaa" alt="" style="background-image:url(’aaa.jpg’)" /》
C#如何改button形状
ImageButton是asp.net中的控件。如果说是winform,可以用普通的button 设置背景图和按钮的显示格式。也可以用imagebox控件。 \x0d\x0a\x0d\x0ausing System;\x0d\x0ausing System.Collections;\x0d\x0ausing System.ComponentModel;\x0d\x0ausing System.Drawing;\x0d\x0ausing System.Data;\x0d\x0ausing System.Windows.Forms;\x0d\x0ausing System.Drawing.Drawing2D;\x0d\x0ausing System.Drawing.Imaging;\x0d\x0ausing System.Text;\x0d\x0a\x0d\x0anamespace 自定义水晶按钮\x0d\x0a{\x0d\x0a///

更多文章:
中文自动翻译成英语的输入法(有没有一种输入法能将打出来的中文自动变成英文)
2026年11月7日 02:10
equals to(为什么有些英文句子要用的是“equals to that of” 这个that和of之间还可以加名词吗)
2026年11月7日 01:50
default values(电脑开机后出现all settings was reset to default values.)
2026年11月7日 01:20
structured reviewing翻译(软件测试的英文翻译)
2026年11月7日 00:50
dos命令清屏(Windows DOS内部命令详解04——del、ren、cls、ver、date、time)
2026年11月6日 15:40
constantly的用法及词组(英语时态对应的时间状语都有哪些)
2026年11月6日 15:20






