在ComboBox中绘制图形

本文介绍如何在ComboBox中绘制图形,以实现更加个性化的效果。通过设置ComboBox的DrawMode为OwnerDrawVariable,并利用DrawItem和MeasureItem事件,可以实现在下拉列表中绘制不同样式的填充图案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。

作者:Zealot
为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。效果如图所示。

DrawComboBoxItem1.JPG       DrawComboBoxItem2.JPG

DrawComboBoxItem3.JPG

代码如下:

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Drawing.Drawing2D;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
None.gifpartial 
class Form2 : Form
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public Form2()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        InitializeComponent();
InBlock.gif        
this.comboBox1.Items.Add("Solid Brush");
InBlock.gif        
this.comboBox1.Items.Add("Horizontal");
InBlock.gif        
this.comboBox1.Items.Add("Min");
InBlock.gif        
this.comboBox1.Items.Add("Vertical");
InBlock.gif        
this.comboBox1.Items.Add("Forward Diagonal");
InBlock.gif        
this.comboBox1.Items.Add("Backward Diagonal");
InBlock.gif        
this.comboBox1.Items.Add("Cross");
InBlock.gif        
this.comboBox1.Items.Add("Large Grid");
InBlock.gif        
this.comboBox1.Items.Add("Max");
InBlock.gif        
this.comboBox1.Items.Add("Diagonal Cross");
InBlock.gif
InBlock.gif        
this.comboBox1.SelectedIndex = 0;
InBlock.gif        
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
InBlock.gif        
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
InBlock.gif        
this.comboBox1.DropDownWidth = 190;
InBlock.gif        
this.comboBox1.DropDownHeight = 300;
InBlock.gif        
this.comboBox1.MeasureItem += new MeasureItemEventHandler(comboBox1_MeasureItem);
InBlock.gif        
this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string displayText = this.comboBox1.Items[e.Index].ToString();
InBlock.gif        SizeF stringSize 
= e.Graphics.MeasureString(displayText, this.Font);
InBlock.gif        e.ItemHeight 
= (int)stringSize.Height;
InBlock.gif        e.ItemWidth 
= (int)stringSize.Width;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
private String RemoveSpaces(String str1)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int start;
InBlock.gif        
int at;
InBlock.gif        
int end;
InBlock.gif
InBlock.gif        String str2 
= String.Copy(str1);
InBlock.gif
InBlock.gif        at 
= 0;
InBlock.gif        end 
= str2.Length - 1;
InBlock.gif        start 
= 0;
InBlock.gif
InBlock.gif        
while ((start <= end) && (at > -1))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// start+count must be a position within str2.
InBlock.gif
            at = str2.IndexOf(" ", start);
InBlock.gif            
if (at == -1break;
InBlock.gif            str2 
= str2.Remove(at, 1);
InBlock.gif            start 
= at + 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return str2;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private HatchStyle getHatchStyle(String s)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        String str 
= RemoveSpaces(s);
InBlock.gif        
return (HatchStyle)Enum.Parse(typeof(HatchStyle), str, true);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//throw new NotImplementedException();
InBlock.gif
        e.DrawBackground();
InBlock.gif
InBlock.gif        Rectangle r 
= e.Bounds;
InBlock.gif
InBlock.gif        
if (e.Index != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (e.Index > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Rectangle rd 
= r;
InBlock.gif                rd.Width 
= rd.Left + 25;
InBlock.gif
InBlock.gif                Rectangle rt 
= r;
InBlock.gif                r.X 
= rd.Right;
InBlock.gif
InBlock.gif                
string displayText = this.comboBox1.Items[e.Index].ToString();
InBlock.gif                HatchStyle hs 
= this.getHatchStyle(displayText);
InBlock.gif
InBlock.gif                
using (HatchBrush b = new HatchBrush(hs,e.ForeColor,e.BackColor))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    e.Graphics.FillRectangle(b,rd);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                StringFormat sf 
= new StringFormat();
InBlock.gif                sf.Alignment 
= StringAlignment.Near;
InBlock.gif
InBlock.gif                
using(SolidBrush sb = new SolidBrush(Color.White))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                        
if((e.State & DrawItemState.Focus) == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sb.Color 
= SystemColors.Window;
InBlock.gif                            e.Graphics.FillRectangle(sb,r);
InBlock.gif                            sb.Color 
= SystemColors.WindowText;
InBlock.gif                            e.Graphics.DrawString(displayText,
this.Font,sb,r,sf);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            sb.Color 
= SystemColors.Highlight;
InBlock.gif                            e.Graphics.FillRectangle(sb,r);
InBlock.gif                            sb.Color 
= SystemColors.HighlightText;
InBlock.gif                            e.Graphics.DrawString(displayText,
this.Font,sb,r,sf);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
   // if (e.Index > 0)
InBlock.gif
            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using(SolidBrush sb = new SolidBrush(Color.White))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if((e.State & DrawItemState.Focus) == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Color 
= SystemColors.Window;
InBlock.gif                        e.Graphics.FillRectangle(sb,e.Bounds);
InBlock.gif                        
string displayText = this.comboBox1.Items[e.Index].ToString();
InBlock.gif                        sb.Color 
= SystemColors.WindowText;
InBlock.gif                        e.Graphics.DrawString(displayText,
this.Font,sb,e.Bounds);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        sb.Color 
= SystemColors.Highlight;
InBlock.gif                        e.Graphics.FillRectangle(sb,e.Bounds);
InBlock.gif                        
string displayText = this.comboBox1.Items[e.Index].ToString();
InBlock.gif                        sb.Color 
= SystemColors.HighlightText;
InBlock.gif                        e.Graphics.DrawString(displayText,
this.Font,sb,e.Bounds);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            e.DrawFocusRectangle();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

转载于:https://www.cnblogs.com/hzuIT/articles/695473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值