|
楼主 |
发表于 2007-1-5 12:46:33
|
显示全部楼层
呵呵,也不能让大家失望啊~~发点实用的给一起学习C#的朋友们~~
重绘LISTBOX
感觉重新绘制一遍比默认样式好看多了
在窗口中拖一个ListBox控件~~设置它的DrawMode属性为OwnerDrawVariable
然后要添加两个事件~~~DrawItem事件跟MeasureItem事件
下面贴出我的代码
- private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
- {
- e.DrawBackground();
- Rectangle r = new Rectangle(0, 0, listBox1.Width, 100);
- bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
- LinearGradientBrush lgb = null;
- if (!selected)
- lgb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);
- else
- lgb = new LinearGradientBrush(r, Color.Cyan, Color.White, LinearGradientMode.Horizontal);
- e.Graphics.FillRectangle(lgb, e.Bounds);
- e.Graphics.DrawRectangle(SystemPens.WindowText, e.Bounds);
- Rectangle r2 = e.Bounds;
- string displayText = (string)listBox1.Items[e.Index];
- SizeF size = e.Graphics.MeasureString(displayText, this.Font);
- r2.Y = (int)(r2.Height / 2) - (int)(size.Height / 2) + e.Bounds.Y;
- r2.X = 2;
- e.Graphics.DrawString(displayText, this.Font, Brushes.Black, r2);
- e.DrawFocusRectangle();
- }
- private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
- {
- string displayText = (string)listBox1.Items[e.Index];
- SizeF size = e.Graphics.MeasureString(displayText, this.Font);
- size.Height += 10;
- e.ItemHeight = (int)size.Height;
- }
复制代码
附件为效果图~~~没用到GDI+的高深知识就能改善控件的外观样式~~~效果不错吧````
[ 本帖最后由 欣云 于 2007-1-5 12:54 编辑 ] |
-
我绘制的ListBox控件
|