實現自繪 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中繪製圖形。效果如圖所示。
原始碼: