一早收到CodeProject的订阅邮件,大概看了一下。里面有一个做VerticalLabel的例子,我没看,就想自己做一个试试。抛砖引玉!
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Webb.Publics.WinForm


{

/**//// <summary>
/// Summary description for VerticalLabel.
/// </summary>
public class VerticalLabel : System.Windows.Forms.Label

{
private int _Span = 0;
public int TextSpan

{

get
{return this._Span;}

set
{
this._Span = value;
this.Refresh();
}
}

public VerticalLabel()

{
//
// TODO: Add constructor logic here
//
this.Width = 35;
this.Height = 100;
}

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)

{
//base.OnPaint (e);
SizeF m_size = e.Graphics.MeasureString(this.Text,this.Font);
float m_SingleWidth = m_size.Width/this.Text.Length+2;
float m_SingleHeight = m_size.Height;
float m_TotalWidth = m_SingleWidth;
float m_TotalHeight = m_size.Height*this.Text.Length;
//int i = 0;
PointF m_Offset = this.MeasurePosition(m_TotalWidth,m_TotalHeight);
for(int i = 0;i<this.Text.Length;i++)

{
RectangleF m_Rec = new RectangleF(m_Offset.X,m_Offset.Y+(m_SingleHeight+this.TextSpan)*i,m_SingleWidth,m_SingleHeight);
e.Graphics.DrawString(this.Text.Substring(i,1),this.Font,new SolidBrush(this.ForeColor),m_Rec);
}
}

private PointF MeasurePosition(float i_TotalWidth,float i_TotalHeight)

{
PointF m_Result;
switch(this.TextAlign)

{
//Bottom
case ContentAlignment.BottomCenter:
m_Result = new PointF((this.Width-i_TotalWidth)/2,this.Height-i_TotalHeight);
break;
case ContentAlignment.BottomLeft:
m_Result = new PointF(0,this.Height-i_TotalHeight);
break;
case ContentAlignment.BottomRight:
m_Result = new PointF(this.Width-i_TotalWidth,this.Height-i_TotalHeight);
break;
//Middle
case ContentAlignment.MiddleCenter:
m_Result = new PointF((this.Width-i_TotalWidth)/2,(this.Height-i_TotalHeight)/2);
break;
case ContentAlignment.MiddleLeft:
m_Result = new PointF(0,(this.Height-i_TotalHeight)/2);
break;
case ContentAlignment.MiddleRight:
m_Result = new PointF(this.Width-i_TotalWidth,(this.Height-i_TotalHeight)/2);
break;
//Top
case ContentAlignment.TopCenter:
m_Result = new PointF((this.Width-i_TotalWidth)/2,0);
break;
default:
case ContentAlignment.TopLeft:
m_Result = new PointF(0,0);
break;
case ContentAlignment.TopRight:
m_Result = new PointF(this.Width-i_TotalWidth,0);
break;
}
return m_Result;
}
}
}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication1


{

/**//// <summary>
/// Summary description for Form5.
/// </summary>
public class Form5 : System.Windows.Forms.Form

{
private System.Windows.Forms.PropertyGrid propertyGrid1;
private Webb.Publics.WinForm.VerticalLabel verticalLabel2;

/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form5()

{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}


/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if(components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows Form Designer generated code#region Windows Form Designer generated code

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

{
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.verticalLabel2 = new Webb.Publics.WinForm.VerticalLabel();
this.SuspendLayout();
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Right;
this.propertyGrid1.LargeButtons = false;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(304, 0);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.PropertySort = System.Windows.Forms.PropertySort.Alphabetical;
this.propertyGrid1.SelectedObject = this.verticalLabel2;
this.propertyGrid1.Size = new System.Drawing.Size(272, 269);
this.propertyGrid1.TabIndex = 1;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// verticalLabel2
//
this.verticalLabel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.verticalLabel2.Location = new System.Drawing.Point(8, 8);
this.verticalLabel2.Name = "verticalLabel2";
this.verticalLabel2.Size = new System.Drawing.Size(288, 256);
this.verticalLabel2.TabIndex = 3;
this.verticalLabel2.Text = "这是一个测试";
this.verticalLabel2.TextSpan = 0;
//
// Form5
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(576, 269);
this.Controls.Add(this.verticalLabel2);
this.Controls.Add(this.propertyGrid1);
this.Name = "Form5";
this.Text = "Form5";
this.Load += new System.EventHandler(this.Form5_Load);
this.ResumeLayout(false);

}
#endregion

private void Form5_Load(object sender, System.EventArgs e)

{
}
}
}
