0

I have a transparent WinForms app with GDI drawings (I use it as an overlay). The problem is that whenever I click on the GDI drawing the focus goes to the app window. How do I turn that of?

TaW
  • 53,122
  • 8
  • 69
  • 111
DR.C0de
  • 58
  • 8

2 Answers2

1

You need to use the right Color as the TransparencyKey!

Everything makes the Form clickable except Color.Fuchsia.

A not really explicable, let alone documented, 'feature', that may have started as a bug but now, hopefully forever and ever, let's us switch from clickable transparent forms, onto which we can draw and non-clickable ones through which we can interact with the background items..

// click-through:
this.BackColor = Color.Fuchsia;  
this.TransparencyKey = this.BackColor;

// clickable:
this.BackColor = Color.FromArgb(255, 147, 151, 162);  // any non-fuchsia color
this.TransparencyKey = this.BackColor;
TaW
  • 53,122
  • 8
  • 69
  • 111
  • You get me wrong... I can click trough my window but as soon as I click on a GDI object drawn in the window (e.Graphics.FillRectangle) it focuses the window. – DR.C0de Aug 25 '15 at 17:59
1

As long as you implemented the overlay correctly (an owned window displayed with the Shown(owner) overload, example) then it just takes a little scrap of copy/paste code. Windows asks you what part of the window was clicked, you can respond with "it is transparent". So it will keep looking for anybody that is interested, its parent window is next.

Like this:

    protected override void WndProc(ref Message m) {
        const int WM_NCHITTEST = 0x84;
        const int HTTRANSPARENT = -1;
        if (m.Msg == WM_NCHITTEST) m.Result = new IntPtr(HTTRANSPARENT);
        else base.WndProc(ref m);
    }
Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536