Completely Customizable Properties Window

A recently needed a properties window that was completely customizable. It also had to support editable combo boxes and send back messages when a property had been altered. I did find a couple other property windows available, but they didn't provide the customization that I needed. Instead of hacking up someone else's code to add my features, I decided to write another one with the functionality I wanted. So here it is...

The control is based off of CListBox, therefore all CListbox functions are available. The following functions are also available to change the way the properties window looks:

void	SetFont(CFont* pFont);
void	SetBkColor(COLORREF crColor);
void	SetPropertyBkColor(COLORREF crColor);
void	SetHighlightColor(COLORREF crColor);
void	SetLineStyle(COLORREF crColor, int nStyle = PS_SOLID);
void	SetBoldSelection(BOOL bBoldSelection);
void	SetTextColor(COLORREF crColor);
void	SetTextHighlightColor(COLORREF crColor);
void	SetPropertyTextColor(COLORREF crColor);

The next group of functions provide the ability to add the different properties to the control. It currently supports Combo Boxes, text fields, toggle fields, font selection fields, color selection fields and Static fields.
BOOL	AddString(CString csText);

BOOL	AddString(CString csText, int nType, 
                CString csData, int nPropertySelected = 0, 
                int nAlignment = DT_LEFT, 
                BOOL bComboEditable = FALSE, 
                BOOL bComboSorted = FALSE);

BOOL	AddString(CString csText, COLORREF crColor, 
                int nAlignment = DT_LEFT);

BOOL	AddString(CString csText, CFont* pFont, 
                int nAlignment = DT_LEFT);

The only one that probably needs explanation is the second 'AddString' function. The 'nType' parameter can be either ID_PROPERTY_TEXT or ID_PROPERTY_STATIC or ID_PROPERTY_BOOL or ID_PROPERTY_PATH or ID_PROPERTY_COMBO_LIST. Font and Color should use the other two 'AddString' functions. The 'csData' parameter needs to be formatted delimited by the '!' character. (see example below). The other parameter that needs explanation is 'nPropertySelected'. This is used to set the selected item in a combobox or set the bool value to true or false. (one again, see example below)

In order to get the data back out of the properties, use the following functions.

bool	GetProperty(int nItem, CString* pText);	
bool	GetProperty(int nItem, bool* bValue);	
bool	GetProperty(int nItem, COLORREF* crColor);	   
bool	GetProperty(int nItem, LOGFONT*	LogFont);
bool	GetProperty(int nItem, CStringArray* pArray, 
                  int* SelectedItem = NULL);
bool	GetProperty(int nItem, int* SelectedItem, 
                  CString* csText = NULL);

One final feature is a message it sends back to the parent window when a property has been changed. The ID for the message is ID_PROPERTY_CHANGED. Just trap this message in the parent with the standard ON_MESSAGE MACRO. The first parameter will be the item that changed. The second parameter will be this items type. (COMBO, TEXT, Etc. ) Here's an example.

Declaration of the Message to trap in the BEGIN_MESSAGE_MAP:

ON_MESSAGE(ID_PROPERTY_CHANGED, OnPropertyChanged)

Declaration of the function:

LONG CParentClass::OnPropertyChanged(UINT wItemChanged, 
                                     LONG lPropertyType)

Declaration of the function in the .h parent class file:

afx_msg LONG OnPropertyChanged(UINT wItemChanged, LONG lPropertyType);

Here's an example of how to create, set GUI contraints, add properties, and get values from the control.

// Create the list box font
m_pListBoxFont = new CFont();
m_pListBoxFont->CreateFont(14, 0, 0, 0, FW_NORMAL, 0, 0, 0,
                           ANSI_CHARSET, OUT_TT_PRECIS,
                           CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
                           VARIABLE_PITCH | FF_SWISS,
                           "arial");	
	
// Create a Property Font
m_pPropertyFont = new CFont();
m_pPropertyFont->CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0, 
                            ANSI_CHARSET, OUT_TT_PRECIS, 
                            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, 
                            VARIABLE_PITCH | FF_SWISS, "system");	
	
// Create the Control
m_pPropertyListCtrl = new CPropertyListCtrl();
m_pPropertyListCtrl->Create(WS_CHILD|WS_BORDER|WS_VISIBLE
                            |LBS_NOTIFY|WS_VSCROLL|WS_HSCROLL
                            |LBS_HASSTRINGS|LBS_OWNERDRAWFIXED, 
                            CRect(200,100,550,300), this, 
                            ID_PROPERTYLIST);
	
// Set some attributes (Completly Optional)
m_pPropertyListCtrl->SetFont(m_pListBoxFont);
m_pPropertyListCtrl->SetBkColor(RGB(214,227,239));
m_pPropertyListCtrl->SetTextColor(RGB(74,109,132));
m_pPropertyListCtrl->SetTextHighlightColor(RGB(80,80,80));
m_pPropertyListCtrl->SetHighlightColor(RGB(246,246,220));
m_pPropertyListCtrl->SetPropertyBkColor(RGB(255,255,255));
m_pPropertyListCtrl->SetPropertyTextColor(RGB(0,0,192));
m_pPropertyListCtrl->SetBoldSelection(TRUE);
m_pPropertyListCtrl->SetLineStyle(RGB(74,109,132), PS_SOLID);

// Add the Properties
m_pPropertyListCtrl->AddString("Static Text",
                               ID_PROPERTY_STATIC,
                               "C:\\Windows", 0, DT_RIGHT);

m_pPropertyListCtrl->AddString("Text Property",
                               ID_PROPERTY_TEXT,
                               "This is some text", 0, DT_RIGHT);

m_pPropertyListCtrl->AddString("Toggle Property",
                               ID_PROPERTY_BOOL,
                               "True!False", 0, DT_RIGHT);

m_pPropertyListCtrl->AddString("Color Property",
                               RGB(57,
                               09,165), DT_RIGHT);

m_pPropertyListCtrl->AddString("Font Property",
                               m_pPropertyFont,
                               DT_RIGHT);

m_pPropertyListCtrl->AddString("File Property",
                               ID_PROPERTY_PATH,
                               "D:\\Code\\C++\\32bit\\"
                               "Programs\\Blackjack\\Images"
                               "\\Dealer.bmp!Bitmap Files "
                               "(*.BMP)|*.bmp|All Files (*.*)"
                               "|*.*|", 0, DT_RIGHT);

m_pPropertyListCtrl->AddString("Combobox Property",
                               ID_PROPERTY_COMBO_LIST,
                               "Porsche!BMW!Mercedes!Lotus!Ford!"
                               "Chevy!Kia!Dodge!Nissan!Toyota", 
                               2, DT_RIGHT, TRUE, TRUE);


// Get the Properties
// Text and Path
CString csText;
m_pPropertyListCtrl->GetProperty(0, &csText);	

// Toggle
bool bValue;
m_pPropertyListCtrl->GetProperty(1, &bValue);
	
// Color
COLORREF crColor;
m_pPropertyListCtrl->GetProperty(2, &crColor);

// Font
LOGFONT lf;
m_pPropertyListCtrl->GetProperty(3, &lf);

// Combo Box
CStringArray Items;
m_pPropertyListCtrl->GetProperty(5, &Items);

// Combo Box (get selected item index and text)
int nSelectedItem;
m_pPropertyListCtrl->GetProperty(5, &nSelectedItem, &csText);

Downloads

Download source - 9 Kb
Download demo project - 26 Kb


Comments

  • some thing wrong with visual style

    Posted by c_m_ on 08/23/2010 07:15am

    I find the visual style will change the behave of the listbox,with the visual style the OnSelchange and OnDblclk function will be trigered when m_pCurItem==0 and this will crash the program ,without the visual style this will never happen.

    Reply
  • Exactly what I needed!

    Posted by Legacy on 02/04/2004 12:00am

    Originally posted by: Mouse

    thank you very much for sharing this wonderful code!
    -andy

    Reply
  • thanks

    Posted by Legacy on 08/17/2003 12:00am

    Originally posted by: Dr. Evil

    fantastic control,
    great work. thanks heaps.
    pity the comments are populated
    by idiots trying to promote stuff to
    buy though. advertise somewhere else
    ****heads.

    Reply
  • Here's another excellent properties control, one of the best !!!

    Posted by Legacy on 07/29/2003 12:00am

    Originally posted by: Scott Evans

    I just ran across this control the other day, very nice!
    http://www.codejock.com/products/propertygrid/

    Reply
  • Multi-line text?

    Posted by Legacy on 03/16/2003 12:00am

    Originally posted by: Greg Ennis

    How can I do a multi-line text property?

    Reply
  • How do you change an existing properties value?

    Posted by Legacy on 10/10/2002 12:00am

    Originally posted by: MarkgM

    Using CPropertyListCtrl, is there a way to programmatically change the text
    
    in a text property after that property has been created?
    Like:
    // Create the property
    m_FieldList->AddString("First Text Property",
    ID_PROPERTY_TEXT,
    "theValue", 0, DT_RIGHT);
    And later:
    // Set the edit control's value
    ??? how to get to it?

    Reply
  • AGreat control... but there is a problem with the ComboBox.

    Posted by Legacy on 07/31/2002 12:00am

    Originally posted by: Gazza

    Great control... but there is a problem with the ComboBox.
    When a combo-item is clicked on, the pull-down list only appears within the constraints of the main list-control window, and does not overlay outside it.

    Reply
  • Another great properties control

    Posted by Legacy on 01/12/2002 12:00am

    Originally posted by: Noemie Bigard

    Found this fresh one at http://www.aircom.org
    Noemie

    Reply
  • Bug when reset() is called

    Posted by Legacy on 10/24/2001 12:00am

    Originally posted by: Hans van Amstel

    When a reset is called on the CPropertyListCtrl it results in a crash in the DrawItem method.
    
    

    This can be fixed by adding a call to ResetContent() at the end of the Reset() method.

    void CPropertyListCtrl::Reset()
    {

    // Clear the List
    POSITION Pos = m_Items.GetHeadPosition();
    while(Pos)
    {
    m_pCurItem = (PROPERTYITEM*)m_Items.GetNext(Pos);
    if(m_pCurItem->pBrush)
    delete m_pCurItem->pBrush;
    delete m_pCurItem;
    }
    m_Items.RemoveAll();

    // Reset the listbox content
    ResetContent();

    }

    Reply
  • ExPropertiesList

    Posted by Legacy on 09/29/2001 12:00am

    Originally posted by: Mike Philis

    Do you need a powerfull PropertiesList? Check this out:
    
    http://www.exontrol.com/sg.jsp?content=products/expropertieslist

    Mike
    www.exontrol.com

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds