着色选项卡控件






4.13/5 (56投票s)
着色选项卡控件
引言
选项卡控件对于Windows开发者来说绝非陌生。微软从Visual Studio到Visual Studio.NET SDK都包含了此控件,以提高开发人员的效率和最终用户的体验。我同意我们的Windows开发应该遵循一些标准,以提供更好的用户体验。在这里,我尝试对控件外观进行一些额外的增强,前提是开发者希望这样做。
背景
这篇文章篇幅较小,可以应用于您不同的应用程序中,前提是它必须是WinForm开发。您可以参考这篇文章来更新您父窗口中的选项卡控件外观。
默认情况下,或者通过任何属性,现有的VS.NET SDK不支持增强选项卡页面外观的功能,例如更改字体外观、颜色、标题背景色等。在这里,我尝试做的是,稍微重写一些代码,找到解决方案。因此,您可以利用这个想法来增强您的产品或项目。
如果您觉得这篇文章有帮助,请不要忘记投票并提出您宝贵的建议。
如果您检查选项卡控件的属性列表,您会发现DrawMode
属性。DrawMode
属性决定了应用程序中如何绘制选项卡页面。如果您检查这些值,您会发现有两个值可以分配给DrawMode
属性。
DrawMode
枚举
正常
这个枚举值将指定操作系统将代表开发者决定如何绘制。
OwnerDrawn
这个枚举值将指定开发者负责绘制选项卡页面的外观。
如果您想自己绘制,则需要为TabControl的DrawItem
事件定义事件处理程序。
在这里,我也在做同样的事情。如果您想更改任何选项卡控件的选项卡页面外观,则需要将
DrawMode
属性设置为OwnerDrawFixed
。- 重写
DrawItem
事件处理程序定义。
源代码
private void tabControl1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
try
{
//This line of code will help you to
//change the apperance like size,name,style.
Font f;
//For background color
Brush backBrush;
//For forground color
Brush foreBrush;
//This construct will hell you to decide
//which tab page have current focus
//to change the style.
if(e.Index == this.tabControl1.SelectedIndex)
{
//This line of code will help you to
//change the apperance like size,name,style.
f = new Font(e.Font, FontStyle.Bold | FontStyle.Bold);
f = new Font(e.Font,FontStyle.Bold);
backBrush = new System.Drawing.SolidBrush(Color.DarkGray);
foreBrush = Brushes.White;
}
else
{
f = e.Font;
backBrush = new SolidBrush(e.BackColor);
foreBrush = new SolidBrush(e.ForeColor);
}
//To set the alignment of the caption.
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
//Thsi will help you to fill the interior portion of
//selected tabpage.
//Continue.........
}
}
注意
包含示例的完整源代码与本文一起提供。