问题
I'm adding properties to an object of type CMFCPropertyGridCtrl
like this:
myPropertyListCtrl.AddProperty(
new CMFCPropertyGridProperty(
_T("Name"),
foo.GetName())
);
The result is that only the second column is visible but not the first that should contain "Name".
- I found
CMFCPropertyGridCtrl::GetPropertyColumnWidth()
but there appears to be no correspondingSet...
method... - I looked at the
NewControls
sample, in which the column sizing appears to be fully automatic. However, I couldn't find the relevant difference to my code.
What am I missing?
回答1:
m_nLeftColumnWidth
responsible for holding the "Name" column's width is a protected
member of the CMFCPropertyGridCtrl
class. Create your own class, that derives from CMFCPropertyGridCtrl
and you will be able to manipulate m_nLeftColumnWidth
.
回答2:
Note that m_nLeftColumnWidth is initially set to 0 in CMFCPropertyGridCtrl's ctor. One of the few other places that it is set, is in the OnSize() method (ref. AfxPropertyGridCtrl.cpp, line 2783 in VS2010), where it is set to half the width of the grid. This may not be ideal, nor the customized value described by overriding the class to explicitly set it, but may be good enough.
If so, then it is merely to trigger having the CMFCPropertyGridCtrl::OnSize() protected method. When used in a resizable window such as a CDockablePane, OnSize() will be called automatically. But in a CDialog, it needs to be trigger explicitly such as to send a WM_SIZE message:
CRect rect;
myPropGrid.GetWindowRect(&rect);
myPropGrid.PostMessage(WM_SIZE, 0, MAKELONG(rect.Width(),rect.Height()));
回答3:
class CServerPropertyGridCtrl : public CMFCPropertyGridCtrl
{
public:
void SetLeftColumnWidth(int cx)
{
m_nLeftColumnWidth = cx;
AdjustLayout();
}
};
回答4:
The reason the "set" isn't there is because it's left to the header control. The below is the method of handling through MFC versus posting window messages:
HDITEM hdItem;
hdItem.mask = HDI_WIDTH; // indicating cxy is width
hdItem.cxy = 300; // whatever you want the property name column width to be
PropListCtrl.GetHeaderCtrl().SetItem(0, &hdItem);
来源:https://stackoverflow.com/questions/3453244/how-to-set-a-cmfcpropertylistctrls-column-width