[SIMPLE C#] 둥근버튼(Round Button) 만들기

나만의 컨트롤 만들기 1탄! Round Button

모서리를 지정 값 만큼 둥글게 만들어서 Path를 반환 하는 함수
private GraphicsPath CreateRoundRectPath(int x, int y, int width, int height, int cornerRadius)
{
    GraphicsPath path = new GraphicsPath();
    if (cornerRadius == 0)
    {
        path.AddRectangle(new Rectangle(x, y, width, height));
    }
    else
    {
        path.AddArc(x, y, cornerRadius, cornerRadius, 180, 90); // 왼쪽 상단 모서리
        path.AddArc(x + width - cornerRadius, y, cornerRadius, cornerRadius, 270, 90);// 오른쪽 상단 모서리
        path.AddArc(x + width - cornerRadius, y + height - cornerRadius, cornerRadius, cornerRadius, 0, 90);// 오른쪽 하단 모서리
        path.AddArc(x, y + height - cornerRadius, cornerRadius, cornerRadius, 90, 90);// 왼쪽 하단 모서리
    }
    path.CloseFigure();
    return path;
}

컨트롤 제작시 기본 파라미터

public UserControl1()
{
    InitializeComponent();

    // Set Optimized Double Buffer to reduce flickering
    this.SetStyle(ControlStyles.UserPaint, true);
    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

    // Redraw when resized
    this.SetStyle(ControlStyles.ResizeRedraw, true);
    this.Resize += (s1, e1) => { this.Invalidate(); };
}

영상의 내용을 외울 필요는 없습니다. 어떤 경우에 사용을 했구나 하는 점만 기억하면 좋겠습니다

아래 소스는 영상과는 조금 다른 내용이지만 95% 비슷한 코드 입니다! 다른 점을 참고하세요

Leave a comment

* - Required fields