C#控件美化之路(5):图标按钮各类样式

C#控件美化之路(5):图标按钮各类样式


上一篇文章已经将图标文字绘制完成。

在一般软件开发中,常用按钮一般不止,一个靠左图标和靠左文字。

常用的一般三种按钮。文字靠下图标居上居中,文字浮现在图标上方。如上图。

公有属性

<code>        [ ]
        [ ]
        

public

TextImageRelation TextImageRelation {

get

;

set

; } = TextImageRelation.ImageBeforeText;/<code>

上一篇文章已经提到如何绘制一个图标,也详细解释了如何使得图标垂直居中。本文不做详细解释。

贴关键代码

第一个:绘制按钮居中,按钮居中

<code>                 
                recimage = new Rectangle((

this

.Width - ImageSize.Width) /

2

, (

this

.Height - ImageSize.Height) /

2

, ImageSize.Width, ImageSize.Height); recString = new Rectangle(

0

,

3

+ ImageSize.Height,

this

.Width,

this

.Height -

3

- ImageSize.Height);

if

(Image !=

null

) g.DrawImage(Image, recimage); g.DrawString(

this

.Text, Font, brush, rec, ControlHelper.StringContersCenter);/<code>

贴上一个比较常用的属性,可以做一个常用静态帮助类

<code>         
         
         
        

public

static

StringFormat StringConters {

get

;

set

; } =

new

StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap) { LineAlignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter };

public

static

StringFormat StringContersCenter {

get

;

set

; } =

new

StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap) { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter };/<code>

以后绘制文字时候就不用每次都写很长一段代码。

第二个:绘制图标考上居中,文字靠下居中

<code>                 
                recimage = new Rectangle((

this

.Width - ImageSize.Width) /

2

,

3

, ImageSize.Width, ImageSize.Height); recString = new Rectangle(

0

,

3

+ ImageSize.Height,

this

.Width,

this

.Height -

3

- ImageSize.Height);

if

(Image !=

null

) g.DrawImage(Image, recimage); g.DrawString(

this

.Text, Font, brush, recString, ControlHelper.StringContersCenter);/<code>

到目前位置一个完整的按钮绘制完成

贴上完整代码块。小伙伴可以直接诶复制粘贴开箱即用。

<code>        

protected

override

void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; Rectangle rec =

this

.ClientRectangle; Rectangle recimage = Rectangle.Empty; Rectangle recString = Rectangle.Empty; Brush brush = new SolidBrush(

this

.ForeColor);

if

(TextImageRelation == TextImageRelation.ImageAboveText) { recimage = new Rectangle((

this

.Width - ImageSize.Width) /

2

,

3

, ImageSize.Width, ImageSize.Height); recString = new Rectangle(

0

,

3

+ ImageSize.Height,

this

.Width,

this

.Height -

3

- ImageSize.Height);

if

(Image !=

null

) g.DrawImage(Image, recimage); g.DrawString(

this

.Text, Font, brush, recString, ControlHelper.StringContersCenter); }

else

if

(TextImageRelation == TextImageRelation.Overlay || Image ==

null

) { recimage = new Rectangle((

this

.Width - ImageSize.Width) /

2

, (

this

.Height - ImageSize.Height) /

2

, ImageSize.Width, ImageSize.Height); recString = new Rectangle(

0

,

3

+ ImageSize.Height,

this

.Width,

this

.Height -

3

- ImageSize.Height);

if

(Image !=

null

) g.DrawImage(Image, recimage); g.DrawString(

this

.Text, Font, brush, rec, ControlHelper.StringContersCenter); }

else

{ recimage = new Rectangle(

2

, (

this

.Height - ImageSize.Height) /

2

, ImageSize.Width, ImageSize.Height); recString = new Rectangle(

2

+ ImageSize.Width +

2

,

0

,

this

.Width - ImageSize.Width -

6

,

this

.Height);

if

(Image !=

null

) g.DrawImage(Image, recimage); g.DrawString(

this

.Text, Font, brush, recString, ControlHelper.StringConters); }

if

(

this

.BorderStyle == BorderStyle.FixedSingle) { System.Drawing.Drawing2D.GraphicsPath path = ControlHelper.CreateRoundedRectanglePath(new Rectangle(

0

,

0

, Width -

1

, Height -

1

),

10

); using (Pen pen = new Pen(Color.FromArgb(

153

,

153

,

153

))) { g.DrawPath(pen, path); } } }/<code>

感兴趣的小伙伴,欢迎在下方留言。关注我,带你重绘美化C#控件


C#控件美化之路(5):图标按钮各类样式


分享到:


相關文章: