代码改变世界

摘录的WinForm control 开发1..BorderLabel

2010-02-22 22:36  c#在路上  阅读(424)  评论(0编辑  收藏  举报

BorderLabel ,对普通的winform拓展。

1,可以调整字体位置

2,可以调整的前景色,后景色

3,可以调整字体位置 

关键代码如下。 

代码
绘制string对象在画板上:
//First, we begin by setting the smoothing mode to
            
//AntiAlias, to reduce image sharpening and improve our drawnings
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

//使用消除锯齿模型,增加图片的显示质量。
            base.OnPaint(e);
            
this.drawningPath.Reset();


            
//Second, lets use a GraphicsPath object and write our string into it.
            
//Our objective, however, is to measure the how much screen space our
            
//drawning will occupy.
在路径中加入字符串
            drawningPath.AddString(
this.text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size,
                
new Point(00), StringFormat.GenericTypographic);

            
//Then, lets calculate the area occuped by our drawning...
            this.drawningSize = GetPathSize(drawningPath);

            
//..and remember to sum also the future border size
            this.drawningSize.Height += this.borderSize + 5;
            
this.drawningSize.Width += this.borderSize + 5;



            
//Lets determine then how we should align our text in
            
//the control's area, both horizontally and vertically

            
//If text is Left-Aligned
            if (this.textAlign == ContentAlignment.TopLeft ||
                
this.textAlign == ContentAlignment.MiddleLeft ||
                
this.textAlign == ContentAlignment.BottomLeft)
                
this.drawningPoint.X = this.Padding.Left;

            
//If text is Center-Aligned
            else if (this.textAlign == ContentAlignment.TopCenter ||
                     
this.textAlign == ContentAlignment.MiddleCenter ||
                     
this.textAlign == ContentAlignment.BottomCenter)
                drawningPoint.X 
= (this.Width - this.drawningSize.ToSize().Width) / 2;

            
//If text is Right-Aligned
            else drawningPoint.X = this.Width - (this.Padding.Right + drawningSize.ToSize().Width);



            
//If text is Top-Aligned
            if (this.textAlign == ContentAlignment.TopLeft ||
                
this.textAlign == ContentAlignment.TopCenter ||
                
this.textAlign == ContentAlignment.TopRight)
                drawningPoint.Y 
= this.Padding.Top;

            
//If text is Middle-Aligned
            else if (this.textAlign == ContentAlignment.MiddleLeft ||
                     
this.textAlign == ContentAlignment.MiddleCenter ||
                     
this.textAlign == ContentAlignment.MiddleRight)
                drawningPoint.Y 
= (this.Height - drawningSize.ToSize().Height) / 2;

            
//If text is Bottom-Aligned
            else drawningPoint.Y = this.Height - (this.Padding.Bottom + drawningSize.ToSize().Height);




            
//After that we can reset our path so we can draw the text in the proper place now
            drawningPath.Reset();

            
//Next, we start add our text into its place, ...
            drawningPath.AddString(this.text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size,
                
this.drawningPoint, StringFormat.GenericTypographic);
//调整字符串对齐方式。
            
//... and then lets use our pen and draw our text to screen
            e.Graphics.DrawPath(drawningPen, drawningPath);



            
//So lets reset our path and do it again, but now for the foreground
            drawningPath.Reset();

            
//Again, we add our text into our path ...
            drawningPath.AddString(this.text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size,
                
this.drawningPoint, StringFormat.GenericTypographic);

            
//... but now, we draw also the foreground
            e.Graphics.FillPath(this.drawningForecolorBrush, drawningPath);
            e.Graphics.DrawPath(drawningPen, drawningPath);

 

 

 /Files/csharponworking/BorderLabelTest.rar