title: Winforms Transparent Panel Control - Hover, Click Through and Redraw Issues
body: <p>I have followed a number of posts here and on other sites for creating a Transparent Panel control for WinForms that has "real" transparency (i.e. it does not just inherit the background color of the container it is in).</p>
  
  <p>Here is my derived panel class so you can see how I did it:</p>
  
  <pre><code>namespace TransparencyPOC
  {
      public partial class TransPictureBox : Panel
      {
          public TransPictureBox()
          {
              InitializeComponent();
          }
  
          public TransPictureBox(IContainer container)
          {
              container.Add(this);
              InitializeComponent();
          }
  
          protected override void OnPaintBackground(PaintEventArgs e)
          {
              //do nothing
          }
  
          protected override void OnMove(EventArgs e)
          {
              RecreateHandle();
          }
  
          protected override CreateParams CreateParams
          {
              get
              {
                  CreateParams cp = base.CreateParams;
                  cp.ExStyle |= 0x20; //WS_EX_TRANSPARENT
                  return cp;
              }
          }
  
          private Image m_Image;
          public Image Image
          {
              get
              {
                  return m_Image;
              }
  
              set
              {
                  m_Image = value;
              }
          }
  
          protected override void OnPaint(PaintEventArgs e)
          {
              base.OnPaint(e);
              Graphics g = e.Graphics;
  
              Bitmap bmp = new Bitmap(m_Image);
              bmp.MakeTransparent();
  
              g.DrawImage(bmp, 0, 0, 100, 100);
              g.Dispose();
          }
  
      }
  }
  </code></pre>
  
  <p>I am using this panel control to hold a PNG with a transparent background, and need to be able to position it above other controls, such as other transparent panels, buttons, etc.</p>
  
  <p>Two issues:</p>
  
  <ul>
  <li>If I position this panel partially over a button control, I can only click the button in the areas where the panel doesn't overlap the control. Is there any way that I can make the hover and click events pass through the transparent panel so that when I hover or click anywhere over the button, even parts of the button that are obscured by the PNG that the button receives those clicks? Assume that this needs to be dynamic, i.e. I won't know what control the image is positioned above, just want it to be able to pass the hover and clicks through to whatever is beneath it.</li>
  <li>When I do hover over part of the button that is not covered by the panel, the button repaints itself above the transparent panel (see image below for before and after)</li>
  </ul>
  
  <p>Before Hovering:</p>
  
  <p><a href="https://i.stack.imgur.com/ZhsEw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZhsEw.png" alt="Before Hovering"></a></p>
  
  <p>After Hovering:</p>
  
  <p><a href="https://i.stack.imgur.com/I5kLq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I5kLq.png" alt="After Hovering"></a></p>
  
  <p>Questions:</p>
  
  <ul>
  <li>Can I allow the button to be hoverable/clickable through the transparent panel? Ideally, I would want any visible portion of the underlying button to be hoverable/clickable, but if that's not doable, then I'd want it to be clickable/hoverable as if there was nothing above it.</li>
  <li>Can I prevent the button from being redrawn over the panel when I do hover over it, and/or trigger a repaint of the panel when this happens?</li>
  </ul>
  
  <p>Thank you in advance!</p>