2

please tell me how to make shapes with rounded edges (Winforms.) Found this code, but it has problems. when you drag a shape, it is bad draws. And some controls inside have problems with painting too.

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

    /// <summary>
    /// radius
    /// </summary>
    public int Radius = 0;

    public AfyForm()
    {
        InitializeComponent();
        min_max_closeEvents();
        borderEvents();

    }

min_max_closeEvents(); borderEvents(); - methods for border painting

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
Udjen
  • 91
  • 1
  • 9
  • That has been answered [here](http://stackoverflow.com/a/18901565/643085) – Federico Berasategui Sep 20 '13 at 14:40
  • 1
    @HighCore that question is about something else, and the answer is completely off. – Geeky Guy Sep 20 '13 at 14:41
  • @Emanuele right on the spot. – Geeky Guy Sep 20 '13 at 14:42
  • @HansPassant I absolutely like your idea, but you would have to actually draw the whole region - and change it whenever the form is resized. This is harder than just calling a Windows API method with a parameter. However, if you provide code for that as an answer, I'll be sure to upvote it. – Geeky Guy Sep 20 '13 at 14:44
  • Hans seems to have deleted his comment. He suggested on using the [Region property](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx) to draw a mask for the form so that the corners would be rounded. More complex than calling the Windows API, but more elegant and maintainable IMO. – Geeky Guy Sep 20 '13 at 14:46

1 Answers1

1

I think if you want something special in GUI, just try moving to WPF, you would surely love it. For simple application without requiring much customization in GUI, you may use winforms but anyway moving to WPF is something we should do soon or later. Here is the code I've done before recently. The idea is simple. First we create a Region of rounded rectangle shape, then assign this region to the Region property of the form. However to Draw the border, if you create the similar GraphicsPath and draw or even fill it, the border won't look good because there is some little gap between the outermost edge and the GraphicsPath.

Here is the demo's link

enter image description here

enter image description here

enter image description here

King King
  • 61,710
  • 16
  • 105
  • 130
  • Thanks, its good solution, but i need to resize form, i need toolbar with caption.By using this method, i havent got borders with ability to resize.( – Udjen Sep 23 '13 at 13:58
  • @Udjen it's a pain to customize the full featured rounded form like as you want. You should go for `WPF`. Customizing `winforms` today is something just for fun. If we have time and want to find something funny with `winforms customizing`, we will possibly spend hours digging into it. – King King Sep 23 '13 at 14:15