Add a button to the title bar of the window
The principle is the same. They all rewrite the Window Process (wndproc) and process some non-customer messages (wm_ncxxxx). It can be said that there was no new idea, but in the process of writing this program, I also learned two skills:
1) in C #, you do not need to call the setwindowlong API to rewrite the window. You can overide A wndproc directly.
2) In Windows API, HDC can be converted to (created) system. Drawing. Graphics through graphics. fromhdc (), and then. NET Framework (GID +?) can be used ??) The drawing function is provided for easy drawing. Finally, we can discard the nasty gdi api (to be honest, Calling Windows API in C # is really too troublesome :)).
The Code is as follows:
Using system;
Using system. drawing;
Using system. Drawing. drawing2d;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;
Using system. diagnostics;
Namespace windowsapplication2
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,266 );
This. Name = "form1 ";
This. Text = "form1 ";
This. sizechanged + = new system. eventhandler (this. form1_sizechanged );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
[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 );
// 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. 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 ();
// Release the GDI resource
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;
}
}
}
// Update the button area 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;
}
}
}