65.9K
CodeProject 正在变化。 阅读更多。
Home

C# .NET 中的扩展垂直标签控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.27/5 (9投票s)

2007年7月27日

CPOL

3分钟阅读

viewsIcon

182552

downloadIcon

8785

C# .NET 中一个自定义的垂直标签用户控件,支持透明背景。

Screenshot - trans_vertical.png

引言

本文介绍了如何在 C# .NET 中创建自定义的垂直标签用户控件。该用户控件提供从上到下或从下到上绘制文本的功能。本文是基于 Raman Tayal 的 VB.NET 垂直标签控件的衍生。我只是将他的作品翻译成 C#,并增加了从下到上绘制文本的功能。此外,更新后的版本现在支持透明背景。

背景

在我的一个项目中,我需要一个可以垂直显示文本的标签控件。我遇到了 Raman Tayal 的 VB.NET 垂直标签控件 并将其翻译成 C#。但是,我需要从上到下绘制文本的附加功能,所以我添加了这个功能。这个控件对我很有用,我希望其他人也能发现它有用。

Using the Code

提供的代码是一个类,它创建了一个 DLL,可以作为 Windows Forms 设计器中工具箱中的一个项目添加。该类使用以下命名空间

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

代码

真正完成这项工作的代码部分是 OnPaint 事件的重写。

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);
    }            
}

如您所见,我在 if (this.TextDrawMode == DrawMode.BottomUp) 中有一个 if 条件。这告诉我们控件在哪里决定根据属性 TextDrawMode 的值从下往上或从上往下绘制文本。

TextDrawMode 的值为 BottomUp 时,您会注意到 TranslateTransform 接受 0 作为平移的 X 分量,并接受控件的高度作为平移的 Y 分量的值。这告诉 GDI 从控件所占据的矩形的左下角开始绘制。

TextDrawMode 的值为 TopBottom 时,您将看到 TranslateTransform 接受控件的宽度作为平移的 X 分量,并接受 0 作为平移的 Y 分量。这告诉 GDI 从控件所占据的矩形的右上角开始绘制。

TextDrawMode 属性是一个附加属性,可以在设计时和运行时设置。

在此更新中,请注意我正在检查 _transparentBG 变量的值,该变量从公共布尔属性 TransparentBackground 获取其值。如果将其设置为 true,请注意 Brush 的颜色设置为 Color.Empty;否则,它使用控件的已分配 Color

此外,我对 VerticalLabel 控件的构造函数进行了修改,以包含以下行

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

最后,为了启用透明度,添加了以下重写

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x20;  // Turn on WS_EX_TRANSPARENT
        return cp;
    }
}

在提供的屏幕截图中,水平对齐的文本使用 WinForms Label 控件。这三个垂直标签以不同的方向 (Bottom-Up | Top-Bottom) 显示,并且具有不同的透明度 (BackgroundTransparent = true|false) 设置。

关注点

由于这是我第一次使用 GDI+ 编写程序,我尝试使用反复试验,但起初令人沮丧,直到我找到了一篇关于如何使用 Graphics.RotateTransform不错的文章

历史

  • 2007 年 7 月 27 日:初始版本。
  • 9 月 27 日:更新,支持透明背景。
© . All rights reserved.