小新的技术天地

Make It Works !

博客园 首页 新随笔 联系 订阅 管理

作者:Niels Penneman   翻译:小新0574

原文链接:http://www.codeproject.com/cs/combobox/ImageCombo_NET.asp

Introduction

When I started programming in .NET, I wondered where the ImageCombo control had gone (I used to program in VB6). The .NET Framework didn't seem to have this functionality built-in, but it's very easy to add it.

当我开始使用.NET编程后,我感到很惊讶,ImageCombo 控件去哪了呢?(我以前使用VB6编程).NET框架似乎没有内建这个功能,不过要构造它很简单。

Using the code

The code consists of two classes: ImageCombo and ImageComboItem. If you want to use them in C#, just copy them into your project; for other .NET languages, you can add a reference to the library.

代码由两个类组成:ImageCombo ImageComboItem。如果你想在你的C#代码里使用它们,只要把它们复制到你的工程里就行了;对于其他.NET语言,你只要增加一个对这个类库的引用就行了。(译注:把这两个类编译成dll,在工程里引用就行了)

The control inherits from ComboBox and introduces one new member: the ImageList property, which doesn't require any further explanation. The control is owner drawn and there is a custom drawing method defined, so don't change its DrawMode property if you want to see the images.

这个控件继承自ComboBox ,增加了一个新的成员:ImageList 属性,这不需要多做解释了吧。这个控件是自绘的,控件定义一个自定义的绘制方法,所以如果你想看到图像,就不要改变它的DrawMode属性。

The ImageComboItem class inherits from Object. You can set a customForeColor and it has a Mark property which determines if the item is shown in bold font style (does not work if owner font is already bold).

ImageComboItem类继承自Object。你可以设置一个自己的ForeColor ,这个类有一个Mark属性,这个属性决定是否项使用粗体显示。(如果项已经是粗体了,这个属性就不会起作用)。

To add an item to an ImageCombo with text "Icon 0" and image index 0, use the following code:

要增加一个使用文本“Icon 0”和(ImageList里)索引值为0的图像的ImageCombo (项)。使用一下代码:

imageCombo.Items.Add(new ImageComboItem("Icon 0"0));

Code listing: ImageCombo class

using System;
using System.Drawing;

