摘錄的WinForm control 開發1..BorderLabel

來源:互聯網
上載者:User

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(0, 0), 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.