title: Transparent Overlapping Circular Progress Bars (Custom Control)
body: <p>I am having some trouble with a custom circular progress bar control. I am trying to overlap the two of them at the lower right corner. It has a transparent background, which obviously in WinForms is showing the background, but has no effect on each other.</p>
  
  <p>Here is what I am seeing:</p>
  
  <p><a href="https://i.stack.imgur.com/piWa3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/piWa3.png" alt="Overlapping Circular Progress Bars"></a></p>
  
  <p>I have been researching on stackoverflow, and have found a few answers to people having this issue with custom picturebox controls. Most of the solutions, seem to have no effect on the circular progress bar control. Some of the solutions I have tried is.</p>
  
  <pre><code>    protected override CreateParams CreateParams
      {
          get
          {
              CreateParams cp = base.CreateParams;
  
              cp.ExStyle |= 0x20;
  
              return cp;
          }
      }
  </code></pre>
  
  <p>I also have the code on the custom control for allowing transparent backgrounds. Obviously, as I stated, this does not effect overlapping controls.</p>
  
  <pre><code>SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  </code></pre>
  
  <p>There is also a TransparentControl solution on stackoverflow which I saw people using. I have created the control, but either have no idea how to use it, or it doesn't work in my situation. Here is the code from that control.</p>
  
  <pre><code>public class TransparentControl : Panel
  {
      public bool drag = false;
      public bool enab = false;
      private int m_opacity = 100;
  
      private int alpha;
      public TransparentControl()
      {
          SetStyle(ControlStyles.SupportsTransparentBackColor, true);
          SetStyle(ControlStyles.Opaque, true);
          this.BackColor = Color.Transparent;
      }
  
      public int Opacity
      {
          get
          {
              if (m_opacity &gt; 100)
              {
                  m_opacity = 100;
              }
              else if (m_opacity &lt; 1)
              {
                  m_opacity = 1;
              }
              return this.m_opacity;
          }
          set
          {
              this.m_opacity = value;
              if (this.Parent != null)
              {
                  Parent.Invalidate(this.Bounds, true);
              }
          }
      }
  
      protected override CreateParams CreateParams
      {
          get
          {
              CreateParams cp = base.CreateParams;
              cp.ExStyle = cp.ExStyle | 0x20;
              return cp;
          }
      }
  
      protected override void OnPaint(PaintEventArgs e)
      {
          Graphics g = e.Graphics;
          Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
  
          Color frmColor = this.Parent.BackColor;
          Brush bckColor = default(Brush);
  
          alpha = (m_opacity * 255) / 100;
  
          if (drag)
          {
              Color dragBckColor = default(Color);
  
              if (BackColor != Color.Transparent)
              {
                  int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
                  int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
                  int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
                  dragBckColor = Color.FromArgb(Rb, Gb, Bb);
              }
              else
              {
                  dragBckColor = frmColor;
              }
  
              alpha = 255;
              bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
          }
          else
          {
              bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
          }
  
          if (this.BackColor != Color.Transparent | drag)
          {
              g.FillRectangle(bckColor, bounds);
          }
  
          bckColor.Dispose();
          g.Dispose();
          base.OnPaint(e);
      }
  
      protected override void OnBackColorChanged(EventArgs e)
      {
          if (this.Parent != null)
          {
              Parent.Invalidate(this.Bounds, true);
          }
          base.OnBackColorChanged(e);
      }
  
      protected override void OnParentBackColorChanged(EventArgs e)
      {
          this.Invalidate();
          base.OnParentBackColorChanged(e);
      }
  }
  </code></pre>
  
  <p>Any assistance would be appreciated. This has been driving me nuts for hours. Thanks :)</p>
  
  <p><strong>UPDATE 1:</strong> I tried using the following code snippet from examples posted below. This yielded the same results. I still have that blank space between the circular progress bars (as seen in the picture). </p>
  
  <pre><code>                Parent.Controls.Cast&lt;Control&gt;()
                    .Where(c =&gt; Parent.Controls.GetChildIndex(c) &gt; Parent.Controls.GetChildIndex(this))
                    .Where(c =&gt; c.Bounds.IntersectsWith(this.Bounds))
                    .OrderByDescending(c =&gt; Parent.Controls.GetChildIndex(c))
                    .ToList()
                    .ForEach(c =&gt; c.DrawToBitmap(bmp, c.Bounds));
  </code></pre>
  
  <p>Still stumped. :(</p>
  
  <p><strong>UPDATE 2:</strong> I tried setting the front circularprogressbar to use the back circularprogressbar as it's parent in the FormLoad. That didn't work out either. It made them transparent to each other, but cut off any part of the top circularprogressbar that wasn't within' the boundaries of the back.</p>
  
  <pre><code>var pts = this.PointToScreen(circularprogressbar1.Location);
  pts = circularprogressbar2.PointToClient(pts);
  circularprogressbar1.Parent = circularprogressbar2;
  circularprogressbar1.Location = pts;
  </code></pre>