combobox:制作漂亮的Visual C#组件ComboBox来源: 发布时间:星期四, 2009年2月12日 浏览:171次 评论:0
组合框是组成Windows窗口常见 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 图1 今天我们就来给组合框美容 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 例1:本例中我们在 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 建立 ![]() ![]() ![]() ![]() ![]() 第 2步:界面设计 本例界面比较简单 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 图2 第 3步:Control控件属性设置 (1)把 3个label ![]() (2) 3个comboBox ![]() DrawMode:OwnerDrawFixed; DropDownStyle:DropDownList; 注:这两个属性只有如此设置才能保证我们对组合框进行个性化改造 ![]() ![]() ![]() ![]() ![]() ![]() ![]() (3)通过imageList1 ![]() ![]() 第 4步:编程 介绍说明: 当我们通过组合框 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() (1)在组合框条目中显示文本时都是利用grphics类 ![]() ![]() ![]() ![]() ![]() ![]() ①public void DrawString( ![]() ![]() 在指定位置并且用指定 ![]() ![]() ![]() ![]() ②public void DrawString( ![]() 在指定矩形并且用指定 ![]() ![]() ![]() ![]() ③public void DrawString( ![]() ![]() 使用指定 StringFormat 对象 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ④public void DrawString( ![]() 使用指定 StringFormat 对象 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ⑤public void DrawString( ![]() 在指定位置并且用指定 ![]() ![]() ![]() ![]() ⑥public void DrawString( ![]() 使用指定 StringFormat 对象 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 在本例中我们注意使用第④种 ![]() (2)在组合框画矩形时大都使用graphics类 ![]() ![]() ![]() ![]() FillRectangle(brush_name ![]() (3)comboBox1~comboBox3 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() BackColor:获取所绘制 ![]() ![]() ![]() Bounds:获取表示所绘制项 ![]() ![]() ![]() Font:获取分配给所绘制项 ![]() ![]() ![]() ForeColor: 获取所绘制项 ![]() ![]() Graphics:获取要在其上绘制项 ![]() ![]() Index:获取所绘制项 ![]() ![]() State:获取所绘制项 ![]() ![]() 有了以上 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ArrayList brushArray = ![]() ![]() ArrayList fontArray = ![]() ![]() ![]() ![]() ![]() ![]() ![]() private void Form1_Load(object sender, ![]() { //创建 ![]() fontArray .Add( ![]() fontArray .Add( ![]() fontArray .Add( ![]() fontArray .Add( ![]() ![]() fontArray .Add( ![]() fontArray .Add( ![]() //创建画刷 brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() brushArray.Add( ![]() //画comboBox1 ![]() ![]() comboBox1.Items.Add(\"中国\"); comboBox1.Items.Add(\"巴西\"); comboBox1.Items.Add(\"哥斯达黎加\"); comboBox1.Items.Add(\"土耳其\"); comboBox1.Items.Add(\"韩国\"); comboBox1.Items.Add(\"日本\"); //画comboBox2 ![]() ![]() 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.Items.Add(\"赵微\"); comboBox3.Items.Add(\"舒淇\"); } 问题:仅从Form_Load ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 图3 comboBox1:格式变化 ![]() ![]() 图4 comboBox2:带有图形 ![]() ![]() 图5:comboBox3:带有图像 ![]() 我们看看comboBox1 ![]() ![]() ![]() private void comboBox1_DrawItem(object sender, ![]() { //确定画布 Graphics g = e.Graphics ; //绘制区域 Rectangle r = e.Bounds ; Font fn = null ; ![]() { //设置字体、 ![]() fn = (Font)fontArray[e.Index]; ![]() ![]() StringFormat sf = ![]() ![]() sf.Alignment = StringAlignment.Near; //根据区别 ![]() ![]() ![]() ![]() { e.Graphics.FillRectangle( ![]() e.Graphics.DrawString( s , fn , ![]() e.DrawFocusRectangle ![]() } ![]() { e.Graphics.FillRectangle( ![]() e.Graphics.DrawString( s , fn , ![]() e.DrawFocusRectangle ![]() } } } 再来看看comboBox2 ![]() ![]() ![]() private void comboBox2_DrawItem(object sender, ![]() { Graphics g = e.Graphics ; Rectangle r = e.Bounds ; ![]() { //设置 ![]() ![]() Rectangle rd = r ; rd.Width = rd.Left + 20 ; Rectangle rt = r ; r.X = rd.Right ; //用区别 ![]() SolidBrush b = (SolidBrush)brushArray[e.Index]; g.FillRectangle(b , rd); //设置 ![]() ![]() StringFormat sf = ![]() ![]() sf.Alignment = StringAlignment.Near; ![]() ![]() { // ![]() e.Graphics.FillRectangle( ![]() //显示 ![]() e.Graphics.DrawString( b.Color.Name, ![]() ![]() //绘制取得焦点时 ![]() e.DrawFocusRectangle ![]() } ![]() { e.Graphics.FillRectangle( ![]() e.Graphics.DrawString( b.Color.Name, ![]() ![]() e.DrawFocusRectangle ![]() } } } 最后我们看看comboBox3 ![]() ![]() ![]() private void comboBox3_DrawItem(object sender, ![]() { Graphics g = e.Graphics ; Rectangle r = e.Bounds ; Size imageSize = imageList1.ImageSize; Font fn = null ; ![]() { fn = (Font)fontArray[0]; ![]() ![]() StringFormat sf = ![]() ![]() sf.Alignment = StringAlignment.Near; ![]() ![]() { //画条目背景 e.Graphics.FillRectangle( ![]() //绘制图像 imageList1.Draw(e.Graphics, r.Left, r.Top,e.Index); //显示 ![]() e.Graphics.DrawString( s , fn , ![]() //显示取得焦点时 ![]() e.DrawFocusRectangle ![]() } ![]() { e.Graphics.FillRectangle( ![]() imageList1.Draw(e.Graphics, r.Left, r.Top,e.Index); e.Graphics.DrawString( s , fn , ![]() e.DrawFocusRectangle ![]() } } } 看到这儿 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 在本文快要结果的前我们还是来看看应用 ![]() ![]() ![]() ![]() ![]() { Form frm= ![]() ![]() frm.ShowDialog ![]() } 0
相关文章读者评论发表评论 |