背景透明的ListBox(C#.WinForm)
2013-07-03 09:52
501 查看
不知道大家有没有遇到过这种问题,C#中希望搞一个背景透明的ListBox,且文本居中,却发现VS里面自带的ListBox控件不支持背景透明,怎么办?
我们可以做一个自定义控件,重载里面的OnPaint函数,使ListBox支持背景透明
我们可以做一个自定义控件,重载里面的OnPaint函数,使ListBox支持背景透明
/// <summary> /// 实现背景透明的ListBox,且文本居中 /// 编写人:涂剑凯 /// </summary> public class TransParentListBox : ListBox { public TransParentListBox() { //如果为 true,控件将自行绘制,而不是通过操作系统来绘制。 //如果为 false,将不会引发 Paint 事件。此样式仅适用于派生自 Control 的类。 this.SetStyle(ControlStyles.UserPaint, true); //如果为 true,控件接受 alpha 组件小于 255 的 BackColor 以模拟透明。 //仅在 UserPaint 位设置为 true 并且父控件派生自 Control 时才模拟透明。 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); } protected override void OnSelectedIndexChanged(EventArgs e) { this.Invalidate(); base.OnSelectedIndexChanged(e); } protected override void OnPaint(PaintEventArgs e) { if (this.Focused && this.SelectedItem != null) { //设置选中行背景颜色 Rectangle itemRect = this.GetItemRectangle(this.SelectedIndex); //e.Graphics.FillRectangle(Brushes.Red, itemRect); e.Graphics.FillRectangle(Brushes.LightBlue, itemRect); } for (int i = 0; i < Items.Count; i++) { //设置绘制文字的格式 StringFormat strFmt = new System.Drawing.StringFormat(); strFmt.Alignment = StringAlignment.Center; //文本垂直居中 strFmt.LineAlignment = StringAlignment.Center; //文本水平居中 e.Graphics.DrawString(this.GetItemText(this.Items[i]), this.Font, new SolidBrush(this.ForeColor), this.GetItemRectangle(i), strFmt); //e.Graphics.DrawString(this.GetItemText(this.Items[i]), this.Font, new SolidBrush(this.ForeColor), this.GetItemRectangle(i)); } base.OnPaint(e); } }
相关文章推荐
- C# winform如何设置ListBox背景图或者透明背景及边框色
- 背景透明的ListBox(C#.WinForm)
- C# WinForm中控件与背景透明+c# linklabel 去掉下划线的方法
- C# Winform中如何让PictureBox的背景透明
- C# WinForm中控件与背景透明
- C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
- C# Winform 变相实现背景透明
- 实现C#WinForm透明背景
- C#(winform)为button添加背景图片
- [C#]桌面模式下GDI绘制(全透明背景)
- c#(winform)中自定义ListItem类方便ComboBox和ListBox添加项完全解决(DropDownList 和Combox 数据绑定的常用方法)
- C#中保存GIF文件后透明背景问题的一个解决方法
- 【转】C#实现Winform自定义半透明遮罩层
- C# WinForm控件美化扩展系列之ListBox
- VS2005 C# WEB swf 文件背景透明
- [转自:https://www.cnblogs.com/dskin/p/4606293.html] C# Winform实现炫酷的透明动画界面 做过.NET Winform窗体美化的人应该都很熟悉U
- C#之WinForm基础 应用button,实现listbox的蓝条上下循环移动
- C#将图片白色背景设置为透明
- C# WinForm开发系列 - ListBox/ListView/Panel【zz】
- C# 实现PNG文件的背景透明显示,解决动态显示闪烁问题 【转】