views:

2882

answers:

3

Hi everyone - there seems to be a similar question to this on here but with the 'opposite' problem (He didn't want mouse events captured).

I have a form with a panel. The window is borderless and set to the exact size of the panel (for all intents and purposes, it is as if the panel is 'free floating'). I can set the panel's BackColor to SystemColors.Control, and then set the window's TransparencyKey to the same. This works in that it achieves the desired effect (transparency), but the panel can no longer capture mouse events (which is vital to the functionality)!

Is there another way around this, or a way to re-enable mouse capture?

I have tried overriding the OnPaintBackground and doing a noop, but this didn't achieve real transparency because it doesn't update the background after every tick (so whatever is behind the panel at the initial draw remains there regardless of whether you move the panel or otherwise update it). It did, however, allow the panel to capture mouse events.

This isn't all that troublesome at this stage in the project but I stumbled across the problem during a quick prototype and it's starting to annoy me now. If anyone has any pointers they'd be much appreciated.

A: 

I don't really have an answer for you, but I do have another (maybe a tad "hacky") way for you to accomplish what you're trying to do.

Set the Forms Opacity property to 1% (don't mess with the transparency key) and now it'll capture the events. The form will not be visible (at least on my machine at 1% I couldn't see it at all) and you'll still be able to capture all mouse clicks.

BFree
Hi BFree - thanks for replying but unfortunately this isn't a viable option for me - as the Panel has child controls which are required to be visible.Thanks anyway!
TomFromThePool
Yea, I was afraid you'd say that. Sorry! Good luck... I'm actually kind of curious myself if there's a solution.
BFree
+1  A: 

If you were using VC++ I would say that you needed a message pump to process WM_ mouse event messages.

A quick search reveals this thread which may be of assistance to you:

Capturing ALL mouse events

I expect that you've already tried using the following:

/// <summary>
/// A transparent control.
/// </summary>
public class TransparentPanel : Panel
{
    public TransparentPanel()
    {
    }

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

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do not paint background.
    }
}
ChrisBD
Hi ChrisBD - thanks for your help. The above method 'kind of' worked. I will keep experimenting with this and see if I can get it up to scratch.Thanks again!
TomFromThePool
A: 

Did you ever get a solution to this. I want to do something similar where I have a full screen transparent window that I can draw on top of using the mouse. I want to be able to illustrate windows and take snapshots afterwards for teaching purposes. I know you can get hold of the desktop window and draw on top of it but I'm not sure how to capture all mouse events for all windows. There is an application called smoothboard that does this but I am not sure how. I know they have used c# to write their software so I will ask them and if they give me an answer I will post it.

Hi Gary - in the end I just decided to not implement this, so I can't help you out. Sorry!
TomFromThePool