網站首頁 編程語言 正文
實踐過程
效果
代碼
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; //該數組用來存儲繪制listBox1背景的Brush對象
private static int place = -1; //顏色位置的取值
private static bool naught = true; //判斷是否重繪
#endregion
#region 屬性
private bool TGradualC = false;
[Browsable(true), Category("控件的重繪設置"), Description("判斷是否進行漸變色的設置")] //在“屬性”窗口中顯示DataStyle屬性
public bool GradualC
{
get { return TGradualC; }
set
{
TGradualC = value;
this.Invalidate();
}
}
private Color TColorSelect = Color.Gainsboro;
[Browsable(true), Category("控件的重繪設置"), Description("項被選中后的高亮度顏色")] //在“屬性”窗口中顯示DataStyle屬性
public Color ColorSelect
{
get { return TColorSelect; }
set
{
TColorSelect = value;
this.Invalidate();
}
}
private Color TColor1 = Color.CornflowerBlue;
[Browsable(true), Category("控件的重繪設置"), Description("第一個顏色的設置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color1
{
get { return TColor1; }
set
{
TColor1 = value;
this.Invalidate();
}
}
private Color TColor1Gradual = Color.Thistle;
[Browsable(true), Category("控件的重繪設置"), Description("第一個顏色的漸變色設置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color1Gradual
{
get { return TColor1Gradual; }
set
{
TColor1Gradual = value;
this.Invalidate();
}
}
private Color TColor2 = Color.PaleGreen;
[Browsable(true), Category("控件的重繪設置"), Description("第二個顏色的設置")] //在“屬性”窗口中顯示DataStyle屬性
public Color Color2
{
get { return TColor2; }
set
{
TColor2 = value;
this.Invalidate();
}
}
private Color TColor2Gradual = Color.DarkKhaki;
[Browsable(true), Category("控件的重繪設置"), Description("第二個顏色的漸變色設置")] //在“屬性”窗口中顯示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); //設置重繪的區域
SolidBrush SolidB1 = new SolidBrush(this.Color1); //設置上一行顏色
SolidBrush SolidB2 = new SolidBrush(this.Color2); //設置下一行顏色
//設置上一行的漸變色
LinearGradientBrush LinearG1 = new LinearGradientBrush(r, this.Color1, this.Color1Gradual,
LinearGradientMode.BackwardDiagonal);
//設置下一行的漸變色
LinearGradientBrush LinearG2 = new LinearGradientBrush(r, this.Color2, this.Color2Gradual,
LinearGradientMode.BackwardDiagonal);
//將單色與漸變色存入Brush數組中
listBoxBrushes = new Brush[] {SolidB1, LinearG1, SolidB2, LinearG2};
if (this.Items.Count <= 0) //如果當前控件為空
return;
if (e.Index == (this.Items.Count - 1)) //如果繪制的是最后一個項
{
bool tem_bool = true;
if (e.Index == 0 && tem_bool) //如果當前繪制的是第一個或最后一個項
naught = false; //不進行重繪
}
if (naught) //對控件進行重繪
{
//獲取當前繪制的顏色值
Brush brush =
listBoxBrushes[
place = (GradualC) ? (((e.Index % 2) == 0) ? 1 : 3) : (((e.Index % 2) == 0) ? 0 : 2)];
//用指定的畫刷填充列表項范圍所形成的矩形
e.Graphics.FillRectangle(brush, e.Bounds);
//判斷當前項是否被選取中
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ? true : false;
if (selected) //如果當前項被選中
{
e.Graphics.FillRectangle(new SolidBrush(ColorSelect), e.Bounds); //繪制當前項
}
//繪制當前項中的文本
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, Brushes.Black, e.Bounds);
}
e.DrawFocusRectangle(); //繪制聚焦框
}
}
原文鏈接:https://zhima.blog.csdn.net/article/details/128101726
相關推薦
- 2022-02-20 Ubuntu18.04更改apt源為阿里云源的詳細過程_Linux
- 2022-09-01 寫一個shell腳本實現視頻處理_linux shell
- 2022-06-26 Python使用Tkinter?GUI實現輸入驗證功能_python
- 2023-01-07 Android?RecyclerLineChart實現圖表繪制教程_Android
- 2023-07-15 element 確認消息的使用步驟
- 2022-04-26 python?logging多進程多線程輸出到同一個日志文件的實戰案例_python
- 2024-01-07 @Modifying注解和@Transactional注解?
- 2022-10-11 XGBoost與GBDT和LGBM區別
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支