Winform C # custom style in the title bar

Source: Internet
Author: User

# Region change the window style

# If! Debug

[Dllimport ("user32.dll")]
Private Static extern intptr getwindowdc (intptr hwnd );

[Dllimport ("user32.dll")]
Private Static extern int releasedc (intptr hwnd, intptr HDC );

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

Processing color # region processing color

Int ibox = 1;

If (this. maximizebox) ibox ++;
If (this. minimizebox) ibox ++;

// Obtain the background image.
Image imgpm = image. fromfile (application. startuppath + @ "main.bmp ");



// Gs. drawimage (imgbg, 3, 0, this. Width-(ibox * iboxwidh), systeminformation. captionheight + 2); // display the background image

// Display the background color in the title bar
Int xpos = This. Width-iboxwidh * ibox-2-ibox * 3;

Rectangle excluderect;
// The position in the title bar that does not need to be filled
For (INT I = 0; I <ibox; I ++)
...{
If (I> 0)
...{
Xpos = xpos + iboxwidh + 2;
}

Excluderect = new rectangle (xpos, 5, iboxwidh, iboxwidh + 1 );
GS. excludeclip (excluderect );
}


// Background area
Rectangle rbackground = new rectangle (0, 0, this. Width, systeminformation. captionheight + 3 );
// Gradient of the background color
System. Drawing. drawing2d. lineargradientbrush bbackground
= New system. Drawing. drawing2d. lineargradientbrush (rbackground, color. fromargb (100,145,175), color. fromargb (120,), lineargradientmode. Vertical );

// Fill the background color in the title bar
GS. fillrectangle (bbackground, rbackground );
// Display the icon file
GS. drawimage (imgpm );

# Endregion

Stringformat strfmt = new stringformat ();

// Strfmt. Alignment = stringalignment. Center;
// Strfmt. linealignment = stringalignment. Center;

// Gs. drawstring (this. text, this. Font, brushes. blchedalmond, m_rect, strfmt );
// Set the title Font
Font drawfont = new font ("", 10, system. Drawing. fontstyle. Bold );
// Set the title Color
Solidbrush drawbrush = new solidbrush (color. White );

// Re-draw the title
GS. drawstring (this. Text, drawfont, drawbrush, 30, 8 );

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;

}

}

}

# Endif

# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.