VerticalLabel的简单实现

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

VerticalLabel.JPG

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

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

    }

}

原文地址:https://www.cnblogs.com/WuCountry/p/838599.html