網(wǎng)站首頁 編程語言 正文
實踐過程
效果
代碼
public partial class DrawListBox : ListBox
{
public DrawListBox()
{
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("控件的重繪設置"), 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
#region 事件
/// <summary>
/// 鼠標移出控件的可見區(qū)域時觸發(fā)
/// </summary>
protected virtual void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
Rectangle r = new Rectangle(0, 0, this.Width, this.Height);//設置重繪的區(qū)域
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數(shù)組中
listBoxBrushes = new Brush[] { SolidB1, LinearG1, SolidB2, LinearG2 };
e.DrawBackground();
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();//繪制聚焦框
}
#endregion
}
原文鏈接:https://zhima.blog.csdn.net/article/details/128101960
相關(guān)推薦
- 2022-07-19 Linux cat more grep head tail cut uniq sort tr命令詳解
- 2022-05-13 python實現(xiàn)簡易圖書管理系統(tǒng)_python
- 2023-07-16 uniapp 調(diào)用拍照組件
- 2022-11-29 詳解Go語言設計模式之單例模式_Golang
- 2023-07-14 瀏覽器本地存儲Cookie、LocalStorage、SessionStorage
- 2021-12-03 Go語言原子操作及互斥鎖的區(qū)別_Golang
- 2024-01-27 Linux關(guān)于Centos IP靜態(tài)配置
- 2022-09-13 c++實現(xiàn)排序算法之希爾排序方式_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支