Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
if (Environment.OSVersion.Version.Major < 6) { Debug.WriteLine("How about trying this on Vista?"); return false; }
// [DllImport("dwmapi.dll")]static extern void DwmIsCompositionEnabled(ref bool pfEnabled); bool isGlassSupported = false; DwmIsCompositionEnabled(ref isGlassSupported); return isGlassSupported;
// [DllImport("dwmapi.dll")]static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins); Margins marg; marg.Top = 20; // extend 20 pixels from the top marg.Left = 0; marg.Right = 0; marg.Bottom = 0; DwmExtendFrameIntoClientArea(this.Handle, ref marg);
SolidBrush blackBrush = new SolidBrush(Color.Black); g.FillRectangle(blackBrush, 0, 0, this.ClientSize.Width, marg.Top);
// make windows do the work for us by lieing to it about where the user clickedprotected override void WndProc(ref Message m){ base.WndProc(ref m); if (m.Msg == 0x84 // if this is a click && m.Result.ToInt32() == 1 // ...and it is on the client && this.IsOnGlass(m.LParam.ToInt32())) // ...and specifically in the glass area { m.Result = new IntPtr(2); // lie and say they clicked on the title bar }}private bool IsOnGlass(int lParam){ // get screen coordinates int x = (lParam << 16) >> 16; // lo order word int y = lParam >> 16; // hi order word // translate screen coordinates to client area Point p = this.PointToClient(new Point(x, y)); // work out if point clicked is on glass if (y < marg.Top) return true; return false;}
this.RecreateHandle();