开发者

Binding a CPen to a list box

开发者 https://www.devze.com 2023-03-27 10:44 出处:网络
Does anyone know how to bind a CPen object to a listbox in VS2005 C++? Can I do it as a ToString with some sort of conversion?

Does anyone know how to bind a CPen object to a listbox in VS2005 C++?

Can I do it as a ToString with some sort of conversion?

I am creating a custom list of different pens for the user to select.

Thanks.

    COLORREF  rgbRED = (255,0,0);
    CPen penRed(PS_SOLID,3,rgbRED);

    CDialog::OnInitDialog();
    ShowWindow(SW_SHOW);
    UpdateData();
    lbLineWeight.Ins开发者_如何学CertString(penRed);


2 options.

  • (simple) Use a normal CListBox with strings as the items, and keep the link between the string to the actual CPen as free functions (or member of some other classes) and you will have to do a one-to-one association between the current selected item (usually an index number) and the CPen information you have.

  • (a bit more complex) Derive your own class from CListBox and keep the CPen data internally, you will still have to keep a list of valid CPen in that new class, and do the one-to-one association between the selected item and the actual CPen; as a bonus you can make you derived CListBox owner-drawn and instead of using string, you could draw some sort of representation of each pen in the list items.

Another tought, you could add the CPen as a user data to each CListBox item (CListBox::SetItemData) to make the link between the item and the actual item a bit more easy.

Good luck.

Max.


Assuming I understand you correctly you want to have a CListBox which allows the user to select a CPen for use elsewhere.

I would probably make a little helper class:

struct PenHelper
{
   CString m_displayName;
   LOGPEN m_penProps;

   bool CreatePen(CPen* pPen)
   {
       return pPen->CreatePenIndirect(&m_penProps) == 1;
   }
};

The idea being you could have a container like std::map of multiple PenHelper each with a names like "Solid Red" and a corresponding LOGPEN struct with properties that match the name. In the CListBox you add the display name string. When they select one you can look it up by name and use the create function to actually make the corresponding CPen

Just one of a million ways to skin a cat.

Edit: Quick note. In order to handle ON_LBN_SELCHANGE in your message map for when they make a selection in your CListBox make sure you gave it the LBS_NOTIFY style in the Create call otherwise it won't fire.

0

精彩评论

暂无评论...
验证码 换一张
取 消