|
按钮的绘制我们可以分为三个部分:图标(Image)、文字(Text)和背景(Background)。图标和文字绘制的难点是计算他们不同相对关系时的位置,背景就是要实现不同鼠标状态下的不同效果,还有就是要绘制漂亮的效果,也要花不少心思。 按钮的图标和文字的关系(TextImageRelation)有5种,我们要对这5种不同的关系计算出图标和文字的具体位置,而图标和文本又还有不同的对齐方式,所以计算起来非常复杂,
实际处理中,我只对他们的关系作了处理,图标的对齐方式默认为居中,因为这让比较符合我们的用法习惯,文字的对齐方式是做了处理的。看看计算图标和文字位置的函数:
private void CalculateRect( out Rectangle imageRect, out Rectangle textRect)
{
imageRect = Rectangle.Empty;
textRect = Rectangle.Empty;
if (Image == null)
{
textRect = new Rectangle(
2,
0,
Width - 4,
Height);
return;
}
switch (TextImageRelation)
{
case TextImageRelation.Overlay:
imageRect = new Rectangle(
2,
(Height - ImageWidth) / 2,
ImageWidth,
ImageWidth);
textRect = new Rectangle(
2,
0,
Width - 4,
Height);
break;
case TextImageRelation.ImageAboveText:
imageRect = new Rectangle(
(Width - ImageWidth) / 2,
2,
ImageWidth,
ImageWidth);
textRect = new Rectangle(
2,
imageRect.Bottom,
Width,
Height - imageRect.Bottom - 2);
break;
case TextImageRelation.ImageBeforeText:
imageRect = new Rectangle(
2,
(Height - ImageWidth) / 2,
ImageWidth,
ImageWidth);
textRect = new Rectangle(
imageRect.Right + 2,
0,
Width - imageRect.Right - 4,
Height);
break;
case TextImageRelation.TextAboveImage:
imageRect = new Rectangle(
(Width - ImageWidth) / 2,
Height - ImageWidth - 2,
ImageWidth,
ImageWidth);
textRect = new Rectangle(
0,
2,
Width,
Height - imageRect.Y - 2);
break;
case TextImageRelation.TextBeforeImage:
imageRect = new Rectangle(
Width - ImageWidth - 2,
(Height - ImageWidth) / 2,
ImageWidth,
ImageWidth);
textRect = new Rectangle(
2,
0,
imageRect.X - 2,
Height);
break;
}
if (RightToLeft == RightToLeft.Yes)
{
imageRect.X = Width - imageRect.Right;
textRect.X = Width - textRect.Right;
}
}
在这个新的控件中,加入了几个新的属性,来实现不同的外观效果。BaseColor用来设置不同的外观颜色,RoundStyle用来设置按钮哪些角显示为圆角,Radius用来设置圆角的宽度,ImageWidth用来设置图标显示的大小。来看看具体效果:
![]() |






骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价