List Control and Property Grid with the MFC Feature Pack - Properties
(Page 4 of 4 )
A property grid contains multiple groups (or just one), and within that, multiple properties (or just one). Before we add any property, we need to create a group. For this, use the CMFCPropertyGridProperty class. Within this you can add items with the AddSubItem function. You can add a property group to the grid control with the AddProperty function.
//create a new item
CMFCPropertyGridColorProperty* pColorProp =
new CMFCPropertyGridColorProperty(
_T("Window Color"),RGB(210, 192, 254), NULL, _T("Specifies the default dialog color"));
// A little customization
pColorProp->EnableOtherButton(_T("Other..."));
pColorProp->EnableAutomaticButton(_T("Default"), ::GetSysColor(COLOR_3DFACE));
// Add a new group
CMFCPropertyGridProperty* pGroup3 = new CMFCPropertyGridProperty(_T("Misc"));
// Add the new item into this
pGroup3->AddSubItem(pColorProp);
// And finally add the end result to the grid control
m_wndPropList.AddProperty(pGroup3);

Creating a custom item is not that hard. You need to know which functions to overwrite and derive from one of the properties. We will create one that will have one accept and one cancel button near an edit box:
//Definition of a class
class CTwoButtonsProp : public CMFCPropertyGridProperty
{
public:
CTwoButtonsProp(const CString& strName,
const COleVariant& varValue);
protected:
virtual BOOL HasButton() const { return TRUE; }
virtual void AdjustButtonRect();
virtual void OnClickButton(CPoint point);
virtual void OnDrawButton(CDC* pDC, CRect rectButton);
CImageList m_images;
};
//Constructor
CTwoButtonsProp::CTwoButtonsProp(const CString& strName,
const COleVariant& varValue) : CMFCPropertyGridProperty(strName, varValue)
{
// Load image and create a list from it
CBitmap bmp;
bmp.LoadBitmap(IDB_BUTTONS);
m_images.Create(14, 14, ILC_MASK | ILC_COLOR8, 0, 0);
m_images.Add(&bmp, RGB(255, 0, 255));
}
// Change the size of the button
void CTwoButtonsProp::AdjustButtonRect()
{
CMFCPropertyGridProperty::AdjustButtonRect();
m_rectButton.left -= m_rectButton.Width();
}
//Action to take upon button click
void CTwoButtonsProp::OnClickButton(CPoint point)
{
BOOL bIsLeft = point.x < m_rectButton.CenterPoint().x;
AfxMessageBox(bIsLeft ? _T("Left button clicked") :
_T("Right button clicked"));
}
// How to draw the buttons
void CTwoButtonsProp::OnDrawButton(CDC* pDC, CRect rectButton)
{
for (int i = 0; i < 2; i++)
{
CMFCToolBarButton button;
CRect rect = rectButton;
if (i == 0)
{
rect.right = rect.left + rect.Width() / 2;
}
else
{
rect.left = rect.right - rect.Width() / 2;
}
// Acquire some visual studio style
CMFCVisualManager::AFX_BUTTON_STATE state = CMFCVisualManager::ButtonsIsHighlighted;
CMFCVisualManager::GetInstance()-> OnFillButtonInterior(pDC, &button, rect, state);
m_images.Draw(pDC, i, CPoint(rect.left, rect.top), ILD_NORMAL);
CMFCVisualManager::GetInstance()-> OnDrawButtonBorder(pDC, &button, rect, state);
}
}
//Finally in the initialization create an instance of this
pGroup5->AddSubItem(new CTwoButtonsProp(_T("2 Buttons"), _T("text")));
The result speaks for itself:

And I will show you some of the other properties and styles you can create. For now this will be only in pictures:















This will be all for this piece on the MFC Feature Pack. I would like to thank you for reading my article. I hope you have learned a lot and you will rate my article as you think it's worth. If you have any questions feel free to put them up here on the blog or over our friendly community at DevHardware. Live With Passion!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |