'创建圆角矩形 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
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); }
'创建圆角矩形 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
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); }
重写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);}
改变控件的 Region 就可以了
GraphicsPath oPath = new GraphicsPath(); oPath.AddEllipse(this.ClientRectangle); 重绘按钮 http://www.codeproject.com/KB/buttons/TransButtonNetDemo.aspx
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简单
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简单
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);}
oPath.AddEllipse(this.ClientRectangle);
重绘按钮
http://www.codeproject.com/KB/buttons/TransButtonNetDemo.aspx