When dragging a small window program, I encountered a problem. How can I draw the title bar and get it out in just half a day. Similar to VC, but in. net, HDC In the API needs to be converted to System. Drawing. Graphics. FromHdc () through Graphics.
The program running result is as follows:
The Code is as follows:
/// <Summary>
/// Redraw and set the title bar of the window
/// </Summary>
/// <Param name = "hwnd"> </param>
/// <Returns> </returns>
[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 ();
// Rectangular area of the title bar button
Rectangle m_rect = new Rectangle (3,4, 194,18 );
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
{
// Process the OnPaint event
IntPtr hDC = GetWindowDC (m. HWnd );
// Convert DC to. NET Graphics to easily use the drawing function provided by the Framework.
Graphics gs = Graphics. FromHdc (hDC );
Gs. FillRectangle (new LinearGradientBrush (m_rect, Color. Green, Color. Green, LinearGradientMode. BackwardDiagonal), m_rect );
StringFormat strFmt = new StringFormat ();
StrFmt. Alignment = StringAlignment. Center;
StrFmt. LineAlignment = StringAlignment. Center;
Gs. DrawString ("title bar redraw example", this. Font, Brushes. blchedalmond, m_rect, strFmt );
Gs. Dispose ();
// Release the GDI resource
ReleaseDC (m. HWnd, hDC );
Break;
}
Case 0xA1: // WM_NCLBUTTONDOWN
{
// Process the MouseDown event
Point mousePoint = new Point (int) m. LParam );
MousePoint. Offset (-this. Left,-this. Top );
If (m_rect.Contains (mousePoint ))
{
MessageBox. Show ("this is an example of redrawing the title bar! ");
}
Break;
}
Case 0x00A0: // WM_NCMOUSEMOVE
{
// Process the MouseMove event
Point mousePoint = new Point (int) m. LParam );
MousePoint. Offset (-this. Left,-this. Top );
If (m_rect.Contains (mousePoint ))
{
Cursor. Current = Cursors. SizeAll;
}
Break;
}
}
}
Main Event reference table:
In winuser. h, the definition is as follows:
# Define WM_NCMOUSEMOVE 0x00A0
# Define WM_NCLBUTTONDOWN 0x00A1
# Define WM_NCLBUTTONUP 0x00A2
# Define WM_NCLBUTTONDBLCLK 0x00A3
# Define WM_NCRBUTTONDOWN 0x00A4
# Define WM_NCRBUTTONUP 0x00A5
# Define WM_NCRBUTTONDBLCLK 0x00A6
# Define WM_NCMBUTTONDOWN 0x00A7
# Define WM_NCMBUTTONUP 0x00A8
# Define WM_NCMBUTTONDBLCLK 0x00A9
Source: http://www.cnblogs.com/hanyonglu/archive/2011/04/12/2014228.html