網(wǎng)站首頁 編程語言 正文
實踐過程
效果
代碼
public partial class B_ComboBox : ComboBox
{
public B_ComboBox()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawFixed;
//this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ListBox_DrawItem);
}
#region 變量
private static Brush[] listBoxBrushes; //該數(shù)組用來存儲繪制listBox1背景的Brush對象
private static int place = -1; //顏色位置的取值
private static bool naught = true; //判斷是否重繪
#endregion
#region 屬性
private bool TGradualC = false;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("判斷是否進行漸變色的設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public bool GradualC
{
get { return TGradualC; }
set
{
TGradualC = value;
this.Invalidate();
}
}
private Color TColorSelect = Color.Gainsboro;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("項被選中后的高亮度顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color ColorSelect
{
get { return TColorSelect; }
set
{
TColorSelect = value;
this.Invalidate();
}
}
private Color TColor1 = Color.CornflowerBlue;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第一個顏色的設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color1
{
get { return TColor1; }
set
{
TColor1 = value;
this.Invalidate();
}
}
private Color TColor1Gradual = Color.Thistle;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第一個顏色的漸變色設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color1Gradual
{
get { return TColor1Gradual; }
set
{
TColor1Gradual = value;
this.Invalidate();
}
}
private Color TColor2 = Color.PaleGreen;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第二個顏色的設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color2
{
get { return TColor2; }
set
{
TColor2 = value;
this.Invalidate();
}
}
private Color TColor2Gradual = Color.DarkKhaki;
[Browsable(true), Category("控件的重繪設(shè)置"), Description("第二個顏色的漸變色設(shè)置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color2Gradual
{
get { return TColor2Gradual; }
set
{
TColor2Gradual = value;
this.Invalidate();
}
}
#endregion
private void B_ComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle r = new Rectangle(0, 0, this.Width, this.Height); //設(shè)置重繪的區(qū)域
SolidBrush SolidB1 = new SolidBrush(this.Color1); //設(shè)置上一行顏色
SolidBrush SolidB2 = new SolidBrush(this.Color2); //設(shè)置下一行顏色
//設(shè)置上一行的漸變色
LinearGradientBrush LinearG1 = new LinearGradientBrush(r, this.Color1, this.Color1Gradual,
LinearGradientMode.BackwardDiagonal);
//設(shè)置下一行的漸變色
LinearGradientBrush LinearG2 = new LinearGradientBrush(r, this.Color2, this.Color2Gradual,
LinearGradientMode.BackwardDiagonal);
//將單色與漸變色存入Brush數(shù)組中
listBoxBrushes = new Brush[] {SolidB1, LinearG1, SolidB2, LinearG2};
if (this.Items.Count <= 0) //如果當(dāng)前控件為空
return;
if (e.Index == (this.Items.Count - 1)) //如果繪制的是最后一個項
{
bool tem_bool = true;
if (e.Index == 0 && tem_bool) //如果當(dāng)前繪制的是第一個或最后一個項
naught = false; //不進行重繪
}
if (naught) //對控件進行重繪
{
//獲取當(dāng)前繪制的顏色值
Brush brush =
listBoxBrushes[
place = (GradualC) ? (((e.Index % 2) == 0) ? 1 : 3) : (((e.Index % 2) == 0) ? 0 : 2)];
//用指定的畫刷填充列表項范圍所形成的矩形
e.Graphics.FillRectangle(brush, e.Bounds);
//判斷當(dāng)前項是否被選取中
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;
if (selected) //如果當(dāng)前項被選中
{
e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds); //繪制當(dāng)前項
}
//繪制當(dāng)前項中的文本
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
}
e.DrawFocusRectangle(); //繪制聚焦框
}
}
原文鏈接:https://zhima.blog.csdn.net/article/details/128101726
相關(guān)推薦
- 2022-09-17 ASP.NET?Core項目中集成TypeScript_實用技巧
- 2023-06-17 pytorch?簡介及常用工具包展示_python
- 2022-08-07 Android?實現(xiàn)自定義折線圖控件_Android
- 2022-10-22 Python?NumPy教程之?dāng)?shù)組的創(chuàng)建詳解_python
- 2022-07-15 Android自定義view實現(xiàn)圓形進度條效果_Android
- 2022-12-12 Android?DataBinding布局的加載深入探究_Android
- 2023-11-21 NVIDIA jetson nano/ Linux/ Ubuntu18.0.4 配置固定IP靜態(tài)IP
- 2022-05-31 openCV實現(xiàn)圖像融合的示例代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支