multi column combo box

http://www.codeguru.com/cpp/controls/combobox/dropdown/article.php/c8939/LightWeight-MultiColumn-OwnerDrawn-ComboBox-with-AutoCompletion.htm

Light-Weight, Multi-Column, Owner-Drawn ComboBox with AutoCompletion!

Here is one more article on ComboBox. This article presents yet another customized combo-box with the following features:

  • AutoCompletion as you type on; it is similar to the WizardBar in VC++ Developer Studio.
  • Each item can have one more column to supply extra information to the end user. The text (or say comment) is not part of the combobox, and does not participate in the AutoCompletion process. Neither it is shown when the combo-box is not dropped down.
  • It uses owner-drawing. Backed with this, it also supports to change various colors (text, background, and so forth) and the font itself.

Despite all these facts, I made it as lightweight as possible.

The CAutoCombo Class

The class CAutoCombo is inherited, as is obvious, from the MFC CComboBox class. The necessary requirements for using this class are as follows:

  1. You need to sub-class your control by passing its resource ID.
  2. The following properties must be set; otherwise, it will assert:
    • Type must be "DropDown"
    • OwnerDrawing: Fixed
    • "Has Strings" enabled
    • "Sort" should be disabled
  3. After you perform subclassing, you can add items to it and perform other customizations.

Here is a generalized class definition:

  1. classCAutoCombo:publicCComboBox{
  2. public:
  3. voidAddString(CString strItem);
  4. voidAddString(CString strText,CString strExtra);
  5. CAutoCombo();
  6. CStringGetString(int nSel=-1);// -1 means current selection
  7.  
  8. intGetCurSel()const;
  9. voidSetCurSel(int nSelect);
  10. voidDeleteString(int nItem)// Zero-based position
  11. voidResetContent();
  12. intSubclassDlgItem(UINT nID,CWnd* pParent,bool bTabbed=false,
  13. int nTabPos=160);
  14.  
  15. intGetCount()const;
  16.  
  17. voidSetColor(COLORREF nColor,AutoComboColor nFor=NormalText);
  18. voidSetFontEx(CString strFace,bool bBold=true);
  19. };

A Bit of Detail on Important Methods

The first thing, as was mentioned, is to subclass the control. To do this, you call the following:

  1. mCombo.SubclassDlgItem(IDC_SAMPLE,this,// mCombo is of type
  2. true,100);// CAutoCombo

where the first two parameters need no discussion. Third parameter specifies whether or not multi-column mode will be used. The fourth specifies (valid if third param is true), the distance in pixels where should comment (second column) begin. Right now, only left-aligned columns are supported. After doing this, you can add items by using:

  1. mCombo.AddString("Sun","Our global energy!");

where first param is the actual combo-box's string and the second string will be displayed on the right when the combo-box is dropped down. As soon as the user begins typing, it will be dropped down. The class also hides improper usage of CComboBox::AddString by overriding it. The second version can be used to supply comment-text by separating it throught the tab character ("\t"):

  1. mCombo.AddString("Sun\tOur global energy!");

Only this single argumented version be used if multi-column is disabled (the third parameter of SubclassDlgItem is false); otherwise, it will ASSERT.

Changing Fonts

To change the font, you just need to call SetFontEx:

  1. mCombo.SetFontEx("Palatino Linotype");

This sets the specified font. The second parameter of this method (which is, by default, false) expresses whether a bold font should be used. Right now, all items share the same font.

Changing Colors

To change colors, one needs to call a single method, SetColor:

  1. // Sets color for comments part, when item is selected
  2. // Sets normaltext color, does not affect selected mode color
  3. mContinents.SetColor(RGB(0,0,128),SelectedComment);
  4. mContinents.SetColor(RGB(0,128,255),NormalText);

Here, the first argument is manifest. The second argument tells "for which" color is to be changed. It can be from:

  1. enumAutoComboColor{
  2. NormalText,NormalComment,
  3. SelectedText,SelectedComment,
  4. Selection
  5. };

Currently, all items share the color property, depending on the selection mode.

<:section sizset="0" sizcache="81">

Downloads

转载于:https://www.cnblogs.com/MarvinGeng/archive/2012/07/23/2605095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值