網站首頁 編程語言 正文
實踐過程
效果
代碼
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;//該數組用來存儲繪制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>
/// 鼠標移出控件的可見區域時觸發
/// </summary>
protected virtual void ListBox_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 };
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
相關推薦
- 2022-11-23 shell腳本設置日志格式的方法_linux shell
- 2022-10-26 c語言數據結構之棧和隊列詳解(Stack&Queue)_C 語言
- 2022-07-14 一文教會你用redux實現computed計算屬性_React
- 2023-05-22 NumPy迭代數組的實現_python
- 2022-07-24 Golang實現可重入鎖的示例代碼_Golang
- 2022-08-13 beginInvke帶回調函數使用
- 2022-11-30 C#?WinForm自動更新程序之文件上傳操作詳解_C#教程
- 2023-07-31 el-tree默認展開或折疊,全選或全不選
- 最近更新
-
- 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同步修改后的遠程分支