OwnerDraw in C#

80酷酷网    80kuku.com

  对于像ComboBox等控件,我们可以通过设置它们的DrawMode特性(Attribute)来对其进行绘制.

为了方便起见,我们先定义一个类StringColorObject(这与OwnerDraw本身没有关系,只是针对这里要使用到的ComboBox而特意书写的一个类.类的命名得益于QuickStart).这个类本身很简单:



using System.Drawing;



namespace OwnerDrawSample

{

public class StringColorObject

{

//field to hold the string representing the color

private string colorRepresent;

//field to hold the actually color,the value of the string colorRepresent

private Color color;



//the constructor

//takes 2 parameters, the color representation and the color itself

public StringColorObject(string colorRepresent,Color color)

{

this.colorRepresent = colorRepresent;

this.color = color;

}



//some attributes

public string ColorRepresentation

{

get

{

return this.colorRepresent;

}

//Only getter,no setter

}



public Color Color

{

get

{

return this.color;

}

//only getter,no setter

}



//override the ToString method in System.Object

public override string ToString()

{

return this.colorRepresent;

}



//override the GetHashCode method

public override int GetHashCode()

{

//return the key field’s hash code

return this.color.GetHashCode();

}



//override the Equals method

public override bool Equals(object obj)

{

if(!(obj is StringColorObject))

return false;

return this.color.Equals(((StringColorObject)obj).color);

}

}

}



建立一个Windows Application,并从工具箱中拖拽一个ComboBox到Form中去,并命名为cmbColors然后在Form的构造函数中添加:



//

// TODO: 在InitializeComponent调用后添加任何构造函数代码

//

this.FillComboxColor(this.cmbColors);

this.cmbColors.SelectedIndex = 0;



其中,FillComboxColor(ComboBox )为自定义函数,它用来填充参数指定的ComboBox对象.它的实现为:

private void FillComboxColor(ComboBox cmb){

cmb.Items.AddRange(new Object[]{

new StringColorObject("黑色(black)",Color.Black),

new StringColorObject("蓝色(blue)",Color.Blue),

new StringColorObject("深红(dark red)",Color.DarkRed),

new StringColorObject("绿色(green)",Color.Green),

//……

});

}

上面只是一些准备工作,OwnerDraw的实际工作现在才刚刚开始.将ComboBox对象cmbColors的DrawMode设置为OwnerDrawFixed(或是OwnerDrawVariable,这里设置为OwnerDrawFixed模式).附带说明一下:DrawMode可以取Normal,OwnerDrawFixed和OwnerDrawVariable三者之一.其中,Normal模式表示控件由操作系统绘制,并且元素的大小都相等;OwnerDrawFixed模式表示控件由手工绘制,并且元素的大小都相等;OwnerDrawVariable则是表示控件由手工绘制,并且元素的大小可以不相等。



下面开始OwnerDraw的核心部分:

1) 撰写控件的MeasureItem事件

当要显示ComboBox的某一项(Item)的时候[即是单击ComboBox的下拉按钮时],这个事件会被首先唤起。它用来测量Item的长度,宽度信息。例如:

//注册事件

this.cmbColor.MeasureItem += new MeasureItemEventHandler(this.cmbColor_MeasureItem);

//实现cmbColor_MeasureItem---测量ComboBox的每一个Item

private void cmbColor_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)

{

//一般来说,这里需要设置的是MeasureItemEventArgs对象e的ItemHeight属性和ItemWidth属性

ComboBox cmb = (ComboBox)sender;

e.ItemHeight = cmb.ItemHeight;//当然,你可以写e.ItemHeight = 20;

//不过对于OwnerDrawFixed模式,这个函数好像都是多余.(对于ComboBox而言如此)

//WHY?????????????MORE WORK IS ESSENTIAL.

}



2) 撰写控件的DrawItem事件

ComboBox中的项的绘制任务将在DrawItem事件中完成.类似地,先注册该事件,然后再添加适当的代码完成对事件的响应:

//注册事件

this.cmbColor.DrawItem += new

DrawItemEventHandler(this.cmbColor_DrawItem);

//绘制Item

private void cmbColor_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

{

ComboBox cmb = (ComboBox)sender;

if(cmb == null) return;

int index = e.Index; //获取要绘制的Item的index(索引)

if(index == -1) return;

//如果Item被选择,绘制正确的背景色

e.DrawBackground();

e.DrawFocusRectangle();//因为Item被选择,绘制焦点矩形



Graphics g = e.Graphics;

//根据Item的index获取对应的颜色

Color c = ((StringColorObject)cmbColor.Items[index]).Color;

Rectangle rectColor = e.Bounds;//获取包围Item的边框,并存储在rectColor中

//将rectColor做适当变换,这不会影响到e.Bounds本身

rectColor.Offset(2,2);//右移2,下移2

rectColor.Width = 20;//宽度设置为20

rectColor.Height -= 4;//高度减4

g.DrawRectangle(new Pen(c),rectColor);//绘制rectColor代表的矩形,即是在图中看到的Item左边的矩形部分



//再次将rectColor做变换,并填充之

rectColor.Offset(2,2);

rectColor.Width = 17;//要注意,在C#中绘制和填充

rectColor.Height -= 3;// 对最后一个象素点的不同处理方式

g.FillRectangle(new SolidBrush(c),rectColor);//填充rectColor矩形,这个矩形就是在图中看到的Item左边的那个被填充的矩形



g.DrawString(((StringColorObject)cmbColor.Items[index]).ToString(),

this.Font,new SolidBrush(c),e.Bounds.Left + 25,e.Bounds.Top);

//绘制字符串

}



这段程序会运行得很好.为了能够读懂程序,你得知道如下的一些概念:

1). e.Index MeasureItemEventArgs对象的Index属性表达的意思是欲绘制的Item的索引值之义.对于上面的例子,黑色(black),蓝色(blue)…分别对应着index的0,1…(这种说法不是很严密,但易于接受.比较严密的说法是,StringColorObject对象对应着index…).

2). e.Graphics 这是要在Item上绘制的Graphics对象.有了它,就能在Item上实现图形的绘制了.

3). e.Bounds 这是要绘制的Item的边界矩形.


知道了这些概念,就不难理解上面DrawItem完成的操作以及为什么会那样去完成。



对于MenuItem的手工绘制,你要做的事和上面差不多.不同的是,你需要将其OwnerDraw由默认的false设置为true.然后再MeasureItem,DrawItem.如果你亲身实践,你会发现如果你不注册MeasureItem并辅以相应代码,你不会看到那个MenuItem.



---the end



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: