咋个在C#中制作圆形的按钮(菜鸟提问,希望各位大大回答详细点哦!。。谢谢!)

Girl_my_lover 2009-02-08 10:41:11
C#中的普通按钮都是方形的啊、
我想做个圆形的按钮。。不知道各位大大有什么好的技巧
最好带点代码。
我是菜鸟哈
谢谢!
...全文
1205 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nmgdxz 2010-08-15
  • 打赏
  • 举报
回复
学习一下
changmengmeng 2009-02-08
  • 打赏
  • 举报
回复
1、用PIctuerBox也行

2、用VB写的代码: RoundRectFrame Picture1, 30, 30

'创建圆角矩形
Public Sub RoundRectFrame(objEllipse As Object, Optional ByVal x3 As Long = 8 _
, Optional ByVal y3 As Long = 8)
Dim lngW As Long, lngH As Long
Dim hWndRet As Long

lngW = objEllipse.Width \ Screen.TwipsPerPixelX
lngH = objEllipse.Height \ Screen.TwipsPerPixelY

hWndRet = CreateRoundRectRgn(0, 0, lngW, lngH, x3, y3)
Call SetWindowRgn(objEllipse.hWnd, hWndRet, True)

End Sub
3、用c#代码做的
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;


namespace WindowsApplication1
{
/// <summary>
/// Summary description for cilpButton.
/// </summary>
public class cilpButton : System.Windows.Forms.Button
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(0,0, this.Width, this.Height);
this.Region = new Region(path);
}

protected override void OnMouseEnter(EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawEllipse(new Pen(Color.Red), 0, 0 , this.Width, this.Height);
g.Dispose();
}
protected override void OnMouseLeave(EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawEllipse(new Pen(this.BackColor), 0, 0 , this.Width, this.Height);
g.Dispose();
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
Pen pen = new Pen(this.BackColor);
Graphics g = this.CreateGraphics();
g.Clear(Color.Goldenrod);
g.FillEllipse(Brushes.DarkKhaki, new Rectangle(0,0, this.Width, this.Height));
}

protected override void OnMouseDown(MouseEventArgs e)
{
OnPaintBackground(null);
}
protected override void OnMouseUp(MouseEventArgs e)
{
this.OnClick(e);//响应click事件.
}
}
}
试试,自己修改修改,大致就是这么个思想,建议用Picturebox简单
changmengmeng 2009-02-08
  • 打赏
  • 举报
回复
1、用PIctuerBox也行

2、用VB写的代码: RoundRectFrame Picture1, 30, 30

'创建圆角矩形
Public Sub RoundRectFrame(objEllipse As Object, Optional ByVal x3 As Long = 8 _
, Optional ByVal y3 As Long = 8)
Dim lngW As Long, lngH As Long
Dim hWndRet As Long

lngW = objEllipse.Width \ Screen.TwipsPerPixelX
lngH = objEllipse.Height \ Screen.TwipsPerPixelY

hWndRet = CreateRoundRectRgn(0, 0, lngW, lngH, x3, y3)
Call SetWindowRgn(objEllipse.hWnd, hWndRet, True)

End Sub
3、用c#代码做的
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;


namespace WindowsApplication1
{
/// <summary>
/// Summary description for cilpButton.
/// </summary>
public class cilpButton : System.Windows.Forms.Button
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(0,0, this.Width, this.Height);
this.Region = new Region(path);
}

protected override void OnMouseEnter(EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawEllipse(new Pen(Color.Red), 0, 0 , this.Width, this.Height);
g.Dispose();
}
protected override void OnMouseLeave(EventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawEllipse(new Pen(this.BackColor), 0, 0 , this.Width, this.Height);
g.Dispose();
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
Pen pen = new Pen(this.BackColor);
Graphics g = this.CreateGraphics();
g.Clear(Color.Goldenrod);
g.FillEllipse(Brushes.DarkKhaki, new Rectangle(0,0, this.Width, this.Height));
}

protected override void OnMouseDown(MouseEventArgs e)
{
OnPaintBackground(null);
}
protected override void OnMouseUp(MouseEventArgs e)
{
this.OnClick(e);//响应click事件.
}
}
}
试试,自己修改修改,大致就是这么个思想,建议用Picturebox简单
gboxcc 2009-02-08
  • 打赏
  • 举报
回复
用PictureBox是最简单的,想自己画也行,下面是一个RoundButton的例子:

http://www.codeproject.com/KB/buttons/RoundButton_csharp.aspx
fenglaijun 2009-02-08
  • 打赏
  • 举报
回复
自己画。。。。
或者用PictureBox
SAP辉哥 2009-02-08
  • 打赏
  • 举报
回复
WPF 技术很容易实现,不过.NET3.0一下版本不可以,5L的方法是最好的,不过需要点GDI方面的知识
wuyq11 2009-02-08
  • 打赏
  • 举报
回复
GraphicsPath oPath = new GraphicsPath();
oPath.AddEllipse(this.ClientRectangle);
重绘按钮
http://www.codeproject.com/KB/buttons/TransButtonNetDemo.aspx
happychou 2009-02-08
  • 打赏
  • 举报
回复
5楼的正解。。。。
wzuomin 2009-02-08
  • 打赏
  • 举报
回复
改变控件的 Region 就可以了
周公 2009-02-08
  • 打赏
  • 举报
回复
重写Button的Paint方法就行了。

//下面的方法是重新写了Button的Paint方法,将会绘制一个圆角按钮
private void roundButton_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{

System.Drawing.Drawing2D.GraphicsPath buttonPath =
new System.Drawing.Drawing2D.GraphicsPath();

// Set a new rectangle to the same size as the button's
// ClientRectangle property.
System.Drawing.Rectangle newRectangle = roundButton.ClientRectangle;

// Decrease the size of the rectangle.
newRectangle.Inflate(-10, -10);

// Draw the button's border.
e.Graphics.DrawEllipse(System.Drawing.Pens.Black, newRectangle);

// Increase the size of the rectangle to include the border.
newRectangle.Inflate( 1, 1);

// Create a circle within the new rectangle.
buttonPath.AddEllipse(newRectangle);

//设置按钮的Region.
roundButton.Region = new System.Drawing.Region(buttonPath);

}

110,580

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