How to set a CMFCPropertyListCtrl's column width?
Asked Answered
G

4

7

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 corresponding Set... 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?

Giorgi answered 10/8, 2010 at 20:40 Comment(0)
H
9

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.

Hoard answered 12/8, 2010 at 12:10 Comment(0)
T
9

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()));
Thwart answered 20/11, 2012 at 23:22 Comment(0)
P
6

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);
Ppi answered 14/8, 2015 at 16:45 Comment(0)
F
4
class CServerPropertyGridCtrl : public CMFCPropertyGridCtrl
{
public:
    void SetLeftColumnWidth(int cx)
    {
        m_nLeftColumnWidth = cx;
        AdjustLayout();
    }
};
Felony answered 16/6, 2011 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.