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