最近从网上看了一篇美化ComboBox的C#代码,但是e.DrawFocusRectangle();不理解意思,注释掉以后感觉程序运行没什么变化,请哪位高高人给小弟解释一下啊
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//确定画布,个人认为这句貌似在这里没用,注释掉以后程序运行没感到不同 Graphics g = e.Graphics; //绘制区域
Rectangle r=e.Bounds;
System.Drawing.Font fn=null; if (e.Index>=0)
{
//设置字体,字符串格式,对齐方式
fn = (System.Drawing.Font)fontArray[e.Index];
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near; //根据不同的状态用不同的颜色表示
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.ComboBoxEdit))
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
} }
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//确定画布,个人认为这句貌似在这里没用,注释掉以后程序运行没感到不同 Graphics g = e.Graphics; //绘制区域
Rectangle r=e.Bounds;
System.Drawing.Font fn=null; if (e.Index>=0)
{
//设置字体,字符串格式,对齐方式
fn = (System.Drawing.Font)fontArray[e.Index];
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near; //根据不同的状态用不同的颜色表示
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.ComboBoxEdit))
{
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
e.DrawFocusRectangle();
} }
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();