The form is divided into two parts: the client area and the non-client zone (non-client area)
The WM_PAINT message, OnPaint () method, GetDC () API functions are all processed by the client area of the form.
The title bar is in the non-client area, so the WM_PAINT message, OnPaint () method, GetDC () API functions are not used
GETWINDOWDC () is the canvas handle that gets the entire form (device context translates as: equipment manifest, which I used to call the canvas handle), including non-client areas
GDI can be drawn without a DC, because the operating system must know where you want to draw the graphics
The system redraws the form when other forms are obscured or moved away. The WM_PAINT and wm_ncpaint messages are emitted to notify the form to redraw the interface.
Received Wm_ncpaint message (non-client area draw message) stating that the non-client area is in need of redrawing
Overloaded WndProc () method (Form message processing), handling messages such as Wm_ncpaint ....
The basic step is to intercept the wm_ncpaint message, get the form's full canvas handle, and draw the graphic on the full canvas.
Wm_ncpaint wm_nccalcsize wm_ncactivate wm_nchittest
Suggested landlord search keywords: "C # wm_ncpaint WndProc Graphics" Get more reference
Tile
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[DllImport ("User32.dll")] private static extern IntPtr GETWINDOWDC (INTPTR hwnd); [DllImport ("User32.dll")] private static extern int ReleaseDC (INTPTR hwnd, INTPTR HDC); [DllImport ("Kernel32.dll")] private static extern int GetLastError (); The rectangular area of the title bar button. Rectangle m_rect = new Rectangle (205, 6, 20, 20); protected override void WndProc (ref Message m) {base. WndProc (ref m); Switch (m.msg) {case 0x86://wm_ncactivate goto case 0x85; Case 0x85://wm_ncpaint {IntPtr HDC = GETWINDOWDC (M.hwnd); Converting DC to. NET graphics makes it easy to use the graphics GS = GRAPHICS.FROMHDC (HDC) provided by the framework. Gs. FillRectangle (New LinearGradientBrush (M_rect, Color.pink, Color.purple, lineargradientmode.backwarddiagonal), m_ RECT); StringFormat strfmt = new StringFormat (); Strfmt.alignment = Stringalignment.center; Strfmt.linealignment = Stringalignment.center; Gs. DRawstring ("√", this.) Font, Brushes.blanchedalmond, M_rect, strfmt); Gs. Dispose (); Frees GDI Resources ReleaseDC (M.hwnd, HDC); Break } case 0xa1://wm_nclbuttondown {point mousepoint = new Point ((int) m.lparam); Mousepoint.offset (-this. Left,-this. TOP); if (m_rect. Contains (Mousepoint)) {MessageBox.Show ("Hello"); } break; }}}//The area of the button is updated in time when the window size changes. private void Form1_sizechanged (object sender, System.EventArgs e) {m_rect. X = this. bounds.width-95; M_rect. Y = 6; M_rect. Width = M_rect. Height = 20; } } }