Resolved Click through

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,

I intend to provide a (new version of a) form to show the state of the keyboard, when the Windows taskbar is not visible : caps lock, num lock, ins, scroll lock.

It will have an opacity adjustable with a slider.

When the user clicks on it, I should like the click to be transmitted to the element that is visible through the form (except, for instance, if the shift key is down). It can be either a web site or a document in a text editor. Perhaps the slider could be accessible to the click, and thus be an exception to the "click through" rule.

I saw some years ago that this exists, but I do not have a too precise idea of how to implement that.
 
Solution
Anyway, here's a quick simple way for you to play with the TransparencyKey, and see how it acts with a button on the form:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace WinForms
{
    class MainForm : Form
    {
        MainForm()
        {
            Text = "Main";
            Size = new Size(800, 600);
            BackColor = Color.Green;
            TransparencyKey = Color.Green;

            var button = new Button() { Text = "Press me!", Dock = DockStyle.Left, BackColor = Color.Red };
            button.Click += (o, e) => MessageBox.Show("Got it.");
            Controls.Add(button);

            CenterToScreen();
        }

        [STAThread]...
The old implementation I had seen in the past didn't actually use Windows controls. They did their own custom painting on top of the current screen, and they just monitored the mouse position. If the mouse position happened to be over the "controls" that they cared about, then they simulated the appropriate actions.

Anyway, for WinForms, using regions for non-rectangular forms may get you started. I'm not quite sure how you'll pull off the non-clickable controls except for the slider, though:

Also, the transparency key may or may not help.
 
So, they divide the screen into regions, according to what they have to put on them. But I missed the point where they took note of what was on the screen before the application.

Oh, an idea. When I had to take care of the cursor, and the message appeared in front of the main form: I hide the main form, and that let the user read.
So, an idea can be to hide the application, and then repeat the mouse click, and show the application again.

Before advancing on this, I tried another approach: when the mouse cursor comes on the form, the form goes the other side of the screen. It is less effective than the click through because of the ocular tire, but it avoids masking something and preventing from clicking on it –which sometimes leads to close the application, and not remember whether the keyboard is in capitals.
 
Last edited:
So, you think having a transparent form will be enough to click on what is behind?
Being transparent when the mouse is on it will avoid the form to receive the clicks?
I said hide the form, setting it transparent can be a variant.
 
When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently. Any mouse actions, such as the click of the mouse, that are performed on the transparent areas of the form will be transferred to the windows below the transparent area. For example, if the client region of a form is made transparent, clicking the mouse on that area would send the event notification of the click to any window that is below it.
From:
 
Oh, I missed this precision.
You say the background. So, something written in the foreground does not interfere?
 
Something in the form foreground will interfere. It's the form background that will allow the click through.
 
Well, you deliver a message of two sentences, that say the opposite of each other.
This has something ... strange.
 
How was it inconsistent?

From post #2:
Also, the transparency key may or may not help.
From post #6:
When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently. Any mouse actions, such as the click of the mouse, that are performed on the transparent areas of the form will be transferred to the windows below the transparent area. ...
From post #8:
Something in the form foreground will interfere. It's the form background that will allow the click through.

I was only ambiguous on post #2, but I never said anything at that point about clicking through. In posts #6, I quoted directly from the documentation. In post #8, I was summarizing what I had previously quoted from the documentation: when you click on the foreground controls, your clicks will go to those control. When you click on the now transparent background, your clicks will go through to what is visible underneath.
 
Anyway, here's a quick simple way for you to play with the TransparencyKey, and see how it acts with a button on the form:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace WinForms
{
    class MainForm : Form
    {
        MainForm()
        {
            Text = "Main";
            Size = new Size(800, 600);
            BackColor = Color.Green;
            TransparencyKey = Color.Green;

            var button = new Button() { Text = "Press me!", Dock = DockStyle.Left, BackColor = Color.Red };
            button.Click += (o, e) => MessageBox.Show("Got it.");
            Controls.Add(button);

            CenterToScreen();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}
 
Last edited:
Solution
Something in the form foreground will interfere. It's the form background that will allow the click through.
This is what I understood as "Oh yes it will interfere ; but in fact ... No."
I was only ambiguous on post #2, but I never said anything at that point about clicking through.
That is perhaps the source of my hesitation, as this is the topic of the thread.
Anyway, here's a quick simple way for you to play with the TransparencyKey, and see how it acts with a button on the form:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace WinForms
{
    class MainForm : Form
    {
        MainForm()
        {
            Text = "Main";
            Size = new Size(800, 600);
            BackColor = Color.Green;
            TransparencyKey = Color.Green;

            var button = new Button() { Text = "Press me!", Dock = DockStyle.Left, BackColor = Color.Red };
            button.Click += (o, e) => MessageBox.Show("Got it.");
            Controls.Add(button);

            CenterToScreen();
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}
Well well, going to have a look at that. In a few days.
 
I suspect that the issue maybe when I talk about foreground and background, I am talking about a single form where the controls are in the foreground, and the client area of the form as the background. Perhaps, you maybe reading foreground as your app being on top, and background as all other apps being under your app.
 
Well, I copied your code, to see. To the program module of a WinForms Framework application, after suppressing the default Form1.
By the way, the Copy control of the forum did not work, I copied from the contextual menu, and had to suppress "Skydiver said".

It is interesting, in the fact it reduces the surface on which you cannot click on what is behind.

Some aspects must be tamed.
Imagine you see "Caps" on the screen. To get the message, the user must know exactly where to see. So, maybe the control has to be a little larger, and something must be displayed when it is off.
The title bar can help for this aspect, with the drawback of being a surface where you cannot click through. The title bar also has the interest to allow to move the application, providing the form is larger than the system buttons (reduce, enlarge, quit) that unfortunately happen to take half the screen with a system font of size 22.
For an application I have put the MaximizeBox and MinimizeBox attributes of the form to false, so that there is only the quit button, which takes more than the place normally dedicated to the three buttons. And I added a button on the form to reduce it as unfortunately if you put MinimizeBox to false, the minimize command is also disactivated in the system menu.

So, thank you for your code that helps test the concept, and sorry if introducing elements of my configuration does not clarify the discussion about the click through concept.

In a previous approach I attributed two places to the form, and when the cursor comes on one, it is displayed on the other.

Maybe, with a transparent background that allows click through, you can limit the escape of the form to the other place to when the cursor comes on one of the controls, or on the title bar. This reduces the ocular tire, and allows clicking on what is behind.

This will suppose a relatively sophisticated formula to decide according to the location of the cursor, whether the form has to be moved or not, but it should be feasible.

A so high title bar with a font that is large enough to be read is a drawback of Windows 10. I wonder whether I can act on this, but probably this is another topic.
 
Back
Top Bottom