0

I have a strange visual error in VS 2019 with a windows forms project:

I started to remake an example project of a modern, dark UI from a YT video: https://www.youtube.com/watch?v=vYDyGxoq9JU

I wanted to have rounded corners in panels and buttons and for that I added a new class SButton:

namespace Dashboard
 {
public class SButton : Button
{
    [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]

    private static extern IntPtr CreateRoundRectRgn
(
     int nLeftRect,
     int nTopRect,
     int nRightRect,
     int nBottomRect,
     int nWidthEllipse,
     int nHeightEllipse
 );

    protected override void OnPaint(PaintEventArgs e)
    {
        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 12, 12));
    }
}
}

I did the same with a new class for panels with rounded corners. I used the new SButton in both ways: generating some just with code and some by draging them from toolbox. They work, but appear with graphic elements of the buttons on the left (which are normal buttons, made "by mouse"), and the top bar of the property window. Appearance changes when hovering over the button.

Screenshot VS 2019

The 3 controls on the right are SButton. Two of them show strange duplicated graphic elemenst from elswehere.

This behavior ist still there when compiling the app.

When I open this project on a different computer who has a slightly different version of VS 2019, the problem is the same. You can download the project (260kb) here, if You are curious.

I find this really strange and do not know how to solve it, except trying to start over from zero again.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Abtzero
  • 19
  • 4
  • If you are getting duplicate that one reason is they are getting painted twice. – jdweng Feb 10 '21 at 17:48
  • 1
    You aren't painting anything. – LarsTech Feb 10 '21 at 17:59
  • 1
    It's a bad idea to remove `base.OnPaint(e)` from the `OnPaint()` method. Move the `CreateRoundRectRgn()` call to the `OnResize()` method override (after calling `base.OnResize(e)` this time). BTW, you're setting the Region, so the external bounds are not anti-aliased. It may not look that great. – Jimi Feb 10 '21 at 18:43
  • Remember to dispose of the old `Region` value. – TnTinMn Feb 11 '21 at 02:39
  • Thanks Jimi for Your proposal. I made a public custom method `makeRoundCorners()` in the custom control class, which I call at Form1_Load. I do then the `Region = ... CreateRoundRectRgn( ...` How would I anti-alias the external bounds? @TnTinMn: Is the disposal of Region necessesary after setting it as rectangle every time? – Abtzero Feb 11 '21 at 11:36
  • When yo use call `CreateRoundRectRgn()`, you need to assign the Pointer (`IntPtr`) to a local variable, then use `var region = Region.FromHRegion(IntPtr)` to create the new Region. After that, you need to call [DeleteObject(IntPtr)](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-deleteobject) to delete the region (`IntPtr -> HRGN`) object. Don't fail on this. – Jimi Feb 11 '21 at 13:13
  • To anti-alias the Borders, you have different choices, depending on the type of Control. For a UserControl, for example: [How to avoid visual artifacts of colored border of zoomable UserControl with rounded corners?](https://stackoverflow.com/a/54794097/7444103). For a Custom Control, it's probably better to draw the *contours* directly on a transparent / translucent background, e.g., [Translucent circular Control with text](https://stackoverflow.com/a/51435842/7444103) – Jimi Feb 11 '21 at 13:20

0 Answers0