Extracted WinForm control development 1 .. BorderLabel

Source: Internet
Author: User

BorderLabel, extended for common winform.

1. You can adjust the font position.

2. You can adjust the foreground and background scenery.

3. You can adjust the font position.

The key code is as follows.

Code Draw a string object on the canvas:
// First, we begin by setting the smoothing mode
// AntiAlias, to reduce image sharpening and improve our drawnings
E. Graphics. SmoothingMode = SmoothingMode. AntiAlias;

// Use the deprecated image model to increase the image quality.
Base. OnPaint (e );
This. drawningPath. Reset ();

// Second, lets use a GraphicsPath object and write our string into it.
// Our objective, however, is to measure the how much screen space our
// Drawning will occupy.
Add a string to the path
DrawningPath. AddString (this. text, this. Font. FontFamily, (int) this. Font. Style, this. Font. Size,
New Point (0, 0), StringFormat. GenericTypographic );

// Then, lets calculate the area occuped by our drawning...
This. drawningSize = GetPathSize (drawningPath );

//... And remember to sum also the future border size
This. drawningSize. Height + = this. borderSize + 5;
This. drawningSize. Width + = this. borderSize + 5;

// Lets determine then how we shoshould align our text in
// The control's area, both horizontally and vertically

// If text is Left-Aligned
If (this. textAlign = ContentAlignment. TopLeft |
This. textAlign = ContentAlignment. MiddleLeft |
This. textAlign = ContentAlignment. BottomLeft)
This. drawningPoint. X = this. Padding. Left;

// If text is Center-Aligned
Else if (this. textAlign = ContentAlignment. TopCenter |
This. textAlign = ContentAlignment. MiddleCenter |
This. textAlign = ContentAlignment. bottoffenter)
DrawningPoint. X = (this. Width-this. drawningSize. ToSize (). Width)/2;

// If text is Right-Aligned
Else drawningPoint. X = this. Width-(this. Padding. Right + drawningSize. ToSize (). Width );

// If text is Top-Aligned
If (this. textAlign = ContentAlignment. TopLeft |
This. textAlign = ContentAlignment. TopCenter |
This. textAlign = ContentAlignment. TopRight)
DrawningPoint. Y = this. Padding. Top;

// If text is Middle-Aligned
Else if (this. textAlign = ContentAlignment. MiddleLeft |
This. textAlign = ContentAlignment. MiddleCenter |
This. textAlign = ContentAlignment. MiddleRight)
DrawningPoint. Y = (this. Height-drawningSize. ToSize (). Height)/2;

// If text is Bottom-Aligned
Else drawningPoint. Y = this. Height-(this. Padding. Bottom + drawningSize. ToSize (). Height );

// After that we can reset our path so we can draw the text in the proper place now
DrawningPath. Reset ();

// Next, we start add our text into its place ,...
DrawningPath. AddString (this. text, this. Font. FontFamily, (int) this. Font. Style, this. Font. Size,
This. drawningPoint, StringFormat. GenericTypographic );
// Adjust the string alignment.
//... And then lets use our pen and draw our text to screen
E. Graphics. DrawPath (drawningPen, drawningPath );

// So lets reset our path and do it again, but now for the foreground
DrawningPath. Reset ();

// Again, we add our text into our path...
DrawningPath. AddString (this. text, this. Font. FontFamily, (int) this. Font. Style, this. Font. Size,
This. drawningPoint, StringFormat. GenericTypographic );

//... But now, we draw also the foreground
E. Graphics. FillPath (this. drawningForecolorBrush, drawningPath );
E. Graphics. DrawPath (drawningPen, drawningPath );

 

 

/Files/csharponworking/BorderLabelTest.rar

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.