110,534
社区成员
发帖
与我相关
我的任务
分享
// ****************************************************************************
// Copyright Swordfish Computing 2003 **
// **
// Filename: Swordfish\Utilities\VerticalLabel.cs **
// Authored by: John Stewien of Swordfish Computing **
// Date: October 2003 **
// **
// - Change Log - **
// ****************************************************************************
// $Id: $
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace xxx
{
/// <summary>
/// This class is a Label control with vertical text
/// </summary>
public class VerticalLabel : System.Windows.Forms.Label
{
/// <summary>
/// The text normally has it's top on the right hand side. When
/// this is true, the text is drawn with it's top on the left hand
/// side.
/// </summary>
private bool flip180 = true;
/// <summary>
/// Contructor.
/// </summary>
public VerticalLabel()
{
}
/// <summary>
/// Overrides the OnPaint Method. Draws vertical text on the control.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
// Get the graphics area to paint to
Graphics g = e.Graphics;
// Create rectangles for the vertical label, the horizontal label,
// and the corner where they meet
RectangleF rectangle = new RectangleF(
(float)this.ClientRectangle.X,
(float)this.ClientRectangle.Y,
(float)this.ClientRectangle.Width,
(float)this.ClientRectangle.Height);
// Set up text format
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.Trimming = StringTrimming.None;
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Set up the string alignment according to the TextAlign property.
switch(this.TextAlign)
{
// Align the text along the bottom
case ContentAlignment.BottomCenter:
goto case ContentAlignment.BottomRight;
case ContentAlignment.BottomLeft:
goto case ContentAlignment.BottomRight;
case ContentAlignment.BottomRight:
stringFormat.Alignment = flip180 ? StringAlignment.Near : StringAlignment.Far;
break;
// Align the text in the middle
case ContentAlignment.MiddleCenter:
goto case ContentAlignment.MiddleRight;
case ContentAlignment.MiddleLeft:
goto case ContentAlignment.MiddleRight;
case ContentAlignment.MiddleRight:
stringFormat.Alignment = StringAlignment.Center;
break;
// Align the text along the top
case ContentAlignment.TopCenter:
goto case ContentAlignment.TopRight;
case ContentAlignment.TopLeft:
goto case ContentAlignment.TopRight;
case ContentAlignment.TopRight:
stringFormat.Alignment = flip180 ? StringAlignment.Far : StringAlignment.Near;
break;
}
// Create brush for writing text
Brush textBrush = new SolidBrush(this.ForeColor);
// Get the width of the wrapped text
// Set the stringFormat for measuring the first character
CharacterRange[] characterRanges ={ new CharacterRange(0, Text.Length)};
stringFormat.SetMeasurableCharacterRanges(characterRanges);
Region[] stringRegions = new Region[1];
stringRegions = g.MeasureCharacterRanges(Text,Font,rectangle,stringFormat);
float textWidth = stringRegions[0].GetBounds(g).Right +2f;
// Calculate the xOffset required to align the text in accordance
// with the TextAlign property.
float xOffset = 0f;
switch(this.TextAlign)
{
// Align the text in the center
case ContentAlignment.BottomCenter:
goto case ContentAlignment.TopCenter;
case ContentAlignment.MiddleCenter:
goto case ContentAlignment.TopCenter;
case ContentAlignment.TopCenter:
xOffset = (rectangle.Width - textWidth)/2f;
break;
// Align the text on the left
case ContentAlignment.BottomLeft:
goto case ContentAlignment.TopLeft;
case ContentAlignment.MiddleLeft:
goto case ContentAlignment.TopLeft;
case ContentAlignment.TopLeft:
xOffset = flip180 ? rectangle.Width - textWidth : 0f;
break;
// Align the text on the right
case ContentAlignment.BottomRight:
goto case ContentAlignment.TopRight;
case ContentAlignment.MiddleRight:
goto case ContentAlignment.TopRight;
case ContentAlignment.TopRight:
xOffset = flip180 ? 0f : rectangle.Width - textWidth;
break;
}
// Store the current matrix
Matrix storedState = g.Transform;
// If we need to flip 180, then rotate 180, and then translate
// back onto the control area
if (flip180)
{
// Flip the view matrix 180 degrees
g.RotateTransform(180f);
// Translate to bring the text back to the rectangle.
// Remember that the translation is flipped 180 degrees
g.TranslateTransform(-rectangle.Width, -rectangle.Height);
}
// Add in the offset to align the text according to the TextAlign property
g.TranslateTransform(xOffset,0f);
// Draw the y axis label
g.DrawString(
this.Text,
this.Font,
textBrush,
rectangle,
stringFormat);
// Restore the view Transform
g.Transform = storedState;
}
/// <summary>
/// Gets/Sets whether the text should be flipped 180 degrees or not
/// </summary>
[Description("Whether the text should be flipped 180 degrees or not"),Category("Appearance")]
public bool Flip180
{
get
{
return flip180;
}
set
{
flip180 = value;
this.Invalidate();
}
}
}
}