Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows form with a transparent background that can be clicked through

INTRODUCTION

Using C# or VB.NET. I'm trying to make a form's background transparent; this form will be overlaped to other window, it will be a top-most window, so the transparent form (and its controls) must have the ability that they must not receive focus and they must can be clicked trough, this means if for example I perform a left-click on the transparent background, then the window on background of that (in the Z-order window) is the window that must receive the click instead.

Notes:

For avoiding the focus I'm overriding the CreateParams property as explained here.

For making my form transparent, I'm calling Win32 DwmExtendFrameIntoClientArea function and also using SharpDX library as explained here. But I think this really doesn't matter with the question itself.

PROBLEM

I'll show a demostration of what I mean using images. Here below is a image of a form (with no transparency, just to simplify understanding) overlapped to a window of a text editor program; note that my form doesn't receive focus. Well, the problem is when I do click on the form's background (or one of its controls) the window on background (the text editor window) still have focus but it can't receive the click.

enter image description here

Here is the same image of above but with a transparent form:

enter image description here

RESEARCH

I'm not really sure about what to investigate, so I'm going blind trying to find something useful in a trial-and-error stage by overriding the Window procedure (WndProc) of the transparent form to test related windows messages, like WM_NCHITEST or WM_MOUSEACTIVATE message as said here:

  • Windows form with a transparent background that cannot be clicked through

  • Make a form not focusable in C#

  • How do I create an "unfocusable" form in C#?

like image 548
ElektroStudios Avatar asked Jan 12 '17 22:01

ElektroStudios


1 Answers

You can do this by sending click (mouse up & mouse down) messages to the window underneath the transparent window using WinAPI.

PostMessageA

You'll need to find the window underneath the point you require.

WindowFromPoint

You'll have to translate the position of the click events accordingly since messages are processed based on relative window position, not absolute screen position.

I actually did this quite successfully to automatically play a facebook game many years ago.

like image 138
Mike Avatar answered Nov 14 '22 23:11

Mike