Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Create rounded button

AddEllipse
working

I need AddRectang

Some solution?

What I have tried:

GraphicsPath grPath = new GraphicsPath();
grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
this.Region = new System.Drawing.Region(grPath);
base.OnPaint(e);
Posted
Updated 8-Apr-18 22:34pm

GraphicsPath class provides several Methods (System.Drawing.Drawing2D)[^]. One of them is GraphicsPath.AddRectangle Method (Rectangle) (System.Drawing.Drawing2D)[^]. At the bottom of destination page, you'll find an example.

If you would like to create button with custom shape, you have to create custom class wich inherits from control. See:
Mick Doherty's .net Button Tips[^]
How to: Create a Shaped Windows Form | Microsoft Docs[^]
Walkthrough: Creating a Windows Forms Control That Takes Advantage of Visual Studio Design-Time Features | Microsoft Docs[^]
Introduction to the Line and Shape Controls (Visual Studio) | Microsoft Docs[^]
Code Magazine: Shaped .NET Windows Forms[^]

Another way is to override OnPaint method for your control.

C#
public class TriangleButton :Button
{
    protected override void OnPaint(PaintEventArgs pevent)
    {
        Rectangle rect = this.ClientRectangle;
        using (GraphicsPath path = new GraphicsPath())
        {
			// draws triangle: /_\
			path.AddPolygon(new Point []{new Point(rect.X+rect.Width/2,rect.Y),new Point(rect.X,rect.Y+rect.Height),new Point(rect.X+rect.Width,rect.Y+rect.Height)});
			path.CloseFigure();
            this.Region = new Region(path);
            pevent.Graphics.FillPath(SystemBrushes.ButtonShadow, path); 
        }
    }
}


usage:
C#
TriangleButton shb = new TriangleButton();
shb.Name = "ShapedButton1";
shb.Location = new Point(50, 50);
shb.Parent = this; //form


Good luck!
 
Share this answer
 
v4
Comments
Goran Bibic 9-Apr-18 3:57am    
Its work...corner radius?


GraphicsPath grPath = new GraphicsPath();

Rectangle rect = new Rectangle(0, 0, 128,128);
grPath.AddRectangle(rect);
this.Region = new System.Drawing.Region(grPath);
base.OnPaint(e);
Goran Bibic 9-Apr-18 7:07am    
How to draw rectangle...no triangle?
Maciej Los 9-Apr-18 7:09am    
Think of it. A button is a rectangle...
Goran Bibic 9-Apr-18 7:17am    
Code work...but button is TRIANGLE, I need Rectangle...

Your code line 6


// draws triangle: /_\
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900