C#中ListBox和ListView的透明及内容居中效果实现
2017-06-19 21:23
399 查看
作为一个C#的初学者,本文的技术参考多篇博客和论坛的内容,具体来自哪里也不甚了了,在此多谢这些大神。
以下代码是ListBox的透明及内容居中效果实现,ListView可依此同样实现。
以下代码是ListBox的透明及内容居中效果实现,ListView可依此同样实现。
class TransparentListBox : ListBox { private Boolean isAllignCenter; public TransparentListBox(Boolean center) { this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BorderStyle = System.Windows.Forms.BorderStyle.None; //this.MultiSelect = false; isAllignCenter = center; } protected override void OnSelectedIndexChanged(EventArgs e) { this.Invalidate(); base.OnSelectedIndexChanged(e); } protected override void OnPaint(PaintEventArgs e) { System.Drawing.StringFormat strFmt = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip); strFmt.Alignment = System.Drawing.StringAlignment.Center; if (this.Focused && this.SelectedItem != null) { Rectangle itemRect = this.GetItemRectangle(this.SelectedIndex); e.Graphics.FillRectangle(Brushes.LightBlue, itemRect); } for (int i = 0; i < Items.Count; i++) { if (isAllignCenter == true) { e.Graphics.DrawString(this.GetItemText(this.Items[i]), this.Font, new SolidBrush(this.ForeColor), this.GetItemRectangle(i), strFmt); } else { e.Graphics.DrawString(this.GetItemText(this.Items[i]), this.Font, new SolidBrush(this.ForeColor), this.GetItemRectangle(i)); } } base.OnPaint(e); } }
相关文章推荐
- c#的listview实现分页效果
- 折腾了一早上的C# WPF ListView+Grid 实现图片+文字 自动换行排列 类似Windows资源管理器效果
- c#的listview实现分页效果
- C# Winform ListView实现单元格双击复制内容到剪贴板
- C# 重写 Listview 的 OnDrawSubItem 函数实现 ProgressBar 进度条效果
- CSS实现背景透明/半透明效果 但内容文字不透明
- C#中listBox实现自动滚动到底部的效果
- C# 鼠标拖放图片透明缩略图效果实现思路
- 实习日记(5)listview实现简介内容简介效果
- CSS实现Div层背景半透明而内容不透明的效果
- CSS实现Div层背景半透明而内容不透明效果
- C# WinForm实现Win7 Aero透明效果代码
- C# 系统应用之透明罩MyOpaqueLayer实现360界面阴影效果
- C# 实现 TextBox内容全部选择的效果 select()
- Android内容覆盖透明状态栏下实现全屏模式下带状态栏的效果
- C#中实现Form的透明属性变化即渐隐效果
- css实现内容上下左右居中的效果
- 类似 MSDN CSDN 导航树效果 ASP.Net(C#) + JavaScript 实现!
- js+css样式表实现鼠标移动变换显示内容效果
- 在datagrid里实现文章内容在点击标题的时候收起或者展开的效果