C# TabControl手動觸發(fā)DrawItem的實現(xiàn)
當(dāng)需要重繪TabControl的背景顏色時,有兩種方法。
方法一
網(wǎng)上有很多文章介紹,將tabControl的DrawMode屬性設(shè)為OwnerDrawFixed,然后在其DrawItem事件處理程序中重繪背景顏色。
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
//標(biāo)簽header的背景
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
e.Graphics.FillRectangle(Brushes.White, (sender as TabControl).GetTabRect(i));
}
//tabControl背景
e.Graphics.FillRectangle(Brushes.White, (sender as TabControl).ClientRectangle);
}
方法二
當(dāng)將tabControl的ItemSize設(shè)為(0,1)以便隱藏標(biāo)簽title的時候,方法一的事件不會被觸發(fā),會導(dǎo)致背景顏色無法重繪,這時候可以使用以下代碼手動執(zhí)行重繪。
private void ReDrawItem()
{
Graphics g = Graphics.FromHwnd(tabControl1.Handle);
//tabPage標(biāo)簽header的背景
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
g.FillRectangle(Brushes.White, tabControl1.GetTabRect(i));
}
//tabControl背景
g.FillRectangle(Brushes.White, tabControl1.ClientRectangle);
}
到此這篇關(guān)于C# TabControl手動觸發(fā)DrawItem的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# 手動觸發(fā)DrawItem內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#如何根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04

