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?
-
what do you mean an app with gdi drawing? could you show some code? – Slashy Aug 25 '15 at 16:46
-
For inspiration, see the answer to [this question](http://stackoverflow.com/questions/2842667/how-to-create-a-semi-transparent-window-in-wpf-that-allows-mouse-events-to-pass). – 500 - Internal Server Error Aug 25 '15 at 16:47
2 Answers
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;

- 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
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);
}

- 1
- 1

- 922,412
- 146
- 1,693
- 2,536