Click here to Skip to main content
Licence CPOL
First Posted 26 Jul 2007
Views 73,067
Downloads 2,183
Bookmarked 65 times

Extended Vertical Label Control in C# .NET

By | 26 Sep 2007 | Article
A custom vertical label user control in C#.NET with support for transparent backgrounds.

Screenshot - trans_vertical.png

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 Tayal'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 Tayal'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 would 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:

using System;
using System.ComponentModel;
using System.Drawing;

Code

The part of the code that really does the job is the override for the OnPaint event.

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 have an if condition in if (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 property TextDrawMode.

When the value of TextDrawMode is BottomUp, you will notice that TranslateTransform 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 value of TextDrawMode is TopBottom, you will see that TranslateTransform 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.

The TextDrawMode 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 the _transparentBG variable which gets its value from a public boolean property TransparentBackground. If this is set to true, notice that the Brush color is set to Color.Empty; otherwise, it uses the control's assigned Color.

Also, I made the modifications on the constructor of the VerticalLabel control to include the following line:

SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);

And finally, to enable transparency, the following override was added:

protected override CreateParams 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 WinForms Label 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 use Graphics.RotateTransform.

History

  • July 27, 2007: 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)

About the Author

/randz

Software Developer (Senior)
Headstrong
Philippines Philippines

Member

Follow on Twitter Follow on Twitter
I am working as as a full-time Software Developer in Makati City, Philippines.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberpcs04143:58 25 Apr '12  
Generalvery thanks Pinmembertalinhu19:25 23 Nov '09  
GeneralThis is copied from another source PinmemberMonkeyCat4:41 28 Jan '09  
GeneralRe: This is copied from another source Pinmember/randz20:49 14 Mar '10  
NewsI have posted previously an article on this subject!!! PinmemberHoria Tudosie3:58 6 Nov '08  
GeneralRe: I have posted previously an article on this subject!!! PinmvpJohn Simmons / outlaw programmer15:24 19 Nov '08  
GeneralThanks PinmemberSpikeAustin10:20 20 Aug '08  
GeneralRe: Thanks Pinmember/randz0:40 21 Aug '08  
QuestionLabel at Run-time PinmemberAlexB473:32 16 Nov '07  
AnswerRe: Label at Run-time Pinmember/randz15:03 18 Nov '07  
QuestionVertical control does not clear old text before writing new one Pinmemberdmcyu6:12 30 Oct '07  
AnswerRe: Vertical control does not clear old text before writing new one Pinmember/randz18:52 9 Nov '07  
AnswerRe: Vertical control does not clear old text before writing new one Pinmemberdmcyu4:24 11 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmember/randz13:43 13 Nov '07  
Hi David,
 
Thank you for your reply. I was able to reproduce your scenario just now. So silly of me to test only on the third vertical label. Smile | :)
 
I think, but I am not sure, the problem was due to the enabling of WS_EX_TRANSPARENT property of the control. Microsoft says that windows does not directly support transparent windows. More on information can be found in this KB92526[^] Article. Also you can find some details regarding the WS_EX_TRANSPARENT in this article[^] from Microsoft.
 
This is going to be a problem when we are going to update the text value of the control during runtime. One workaround that I see is to create the vertical label control at runtime and add it to the form programmatically. I am still looking for possible solution on this problem.
 
It will not be a problem when the background of the control is not transparent.
 
Remember, your work is not yours alone. Somewhere, there are some codes written by others amongst us that depends on your work. By failing to see that you are part of their ecosystem, you are bound to break their code.

GeneralRe: Vertical control does not clear old text before writing new one Pinmemberdmcyu18:47 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmember/randz19:28 13 Nov '07  
GeneralRe: Vertical control does not clear old text before writing new one Pinmemberm0nk3yi3unz18:38 5 Oct '08  
GeneralTransparent Background PinmemberBenjaminHelbig21:28 25 Sep '07  
AnswerRe: Transparent Background [modified] Pinmember/randz16:31 26 Sep '07  
AnswerRe: Transparent Background PinmemberJinHwan4619:40 24 Jul '08  
GeneralWith anti-aliasing support Pinmemberflops425:49 8 Aug '07  
GeneralRe: With anti-aliasing support [modified] Pinmember/randz17:56 8 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 26 Sep 2007
Article Copyright 2007 by /randz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid