如何重写ComboBox中的OnPaint protected override void OnPaint(System.EventArgs e){ //你的代码 base.OnPaint(e);//如果必要,这里可以禁止调用} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ComboBox可以重载OnPaint????OnPaint的重载是Form才有的吧。ComboBox叫DrawItem吧。另外我重载OnPaint的定义是这样的。(如果说ComboBox的重载OnPaint也是这样那,Form与ComboBox不是有一样的重载函数了。)protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)//重载paint{....}下面是个自画ComboBox的例子。没用重载。重载DrawItem与直接在DrawItem好像没什么区别。:(private void Form1_Load(object sender, System.EventArgs e) { //创建字符串格式 fontArray .Add(new Font("Ariel" , 8 , FontStyle.Bold )); fontArray .Add(new Font("Courier" , 8 , FontStyle.Italic)); fontArray .Add(new Font("Veranda" , 8 , FontStyle.Bold)); fontArray .Add(new Font("System" , 8 , FontStyle.Strikeout)); fontArray .Add(new Font("Century SchoolBook" , 8 , FontStyle.Underline)); fontArray .Add(new Font("Helevctia" , 8 , FontStyle.Italic)); //创建画刷 brushArray.Add(new SolidBrush(Color.Red)); brushArray.Add(new SolidBrush(Color.Blue)); brushArray.Add(new SolidBrush(Color.Green)); brushArray.Add(new SolidBrush(Color.Yellow)); brushArray.Add(new SolidBrush(Color.Black)); brushArray.Add(new SolidBrush(Color.Azure)); brushArray.Add(new SolidBrush(Color.Firebrick)); brushArray.Add(new SolidBrush(Color.DarkMagenta)); brushArray.Add(new SolidBrush(Color.DarkTurquoise)); brushArray.Add(new SolidBrush(Color.Khaki)); //画comboBox1,注意它要调用comboBox1_DrawItem来画 comboBox1.Items.Add("中国"); comboBox1.Items.Add("巴西"); comboBox1.Items.Add("哥斯达黎加"); comboBox1.Items.Add("土耳其"); comboBox1.Items.Add("韩国"); comboBox1.Items.Add("日本"); //画comboBox2,注意它要调用comboBox2_DrawItem来画 comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); comboBox2.Items.Add(""); //画comboBox3,注意它要调用comboBox3_DrawItem来画 comboBox3.Items.Add("赵微"); comboBox3.Items.Add("舒淇"); } private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //确定画布 Graphics g = e.Graphics ; //绘制区域 Rectangle r = e.Bounds ; Font fn = null ; if ( e.Index >= 0 ) { //设置字体、字符串格式、对齐方式 fn = (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.NoFocusRect)) { 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(); } } } 我们重绘ComboBox时有两部份,一是可输入框及按钮,一是ListBox,重绘ListBox我们都知道(tongzhenhua也讲的很清楚)。关键是如何重绘可输入框及按钮. 通过重写OnPaint是没有用的。我固计重写OnPaint,只是重写它的父控件,而输入框只是它的一个子控件.所以重写OnPaint没用.虽然可以通过重写Form的OnPaint来重写。但这样无法重写到它的一些状态的改变。 protected override void OnPaint(System.EventArgs e){ base.OnPaint(e); .........} http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=YJA3BSW8-2CES-4L6Q-K7V7-JDBERJEJC4C5 net_lover(孟子E章):我们重绘ComboBox时有两部份,一是可输入框及按钮,一是ListBox,重绘ListBox我们都知道(tongzhenhua也讲的很清楚)。关键是如何重绘可输入框及按钮.向大师请教 //ComboBoxEx.csusing System;using System.Windows.Forms;using System.Drawing;namespace ComboBoxEx{ public class ComboBoxExItem { private int _no=0; private string _text=string.Empty; public ComboBoxExItem(int no,string text) { this._no=no; this._text=text; } public int No { get{return this._no;} set{this._no=value;} } public string Text { get{return this._text;} set{this._text=value;} } public override string ToString() { return this.Text; } } public class ComboBoxEx:ComboBox { public ComboBoxEx() { this.DrawMode=DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(DrawItemEventArgs e) { e.DrawBackground(); StringFormat format=new StringFormat(StringFormatFlags.NoWrap); format.LineAlignment=StringAlignment.Center; SolidBrush fontBrush=new SolidBrush(e.ForeColor); if((e.State&DrawItemState.ComboBoxEdit)!=0) { string text=this.Text; e.Graphics.DrawString(text,e.Font,fontBrush,new RectangleF(e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height),format); } else if(e.Index>=0 && e.Index<this.Items.Count) { string text=string.Empty; try { if(this.Items[e.Index] is ComboBoxExItem) text=(this.Items[e.Index] as ComboBoxExItem).No.ToString()+(this.Items[e.Index] as ComboBoxExItem).Text; else text=this.Items[e.Index].ToString(); } catch { text=string.Empty; } e.Graphics.DrawString(text,e.Font,fontBrush,new RectangleF(e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height),format); } fontBrush.Dispose(); } }} qqq123(qqq123):我们重绘ComboBox时有两部份,一是可输入框及按钮,一是ListBox,重绘ListBox我们都知道(tongzhenhua也讲的很清楚)。关键是如何重绘可输入框及按钮.向大师请教 if((e.State&DrawItemState.ComboBoxEdit)!=0) 表示重绘可输入框.按钮和滚动条以及边框我也不知道如何重绘。 qqq123(qqq123):大虾也有效果啊,是不是如下面的,不对请指出public class ComboBoxEx:ComboBox{ protected override void OnDrawItem(DrawItemEventArgs e) { //base.OnDrawItem (e); if((e.State&DrawItemState.ComboBoxEdit)!=0) { e.Graphics.FillRectangle(SystemBrushes.Info,e.Bounds); } }} 关于:MessageBox.Show() C#传感器接受数据 listbox中如何显示两行的子项 截获键盘或鼠标的消息并中断执行该消息 c# ETL工具取数据 SQL2005 反射中函数调用参数的传递问题??? 想请教各位一个问题... 哪位大哥买过这本书?《Visual C# 2005 程序开发与界面设计秘决》 大家看看STL之父对JAVA的评价 怎样实现查询?---在线等待 ★★★这样的Format怎么写?在线等.......... C#有没有处理IniFile的方法?
(如果说ComboBox的重载OnPaint也是这样那,Form与ComboBox不是有一样的重载函数了。)
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)//重载paint
{
....
}下面是个自画ComboBox的例子。没用重载。重载DrawItem与直接在DrawItem好像没什么区别。:(
private void Form1_Load(object sender, System.EventArgs e)
{
//创建字符串格式
fontArray .Add(new Font("Ariel" , 8 , FontStyle.Bold ));
fontArray .Add(new Font("Courier" , 8 , FontStyle.Italic));
fontArray .Add(new Font("Veranda" , 8 , FontStyle.Bold));
fontArray .Add(new Font("System" , 8 , FontStyle.Strikeout));
fontArray .Add(new Font("Century SchoolBook" , 8 , FontStyle.Underline));
fontArray .Add(new Font("Helevctia" , 8 , FontStyle.Italic));
//创建画刷
brushArray.Add(new SolidBrush(Color.Red));
brushArray.Add(new SolidBrush(Color.Blue));
brushArray.Add(new SolidBrush(Color.Green));
brushArray.Add(new SolidBrush(Color.Yellow));
brushArray.Add(new SolidBrush(Color.Black));
brushArray.Add(new SolidBrush(Color.Azure));
brushArray.Add(new SolidBrush(Color.Firebrick));
brushArray.Add(new SolidBrush(Color.DarkMagenta));
brushArray.Add(new SolidBrush(Color.DarkTurquoise));
brushArray.Add(new SolidBrush(Color.Khaki));
//画comboBox1,注意它要调用comboBox1_DrawItem来画
comboBox1.Items.Add("中国");
comboBox1.Items.Add("巴西");
comboBox1.Items.Add("哥斯达黎加");
comboBox1.Items.Add("土耳其");
comboBox1.Items.Add("韩国");
comboBox1.Items.Add("日本");
//画comboBox2,注意它要调用comboBox2_DrawItem来画
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
//画comboBox3,注意它要调用comboBox3_DrawItem来画
comboBox3.Items.Add("赵微");
comboBox3.Items.Add("舒淇"); } private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
//确定画布
Graphics g = e.Graphics ;
//绘制区域
Rectangle r = e.Bounds ;
Font fn = null ;
if ( e.Index >= 0 )
{
//设置字体、字符串格式、对齐方式
fn = (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.NoFocusRect))
{
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();
}
}
}
{
base.OnPaint(e); .........
}
我们重绘ComboBox时有两部份,一是可输入框及按钮,一是ListBox,重绘ListBox我们都知道(tongzhenhua也讲的很清楚)。关键是如何重绘可输入框及按钮.
向大师请教
using System;
using System.Windows.Forms;
using System.Drawing;namespace ComboBoxEx
{
public class ComboBoxExItem
{
private int _no=0;
private string _text=string.Empty; public ComboBoxExItem(int no,string text)
{
this._no=no;
this._text=text;
}
public int No
{
get{return this._no;}
set{this._no=value;}
}
public string Text
{
get{return this._text;}
set{this._text=value;}
}
public override string ToString()
{
return this.Text;
}
}
public class ComboBoxEx:ComboBox
{
public ComboBoxEx()
{
this.DrawMode=DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
StringFormat format=new StringFormat(StringFormatFlags.NoWrap);
format.LineAlignment=StringAlignment.Center;
SolidBrush fontBrush=new SolidBrush(e.ForeColor);
if((e.State&DrawItemState.ComboBoxEdit)!=0)
{
string text=this.Text;
e.Graphics.DrawString(text,e.Font,fontBrush,new RectangleF(e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height),format);
}
else if(e.Index>=0 && e.Index<this.Items.Count)
{
string text=string.Empty;
try
{
if(this.Items[e.Index] is ComboBoxExItem)
text=(this.Items[e.Index] as ComboBoxExItem).No.ToString()+(this.Items[e.Index] as ComboBoxExItem).Text;
else
text=this.Items[e.Index].ToString();
}
catch
{
text=string.Empty;
}
e.Graphics.DrawString(text,e.Font,fontBrush,new RectangleF(e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height),format);
}
fontBrush.Dispose();
}
}
}
我们重绘ComboBox时有两部份,一是可输入框及按钮,一是ListBox,重绘ListBox我们都知道(tongzhenhua也讲的很清楚)。关键是如何重绘可输入框及按钮.
向大师请教
按钮和滚动条以及边框我也不知道如何重绘。
大虾也有效果啊,是不是如下面的,不对请指出
public class ComboBoxEx:ComboBox
{
protected override void OnDrawItem(DrawItemEventArgs e)
{
//base.OnDrawItem (e);
if((e.State&DrawItemState.ComboBoxEdit)!=0)
{
e.Graphics.FillRectangle(SystemBrushes.Info,e.Bounds);
}
}
}