I have CMFCPropertyGrid
control to which I have added a Combobox property with CMFCPropertyGridProperty::AddOption()
:
foreach(auto i, list)
{
str.Format(_T("<%s> %s"), i.first, i.second);
pProp->AddOption(str);
}
Now I need to do some code when the user drops down the list in this CMFCPropertyGrid
.
I would use CBN_DROPDOWN
if it were a combobox control in dialog window (it has ID). But how can I do that in case of CMFCPropertyGrid
?
CodePudding user response:
For a combobox-style CMFCPropertyGridProperty
, the framework calls the OnClickButton
member when the user clicks the down-arrow button near the top-right of the control (which causes the dropdown list to appear).
So, create a class for your control (derived from CMFCPropertyGridProperty
) and override the OnClickButton
member to add the code you want to run. Here's an outline of what you could do:
class MyComboControl : public CMFCPropertyGridProperty
{
public:
// Constructor: add some options ...
MyComboControl(void) : CMFCPropertyGridProperty(L"Choice:", _variant_t(L""), L"Description Text") {
AllowEdit(FALSE);
for (int i = 0; i < 3; i) {
CString text;
text.Format(L"Option #%d", i);
AddOption(text.GetString());
if (i == 0) SetValue(text.GetString());
}
}
// Override to handle the dropdown activation ...
void OnClickButton(CPoint pt) override {
//... Add any pre-drop code here.
CMFCPropertyGridProperty::OnClickButton(pt); // Call base class
//... Add any post-drop code here
}
};
CodePudding user response:
MFC will notify the parent window for property changes throught AFX_WM_PROPERTY_CHANGED
Sent to the owner of the property grid control (
CMFCPropertyGridCtrl
) when the user changes the value of the selected property.
BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
ON_REGISTERED_MESSAGE(AFX_WM_PROPERTY_CHANGED, OnPropertyChanged)
END_MESSAGE_MAP()
afx_msg LRESULT CMyDialog::OnPropertyChanged(WPARAM wparam, LPARAM lparam)
{
if (!lparam)
return 0;
auto prop = reinterpret_cast<CMFCPropertyGridProperty*>(lparam);
if (prop != m_pProp)
{
auto str_variant = prop->GetValue();
CString str;
if(str_variant.vt == VT_BSTR)
str = CString(str_variant.bstrVal);
}
return 0;
}
If you have multiple control with the grid control, you want to declare pProp
as a class member m_pProp
so its value can be tested.