3

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.

2 Answers 2

2

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. } } 
Sign up to request clarification or add additional context in comments.

2 Comments

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!
same problem! please update the topic if you have found the solution!
1

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.

2 Comments

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!
Yea, I was afraid you'd say that. Sorry! Good luck... I'm actually kind of curious myself if there's a solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.