none
Set the corner radius property of a button's border in the Code Behind RRS feed

Answers

  • Something like this:

    public static void DrawRoundedRectangle(this Graphics g, Color color, Rectangle rec, int radius,
                                                RoundedCorners corners)
        {
            using (var b = new SolidBrush(color))
            {
                int x = rec.X;
                int y = rec.Y;
                int diameter = radius * 2;
                var horiz = new Rectangle(x, y + radius, rec.Width, rec.Height - diameter);
                var vert = new Rectangle(x + radius, y, rec.Width - diameter, rec.Height);
    
                g.FillRectangle(b, horiz);
                g.FillRectangle(b, vert);
    
                if ((corners & RoundedCorners.TopLeft) == RoundedCorners.TopLeft)
                    g.FillEllipse(b, x, y, diameter, diameter);
                else
                    g.FillRectangle(b, x, y, diameter, diameter);
    
                if ((corners & RoundedCorners.TopRight) == RoundedCorners.TopRight)
                    g.FillEllipse(b, x + rec.Width - (diameter + 1), y, diameter, diameter);
                else
                    g.FillRectangle(b, x + rec.Width - (diameter + 1), y, diameter, diameter);
    
                if ((corners & RoundedCorners.BottomLeft) == RoundedCorners.BottomLeft)
                    g.FillEllipse(b, x, y + rec.Height - (diameter + 1), diameter, diameter);
                else
                    g.FillRectangle(b, x, y + rec.Height - (diameter + 1), diameter, diameter);
    
                if ((corners & RoundedCorners.BottomRight) == RoundedCorners.BottomRight)
                    g.FillEllipse(b, x + rec.Width - (diameter + 1), y + rec.Height - (diameter + 1), diameter, diameter);
                else
                    g.FillRectangle(b, x + rec.Width - (diameter + 1), y + rec.Height - (diameter + 1), diameter,
                                    diameter);
            }
        }
    
        public enum RoundedCorners
        {
            None = 0x00,
            TopLeft = 0x02,
            TopRight = 0x04,
            BottomLeft = 0x08,
            BottomRight = 0x10,
            All = 0x1F
        }

    Used like: 

    btnG.DrawRoundedRectangle(Color.Blue, btnBG, 10, RoundedCorners.All)

    Sunday, October 28, 2012 1:31 AM
  • Hi johnr181,

    You can use the SetWindowRgn and CreateRoundRectRgn function to redraw the button.

    Like :

    using System.Runtime.InteropServices;

    [DllImport("user32.dll")] static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw); [DllImport("gdi32.dll")] static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

      private void Form1_Load(object sender, EventArgs e)
      {
         Int32 height = button1.Height;
         Int32 width = button1.Width;
         SetWindowRgn(this.button1.Handle, CreateRoundRectRgn(1, 1, width, height, 10, 10), true);            
      } 

    More information about these two function, please refer to:

    http://msdn.microsoft.com/en-us/library/aa930600.aspx

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd183516(v=vs.85).aspx

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us


    Monday, October 29, 2012 6:14 AM
    Moderator

All replies

  • Something like this:

    public static void DrawRoundedRectangle(this Graphics g, Color color, Rectangle rec, int radius,
                                                RoundedCorners corners)
        {
            using (var b = new SolidBrush(color))
            {
                int x = rec.X;
                int y = rec.Y;
                int diameter = radius * 2;
                var horiz = new Rectangle(x, y + radius, rec.Width, rec.Height - diameter);
                var vert = new Rectangle(x + radius, y, rec.Width - diameter, rec.Height);
    
                g.FillRectangle(b, horiz);
                g.FillRectangle(b, vert);
    
                if ((corners & RoundedCorners.TopLeft) == RoundedCorners.TopLeft)
                    g.FillEllipse(b, x, y, diameter, diameter);
                else
                    g.FillRectangle(b, x, y, diameter, diameter);
    
                if ((corners & RoundedCorners.TopRight) == RoundedCorners.TopRight)
                    g.FillEllipse(b, x + rec.Width - (diameter + 1), y, diameter, diameter);
                else
                    g.FillRectangle(b, x + rec.Width - (diameter + 1), y, diameter, diameter);
    
                if ((corners & RoundedCorners.BottomLeft) == RoundedCorners.BottomLeft)
                    g.FillEllipse(b, x, y + rec.Height - (diameter + 1), diameter, diameter);
                else
                    g.FillRectangle(b, x, y + rec.Height - (diameter + 1), diameter, diameter);
    
                if ((corners & RoundedCorners.BottomRight) == RoundedCorners.BottomRight)
                    g.FillEllipse(b, x + rec.Width - (diameter + 1), y + rec.Height - (diameter + 1), diameter, diameter);
                else
                    g.FillRectangle(b, x + rec.Width - (diameter + 1), y + rec.Height - (diameter + 1), diameter,
                                    diameter);
            }
        }
    
        public enum RoundedCorners
        {
            None = 0x00,
            TopLeft = 0x02,
            TopRight = 0x04,
            BottomLeft = 0x08,
            BottomRight = 0x10,
            All = 0x1F
        }

    Used like: 

    btnG.DrawRoundedRectangle(Color.Blue, btnBG, 10, RoundedCorners.All)

    Sunday, October 28, 2012 1:31 AM
  • Hi johnr181,

    You can use the SetWindowRgn and CreateRoundRectRgn function to redraw the button.

    Like :

    using System.Runtime.InteropServices;

    [DllImport("user32.dll")] static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw); [DllImport("gdi32.dll")] static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

      private void Form1_Load(object sender, EventArgs e)
      {
         Int32 height = button1.Height;
         Int32 width = button1.Width;
         SetWindowRgn(this.button1.Handle, CreateRoundRectRgn(1, 1, width, height, 10, 10), true);            
      } 

    More information about these two function, please refer to:

    http://msdn.microsoft.com/en-us/library/aa930600.aspx

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd183516(v=vs.85).aspx

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us


    Monday, October 29, 2012 6:14 AM
    Moderator
  • WPF?

    Maybe easiest is to use your own style... maybe based on ButtonBase or whatever... Better ask again in the WPF -forum.

    Regards,

      Thorsten

    Monday, October 29, 2012 11:42 AM