How to pass mouse events to applications behind mine in C#/Vista?
Asked Answered
E

3

4

I am writing a very specialized app in C# that floats as a mostly transparent window over the entire desktop. I want to be able to create and pass mouse events to applications behind mine, and have them appear to operate "normally", responding to those events. It would also be preferable if the window manager could respond.

I am not a Windows guru, and am unsure of how to best accomplish this.

From this page: http://bytes.com/forum/thread270002.html

it would appear that mouse_event would be good, except that since my app is floating over everything else, I'm guessing my generated events would never make it to the other apps underneath.

It seems the alternative is SendMessage, but that requires a fair amount of manual manipulation of windows, and the mouse events generated aren't "authentic."

Any thoughts on the best way to approach this?

Extinguish answered 6/10, 2008 at 8:28 Comment(4)
Are you looking to /create/ the events, or just allow existing mouse clicks to pass through?Cason
I am looking to create events.Extinguish
If you're looking to create events, then SendMessage is as good as it gets. AFAIK, messages sent with SendMessage are indistinguishable from normal messages, unless you're specifically looking for it.Meander
If anybody is interested, the work coming out of this question was a system that allows for whole body input into Windows. I created mouse events based on Wii Remote press events. You can see it here: youtube.com/watch?v=7dLDVL7rtrIExtinguish
E
2

After looking at System hooks and other low level solutions I found a much simpler method.

First, set the TransparencyKey and BackColor of the form to be the same. This didn't make any visual difference to me as my form was visually transparent already, but this will help in letting mouse events through.

Second, the following code will let mouse events "fall" through your form.

protected override CreateParams CreateParams
{
    get
    {
        CreateParams createParams = base.CreateParams;
        createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT

        return createParams;
    }
}

Lastly, set TopMost to be true.

Your form will now float on top of everything visually, but will give up focus to all other apps.

Extinguish answered 6/10, 2008 at 10:18 Comment(2)
The question is what do you want to do with it - as a quick single use application this might work well, but if you want to use such a thing on more boxes this might lead to more problems - e.g. what if another topmost window exists.Kinnon
Fionn: agreed. Other topmost windows will break this. How many topmost apps can we have. However, this easiest approach without having to go low-level.Extinguish
K
2

Sounds like you want to do something like filtering user input.

Maybe you just need an keyboard/mouse hook.

You maybe want to take a look at the windows API call SetWindowsHookEx.

There should also be enough samples how to do that in C# available, the first thing i found was this link (maybe not the best article but should give you the idea).

Kinnon answered 6/10, 2008 at 8:44 Comment(1)
Note that if you do use hooks they will not work over Adminstrator windows (Powershell, VS in Admin etc.) if running in User mode.Keishakeisling
E
2

After looking at System hooks and other low level solutions I found a much simpler method.

First, set the TransparencyKey and BackColor of the form to be the same. This didn't make any visual difference to me as my form was visually transparent already, but this will help in letting mouse events through.

Second, the following code will let mouse events "fall" through your form.

protected override CreateParams CreateParams
{
    get
    {
        CreateParams createParams = base.CreateParams;
        createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT

        return createParams;
    }
}

Lastly, set TopMost to be true.

Your form will now float on top of everything visually, but will give up focus to all other apps.

Extinguish answered 6/10, 2008 at 10:18 Comment(2)
The question is what do you want to do with it - as a quick single use application this might work well, but if you want to use such a thing on more boxes this might lead to more problems - e.g. what if another topmost window exists.Kinnon
Fionn: agreed. Other topmost windows will break this. How many topmost apps can we have. However, this easiest approach without having to go low-level.Extinguish
L
1

If you disable the control and return you're able to pass the MouseDown event to the parent.

Linnette answered 17/8, 2011 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.