实现自绘 ComboBox 源代码 (C#)

 

 Implementing an OwnerDrawn ComboBox

源程序下载

 

Introduction
Hi there, I've been waiting for ages and ages for someone to kindly post the code for an owner drawn ComboBox but alas, to no avail. That's right, I'm your average, lowly leech and I've been stealing code off CodeProject for a long time now. Well looks like this time I actually had to go write something on my own. My guess is it was too simple a problem for most of you to bother with. But please do keep writing articles for relative beginners because most of us have to start with the simple things first. Luckily, writing the code turned out to be childs play with the .NET framework.

 

Ok, lets begin. First thing to do is drop a ComboBox onto a form and change it's DrawMode property to OwnerDrawFixed. This will mean that all the items appearing in the ComboBox will have the same dimensons. If you're going to draw items in the ComboBox that are of variable sizes then you would change the property to OwnerDrawVariable and be forced to deal with a MeasureItem event. I won't be covering that here. Next, create a DataSource for the ComboBox, as well as a corresponding collection of images. In my case I simply used two Arrays The following code should now be easy enough to follow

 

Conclusion
Ok, that's it, my very first CodeProject article, go easy on me. The code started out quite long and tedious but I whittled it down as my understanding of DrawItemEventArgs became a bit clearer. I'm not too sure if it's cool to have the ComboBox default backcolor to be anything other than white. But that seems to be more a matter of taste.  

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author
Senkwe Chanda


Nada
Occupation: Web Developer
Location: South Africa

 

The Code

Conclusion
Ok, that's it, my very first CodeProject article, go easy on me. The code started out quite long and tedious but I whittled it down as my understanding of DrawItemEventArgs became a bit clearer. I'm not too sure if it's cool to have the ComboBox default backcolor to be anything other than white. But that seems to be more a matter of taste.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

 

About the Author
Senkwe Chanda Nada
Occupation: Web Developer
Location: South Africa   

 

 

--------------------------------------------------------------------------------------

                  绘 ComboBox 的Item(C#)

 

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

源代码:

 

 

  

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值