This article from http://www.codeproject.com/KB/miscctrl/Vertical_Label_Control.aspx
A custom vertical label user control in C #. Net with support for transparent backgrounds.
- Download demo-17.1 KB
- Download source code-7.76 KB
Introduction
This article describes how to create a custom vertical label user control in C #. net. the user control provides text draw from top or from bottom. this article is a derivation of Raman marshal's vertical label control in VB. net. I just translated his work to C # and added the functionality of drawing text starting from bottom to top. also, an updated version now supports transparent backgrounds.
Background
On one of my projects, I needed a label control that can display text vertically. I encountered Raman marshal's vertical label control in VB. net and translated it to C #. but, I needed additional functionality of drawing text starting from the top, so I just added the functionality. this control has been useful to me, And I hope others wocould find it useful too.
Using the code
The Code provided is a class that creates a DLL that can be added as an item in the toolbox of the Windows Forms designer. The class uses the following namespaces:
Collapse | Copy code
UsingSystem;UsingSystem. componentmodel;UsingSystem. drawing;
Code
The part of the code that really does the job isOverrideForOnpaint
Event.
Collapse | Copy code
Protected Override Void Onpaint (System. Windows. Forms. painteventargs e ){ Float Vlblcontrolwidth; Float Vlblcontrolheight; Float Vlbltransformx; Float Vlbltransformy; color controlbackcolor = backcolor; pen labelborderpen; solidbrush labelbackcolorbrush; If (_ Transparentbg) {labelborderpen = New Pen (color. empty, 0 ); Labelbackcolorbrush = New Solidbrush (color. Empty );} Else {Labelborderpen = New Pen (controlbackcolor, 0 ); Labelbackcolorbrush =New Solidbrush (controlbackcolor);} solidbrush labelforecolorbrush = New Solidbrush ( Base . Forecolor ); Base . Onpaint (E); vlblcontrolwidth = This . Size. width; vlblcontrolheight = This . Size. height; E. Graphics. drawrectangle (labelborderpen, 0 , 0 , Vlblcontrolwidth, vlblcontrolheight); E. Graphics. fillrectangle (labelbackcolorbrush,0 , 0 , Vlblcontrolwidth, vlblcontrolheight); E. Graphics. textrenderinghint = This . _ Rendermode; E. Graphics. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality; If ( This . Textdrawmode = drawmode. bottomup) {vlbltransformx = 0 ; Vlbltransformy = vlblcontrolheight; E. Graphics. translatetransform (vlbltransformx, vlbltransformy); E. Graphics. rotatetransform ( 270 ); E. Graphics. drawstring (labeltext, Font, labelforecolorbrush, 0 , 0 );} Else {Vlbltransformx = vlblcontrolwidth; vlbltransformy = vlblcontrolheight; E. Graphics. translatetransform (vlblcontrolwidth, 0 ); E. Graphics. rotatetransform ( 90 ); E. Graphics. drawstring (labeltext, Font, labelforecolorbrush, 0 , 0 , Stringformat. generictypographic );}}
As you can see, I haveIf
Condition inIf(This. Textdrawmode = drawmode. bottomup)
. This tells us where the control decides whether to draw the text from bottom up or from top to bottom depending on the value of the propertyTextdrawmode
.
When the valueTextdrawmode
IsBottomup
, You will notice thatTranslatetransform
Accepts values zero for the X component of the translation and the height of the control as value for y of the translation. this tells GDI to start drawing from the bottom left of the rectangle occupied by the control.
When the valueTextdrawmode
IsTopbottom
, You will see thatTranslatetransform
Accepts the control's width as the X component of the translation and zero as the Y component of the translation. This tells GDI to start drawing from top right of the rectangle occupied by the control.
TheTextdrawmode
Property is an additional property that can be set during design time and also during runtime.
In this update, please notice that I am checking for the value of_ Transparentbg
Variable which gets its value from a public boolean propertyTransparentbackground
. If this is setTrue
, Notice thatBrush
Color is setColor. Empty
; Otherwise, it uses the control's assignedColor
.
Also, I made the modifications on the constructor ofVerticallabel
Control to include the following line:
Collapse | Copy code
Setstyle (system. Windows. Forms. controlstyles. opaque,True);
And finally, to enable transparency, the followingOverrideWas added:
Collapse | Copy code
protected override createparams { Get {createparams CP = base . createparams; CP. exstyle | = 0x20; // turn on ws_ex_transparent return CP ;}}
On the screenshot that I have provided, the horizontally-aligned text uses a winformsLabel
Control. The three vertical labels were displayed in different orientation (Bottom-up
|Top-bottom
) And also with different transparency (Backgroundtransparent
=True
|False
) Settings.
Points of interest
Since this is my first time writing a program using GDI +, I tried to do it using trial and error, but it was frustrating at first, until I found a nice article on how to useGraphics. rotatetransform
.
History
- July 27,200 7: initial version.
- September 27: Updated with support for transparent background.
License
this article, along with any associated source code and files, is licensed under the Code project open license (cpol)