namespace System.Windows.Forms
{

    
public class ImageCombo : ComboBox
    
{
        
private ImageList imgs = new ImageList();

        
// constructor
        public ImageCombo()
        
{
            
// set draw mode to owner draw
            this.DrawMode = DrawMode.OwnerDrawFixed;    
        }


        
// ImageList property
        public ImageList ImageList 
        
{
            
get 
            
{
                
return imgs;
            }

            
set 
            
{
                imgs 
= value;
            }

        }


        
// customized drawing process
        protected override void OnDrawItem(DrawItemEventArgs e)
        
{
            
// draw background & focus rect
            e.DrawBackground();
            e.DrawFocusRectangle();

            
// check if it is an item from the Items collection
            if (e.Index < 0)

                
// not an item, draw the text (indented)
                e.Graphics.DrawString(this.Text, e.Font, 
                        
new SolidBrush(e.ForeColor), e.Bounds.Left + 
                        imgs.ImageSize.Width, e.Bounds.Top);

            
else
            
{
                
                
// check if item is an ImageComboItem
                if (this.Items[e.Index].GetType() == typeof(ImageComboItem)) 
                
{                                                            

                    
// get item to draw
                    ImageComboItem item = (ImageComboItem) 
                        
this.Items[e.Index];

                    
// get forecolor & font
                    Color forecolor = (item.ForeColor != 
                         Color.FromKnownColor(KnownColor.Transparent)) 
? 
                         item.ForeColor : e.ForeColor;
                    Font font 
= item.Mark ? new Font(e.Font, 
                         FontStyle.Bold) : e.Font;

                    
// -1: no image
                    if (item.ImageIndex != -1
                    
{
                        
// draw image, then draw text next to it
                        this.ImageList.Draw(e.Graphics, 
                           e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
                        e.Graphics.DrawString(item.Text, font, 
                           
new SolidBrush(forecolor), e.Bounds.Left + 
                           imgs.ImageSize.Width, e.Bounds.Top);
                    }

                    
else
                        
// draw text (indented)
                        e.Graphics.DrawString(item.Text, font, 
                            
new SolidBrush(forecolor), e.Bounds.Left + 
                            imgs.ImageSize.Width, e.Bounds.Top);

                }

                
else
                
                    
// it is not an ImageComboItem, draw it
                    e.Graphics.DrawString(this.Items[e.Index].ToString(), 
                      e.Font, 
new SolidBrush(e.ForeColor), e.Bounds.Left + 
                      imgs.ImageSize.Width, e.Bounds.Top);
                
            }


            
base.OnDrawItem (e);
        }

        
    }


}

Code listing: ImageComboItem class

using System;
using System.Drawing;

namespace System.Windows.Forms
{

    
public class ImageComboItem : object
    
{
        
// forecolor: transparent = inherit
        private Color forecolor = Color.FromKnownColor(
                KnownColor.Transparent);
        
private bool mark = false;
        
private int imageindex = -1;
        
private object tag = null;
        
private string text = null;        
        
        
// constructors
        public ImageComboItem()
        
{
        }


        
public ImageComboItem(string Text) 
        
{
            text 
= Text;    
        }


        
public ImageComboItem(string Text, int ImageIndex)
        
{
            text 
= Text;
            imageindex 
= ImageIndex;
        }


        
public ImageComboItem(string Text, int ImageIndex, bool Mark)
        
{
            text 
= Text;
            imageindex 
= ImageIndex;
            mark 
= Mark;
        }


        
public ImageComboItem(string Text, int ImageIndex, 
            
bool Mark, Color ForeColor)
        
{
            text 
= Text;
            imageindex 
= ImageIndex;
            mark 
= Mark;
            forecolor 
= ForeColor;
        }


        
public ImageComboItem(string Text, int ImageIndex, 
               
bool Mark, Color ForeColor, object Tag)
        
{
            text 
= Text;
            imageindex 
= ImageIndex;
            mark 
= Mark;
            forecolor 
= ForeColor;
            tag 
= Tag;
        }


        
// forecolor
        public Color ForeColor 
        
{
            
get 
            
{
                
return forecolor;
            }

            
set
            
{
                forecolor 
= value;
            }

        }


        
// image index
        public int ImageIndex 
        
{
            
get 
            
{
                
return imageindex;
            }

            
set 
            
{
                imageindex 
= value;
            }

        }


        
// mark (bold)
        public bool Mark
        
{
            
get
            
{
                
return mark;
            }

            
set
            
{
                mark 
= value;
            }

        }


        
// tag
        public object Tag
        
{
            
get
            
{
                
return tag;
            }

            
set
            
{
                tag 
= value;
            }

        }


        
// item text
        public string Text 
        
{
            
get
            
{
                
return text;
            }

            
set
            
{
                text 
= value;
            }

        }

        
        
// ToString() should return item text
        public override string ToString() 
        
{
            
return text;
        }


    }


}

About Niels Penneman

 

 I have learnt to program in VB3 when I was about 10. I also learnt Pascal, I moved on to VB5 and later VB6, tried some Visual J++ (1.1) and I have little knowledge of ANSI C. Nowadays I mostly use VB.NET and C# .NET.

     

 我在大概10岁的时候使用VB3开始学习编程。我也学了Pascal,转移到VB5,然后VB6,尝试了一些Visual J++ (1.1),我几乎不怎么了解ANSI C。现在我主要使用VB.NETC#.NET

 

I am currently studying science and maths in the last year of secondary education (maybe a weird way to explain it but it's because I don't know how YOUR educational system is organized ).

 

我现在在学习初中(secondary education)最后一年的自然科学与数学的课程(也许这样解释显得有些奇怪,但是是因为我不知道你们的教育体系是怎样组织的)。

posted on 2004-12-15 20:52  小新0574  阅读(2090)  评论(3编辑  收藏  举报