Niky savaliya
Niky savaliya

Reputation: 23

Creating smooth rounded corners in panels when try to do curve the corner of form in Winforms applications

I am working on a Windows Forms application where I have implemented code to curve the corners of the form. While the code successfully curves the form itself, it does not work for controls like panel within the form. Additionally, when a panel is added to the form, the corner curvature effect of the form also stops working as expected.

I need a solution that allows me to curve the corners of both the form and other controls (e.g. panel) consistently. The implementation should ensure that:

Code:

#region Rounded Corner

protected override void OnLoad(EventArgs e)
{
    if (!DesignMode)
    {
        drawTimer.Interval = 1000 / 60;
        drawTimer.Tick += DrawForm;
        drawTimer.Start();
    }

    base.OnLoad(e);
}

private void DrawForm(object sender, EventArgs e)
{
   using (Bitmap backImage = new Bitmap(this.Width, this.Height))
   {
      using (Graphics graphics = Graphics.FromImage(backImage))
      {
         Rectangle gradientRectangle = new Rectangle(0, 0, 
                                this.Width - 1, this.Height - 1);
          using (Brush b = new LinearGradientBrush(gradientRectangle, 
          Color.FromArgb(70, 95, 122), Color.FromArgb(70, 95, 122), 0.0f))
          {
              graphics.SmoothingMode = SmoothingMode.HighQuality;

              RoundedRectangle.FillRoundedRectangle(graphics, b, gradientRectangle, 20);

              foreach (Control ctrl in this.Controls)
              {
                  using (Bitmap bmp = new Bitmap(ctrl.Width, ctrl.Height))
                  {
                      Rectangle rect = new Rectangle(0, 0, ctrl.Width, ctrl.Height);
                      ctrl.DrawToBitmap(bmp, rect);
                      graphics.DrawImage(bmp, ctrl.Location);
                  }
              }

              PerPixelAlphaBlend.SetBitmap(backImage, Left, Top, Handle);
         }
    }
}

protected override void OnPaint(PaintEventArgs e)
{
    if (DesignMode)
    {
        Graphics graphics = e.Graphics;

        Rectangle gradientRectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

        using (Brush brush = new LinearGradientBrush(gradientRectangle, Color.FromArgb(70, 95, 122), Color.FromArgb(70, 95, 122), 0.0f))
        {
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            RoundedRectangle.FillRoundedRectangle(graphics, brush, gradientRectangle, 40);
        }
    }

    base.OnPaint(e);
}

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;

        if (!DesignMode)
        {
            cp.ExStyle |= 0x00080000;
        }
        return cp;
    }
}

Upvotes: 1

Views: 56

Answers (0)

Related Questions