Windows form with a transparent background that can be clicked through
Asked Answered
F

2

10

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:

Francenefrances answered 12/1, 2017 at 22:25 Comment(6)
Is this what you are looking for?Teratogenic
Or this one? (Read the notes part in the answer to enable or disable click-through)Teratogenic
@Reza Aghaei Thanks a lot,please feel free to publish a answer to give you reputation.The way to go is by overriding CreateParams to set those extended styles plus setting the form opacity level in the range of 1% to 99% opaccity (0.01R to 0.99R);I discovered that using this methodology the form can't be 100% opaque,if you let the form at the default value (I mean 100% opaque),the form will be shown transparent... literally, everything will be totally transparent so the form and its controls will totally dissapear, however it will work perfect at 99% opaccity ...its a weird thing.Francenefrances
How about making your form shorter.Ambience
@Francenefrances Since I'm not sure about your requirement, I prefer to keep just a comment. Feel free to post an answer based on other links which I shared :)Teratogenic
Or you can make use of the form's TransparencyKey property.Divert
C
1

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.

Costanza answered 18/9, 2019 at 21:47 Comment(0)
M
0

Check the RAD designer in Visual Studio.

  • Is the label docked to fill?
  • Where is the main form clickable?

The transparent color is click-though in the main parent, however, components will still retain clicks.

Malmsey answered 29/4, 2019 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.