Completely Customizable Properties Window

Friday Nov 24th 2000 by Brett R. Mitchell
Share:

Properties window with combo boxes that communicates with owner when properties are changed

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
Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved