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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
HalliHax
  • 816
  • 2
  • 11
  • 26

2 Answers2

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.
    }
}
Community
  • 1
  • 1
ChrisBD
  • 9,104
  • 3
  • 22
  • 35
  • 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! – HalliHax May 20 '09 at 11:22
  • same problem! please update the topic if you have found the solution! – Pedro77 Mar 13 '12 at 17:40
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.

BFree
  • 102,548
  • 21
  • 159
  • 201
  • 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! – HalliHax May 19 '09 at 13:49
  • Yea, I was afraid you'd say that. Sorry! Good luck... I'm actually kind of curious myself if there's a solution. – BFree May 19 '09 at 13:53