Rectangle rect=this.button1.ClientRectangle; System.Drawing.Drawing2D.GraphicsPath gPath=new System.Drawing.Drawing2D.GraphicsPath(); int r=15; gPath.AddLine(rect.Left+r,rect.Top,rect.Right-r,rect.Top); gPath.AddArc(rect.Right-2*r,rect.Top,2*r,2*r,270,90); gPath.AddLine(rect.Right,rect.Top+r,rect.Right,rect.Bottom-r); gPath.AddArc(rect.Right-2*r,rect.Bottom-2*r,2*r,2*r,0,90); gPath.AddLine(rect.Right-r,rect.Bottom,rect.Left+r,rect.Bottom); gPath.AddArc(rect.Left,rect.Bottom-2*r,2*r,2*r,90,90); gPath.AddLine(rect.Left,rect.Bottom-r,rect.Left,rect.Top+r); gPath.AddArc(rect.Left,rect.Top,2*r,2*r,180,90); Region reg=new Region(gPath); this.button1.Region=reg;
private void button1_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 = button1.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); // Set the button's Region property to the newly created // circle region. button1.Region = new System.Drawing.Region(buttonPath); }
网上有写自定义控件的例子
System.Drawing.Drawing2D.GraphicsPath gPath=new System.Drawing.Drawing2D.GraphicsPath();
int r=15;
gPath.AddLine(rect.Left+r,rect.Top,rect.Right-r,rect.Top);
gPath.AddArc(rect.Right-2*r,rect.Top,2*r,2*r,270,90);
gPath.AddLine(rect.Right,rect.Top+r,rect.Right,rect.Bottom-r);
gPath.AddArc(rect.Right-2*r,rect.Bottom-2*r,2*r,2*r,0,90);
gPath.AddLine(rect.Right-r,rect.Bottom,rect.Left+r,rect.Bottom);
gPath.AddArc(rect.Left,rect.Bottom-2*r,2*r,2*r,90,90);
gPath.AddLine(rect.Left,rect.Bottom-r,rect.Left,rect.Top+r);
gPath.AddArc(rect.Left,rect.Top,2*r,2*r,180,90);
Region reg=new Region(gPath);
this.button1.Region=reg;
{
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 = button1.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);
// Set the button's Region property to the newly created
// circle region.
button1.Region = new System.Drawing.Region(buttonPath);
}