0

I was trying to make a normal rounding. And I can't smooth out the rounding itself for the button. And the second question, how do I round off the button so that the photo that is used in BackgroundImage doesn't get rounded off???

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Test_Project.SupportClass
{
    public class Buttom_Class4 : Button
    {
        public Buttom_Class4()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
        }


        private int radius = 20;
        [DefaultValue(20)]
        public int Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                this.RecreateRegion();
            }
        }
        private GraphicsPath GetRoundRectagle(Rectangle bounds, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(bounds.X, bounds.Y, radius, radius, 180, 90);
            path.AddArc(bounds.X + bounds.Width - radius, bounds.Y, radius, radius, 270, 90);
            path.AddArc(bounds.X + bounds.Width - radius, bounds.Y + bounds.Height - radius,
                        radius, radius, 0, 90);
            path.AddArc(bounds.X, bounds.Y + bounds.Height - radius, radius, radius, 90, 90);
            path.CloseAllFigures();
            return path;
        }
        private void RecreateRegion()
        {
            var bounds = ClientRectangle;
            bounds.Width--; bounds.Height--;
            using (var path = GetRoundRectagle(bounds, this.Radius))
                this.Region = new Region(path);
            this.Invalidate();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.RecreateRegion();
        }
    }
}
  • Where is the `OnPaint` part? The contents won't reposition/resize themselves by just changing the control's Region. [This](https://stackoverflow.com/q/28486521/10216583) could help. –  Jul 13 '20 at 16:58
  • @JQSOFT my code already works, but, there was one BUT, it does not smooth out the corners – Сергей Маерович Jul 13 '20 at 17:34
  • I see. Then set in the constructor the `FlatStyle = FlatStyle.Flat;` and `FlatAppearance.BorderSize = 0;`. See if that is acceptable for you. If you still need to draw a border, then you need to override the `OnPaint` method to draw it. –  Jul 13 '20 at 18:14
  • You need also to override the `ShowFocusCues` property to return `false`. Just: `protected override bool ShowFocusCues => false;` –  Jul 13 '20 at 18:33
  • You're only defining a Region. Regions are not anti-aliased, so that's the result you get. If you want smooth rounded corners, you need to draw the Path inside the Control's bounds, so anti-aliasing can be applied when you call `Graphics.DrawPath()` or `Graphics.FillPath()`. Set the `Graphics.SmoothingMode` to different values to see the difference. Don't forget to count the size of the Pen used to draw the Border. – Jimi Jul 14 '20 at 00:24
  • The method shown here: [How to avoid visual artifacts of colored border of zoomable UserControl with rounded corners?](https://stackoverflow.com/a/54794097/7444103) is a combination of a Region and a GraphicsPath. The Region defines Bounds that are generated by a GraphicsPath, but the Path is then used to draw the Border, all rendered inside the Path bounds, so anti-aliasing is applied *inside* the bounds and is not lost. Note that the Graphics object draws the contour of shapes in a slightly different way when a GraphicsPath is used. Se the Docs about it. – Jimi Jul 14 '20 at 00:33
  • @Jimi What about the second question? I plan to add a photo to the button, BUT, an important condition, when rounding this button, the image should not suffer, can this be implemented? – Сергей Маерович Jul 14 '20 at 16:14
  • @JQSOFT What about the second question? I plan to add a photo to the button, BUT, an important condition, when rounding this button, the image should not suffer, can this be implemented? – Сергей Маерович Jul 14 '20 at 16:14
  • I don't know what you mean. Have you tried my suggestion? If the result is not good enough for you then you should follow Jimi's comments. Draw everything and apply the required smoothing. –  Jul 14 '20 at 16:36
  • @JQSOFT Guys, after testing my code, I realized that I was not looking for the solution that is indicated above. What I want to achieve is this: I add a button to the form. I add an image to the button, this image is actually a button. Next, I want to round off the button, BUT so that the image itself does not suffer. When you hover over the aka photo button instead of it, the background, which I actually rounded off so that the corners did not get out, changed the background. – Сергей Маерович Jul 14 '20 at 16:38
  • I think a screenshot of what you are trying to achieve will help. Just to show, I get this but I need that. –  Jul 14 '20 at 16:47
  • If you derive your Custom Control from Button, you accept the default rendering procedure. A Button doesn't know anything about your Regions and Clipping, so it renders it to the default squared surface. If you want to control these features, you have to draw the Image yourself, possibly deriving from Control directly, instead of a .Net class that has hard-coded behaviors that may/will conflict with yours. – Jimi Jul 14 '20 at 18:23

0 Answers0