Today I wanted to create a transparent Panel-Control in .NET in order to superimpose it on the WebBrowser Control in order to record click events. The goal was to code a simple macro recorder for websites. Unfortunatelly the .NET framework does not allow (in an easy way) to make a control fully transparent. So it was back to the good ol' winuser.h and the extended window styles. Namely 0x00000020L or as it is better known WS_EX_TRANSPARENT.

I left it as bare as possible (because that's all I needed).

C#:
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7.  
  8. namespace WebBrowserAutomation
  9. {
  10.     /// <summary>
  11.     /// A transparent control.
  12.     /// </summary>
  13.     public class TransparentPanel : Panel
  14.     {
  15.         public TransparentPanel()
  16.         {
  17.         }
  18.  
  19.         protected override CreateParams CreateParams
  20.         {
  21.             get
  22.             {
  23.                 CreateParams createParams = base.CreateParams;
  24.                 createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
  25.                 return createParams;
  26.             }
  27.         }
  28.  
  29.         protected override void OnPaintBackground(PaintEventArgs e)
  30.         {
  31.             // Do not paint background.
  32.         }
  33.     }
  34. }

Now you can draw images or any other control on it. You can capture mouse click events through this transparent layer before they pass through to controls below ... actually the possibilties are quite endless. :)

kick it on DotNetKicks.com