Send Mouse Click to Desktop Through Transparent Form In .NET

By Robbe D. Morris

Printer Friendly Version

Robbe Morris
Robbe & Melisa Morris
  Download C# Source Code
The implementation of transparent Windows forms in .NET allows the developer to create some extremely unique desktop user interfaces.  However, one of the most irritating aspects of transparent forms for me shows itself when I try to click on a desktop icon that shows through the transparent form expecting the desired application to launch and nothing happens.  With that in mind, I've worked up this little sample showing the basics of how to send the mouse click to the desktop icon or even a file from windows explorer when the user clicks a transparent portion of the form.
There are actually two ways to accomplish having a transparent form that allows click thru to the desktop:
1.This first option requires no custom coding on your part.  Set the form's TransparencyKey to some odd ball color you'd never use anywhere on the form.  The TransparencyKey designation tells windows not to repaint anything on that form that has this color which has the end affect of punching a hole in your form.  So, all of the mouse events run right through your form as if it were never there.  Controls on the form, however, are not affected.  They will still work as normal.  If you opt to set a control's backcolor to your TransparencyKey color, they to will have a transparent background.  The main problem with this is that the control no longer reacts properly for focus, certain mouse events, and click events.  In order to get around this, you'll have to put something in the MouseMove event of each control and constantly set its backcolor to some other fixed color when over the control and back to the TransparencyKey on MouseLeave.

2.The second option is for those forms that dictate their level of transparency by using the Opacity property instead.
WinAPI.cs holds the appropriate API calls from the user32.dll in order to utilize its mouse_event method.  Notice there is a call to mouse event for mouse down and mouse up for each click desired.  Most users typically require a double click to open files or desktop icons.  You may opt to refine this sample by allowing the user to set options in your application for how this should work.  To see the sample in action, run the application (download from above) and move the partially transparent form over a desktop icon and then double click the icon.

private bool FormMouseDown=false;

 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
     if (FormMouseDown==true) { return; }
     FormMouseDown = true;
     this.Visible = false;
     this.Visible = true;   

 private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

using System;
using System.Runtime.InteropServices;

namespace WinApi
  public class Mouse
            private static extern void mouse_event(UInt32 dwFlags,UInt32 dx,UInt32 dy,UInt32 dwData,IntPtr dwExtraInfo);

            private const UInt32 MouseEventLeftDown = 0x0002;
            private const UInt32 MouseEventLeftUp = 0x0004;
            public static void SendDoubleClick()
                mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
                mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
                mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
                mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());


Robbe has been a Microsoft MVP in C# since 2004.  He is also the co-founder of which provides .NET articles, book reviews, software reviews, and software download and purchase advice.