Extended vertical label control in C #. net

Source: Internet
Author: User

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 isOverrideForOnpaintEvent.

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 haveIfCondition 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 valueTextdrawmodeIsBottomup, You will notice thatTranslatetransformAccepts 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 valueTextdrawmodeIsTopbottom, You will see thatTranslatetransformAccepts 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.

TheTextdrawmodeProperty 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_ TransparentbgVariable which gets its value from a public boolean propertyTransparentbackground. If this is setTrue, Notice thatBrushColor is setColor. Empty; Otherwise, it uses the control's assignedColor.

Also, I made the modifications on the constructor ofVerticallabelControl 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 winformsLabelControl. 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)

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.