http://www.codeguru.com/Cpp/controls/listview/columns/article.php/c1065/ The new ColorQube MFP uses solid ink technology for a revolution in cost savings. Save up to 62% on color prints and reduce waste by 90%! Calculate how much you could save at FinallyColorIsLess.comPrevent column resizing Rating: Zafir Anjum (view profile) August 6, 1998The header control in the ListView control sends notification to the parent window (e.i. the ListView) before it begins resizing a column. We can override the OnNotify() function in the CListCtrl derived class to handle this notification. The code below prevents resizing of all columns. Note that the resize cursor still shows up.BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { switch (((NMHDR*)lParam)->code) { case HDN_BEGINTRACKW: case HDN_BEGINTRACKA: *pResult = TRUE; // disable tracking return TRUE; // Processed message }
return CListCtrl::OnNotify(wParam, lParam, pResult); }If you want to prevent resizing of only one column, you should check for the value in iItem field of the HD_NOTIFY structure. The code below stops only the first column from being resized. BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam; if((pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA) && pHDN->iItem == 0) // Prevent only first (col# 0) from resizing { *pResult = TRUE; // disable tracking return TRUE; // Processed message }
{
CPropertyPage::OnInitDialog(); // TODO: 在此添加额外的初始化
CListCtrl* pList = (CListCtrl*) GetDlgItem(IDC_LIST_UPDATE);
DWORD styles = LVS_SHOWSELALWAYS | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_FLATSB | LVS_EX_CHECKBOXES;
styles &= ~LVS_EX_TRACKSELECT;
pList->SetExtendedStyle(styles);
pList->InsertColumn(0, _T("简介"), LVCFMT_LEFT, 100);
pList->InsertColumn(1,_T("名称"),LVCFMT_LEFT, 100);
pList->InsertColumn(2, _T("等级"), LVCFMT_LEFT, 100);
pList->InsertColumn(3, _T("大小"), LVCFMT_LEFT, 60);
pList->InsertColumn(4, _T("时间"), LVCFMT_LEFT, 100);
pList->InsertColumn(5, _T("描述"), LVCFMT_LEFT, 100);
pList->InsertColumn(6,_T("下载"),LVCFMT_LEFT, 100);
pList->SetColumnWidth(6,0);
pList->EnableToolTips(TRUE);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}网上是说要重载类
可我想不用重载类有没有办法?
重写CMyHeaderCtrl中的OnChildNotify
BOOL CMyHeaderCtrl::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
{
// TODO: 在此添加专用代码和/或调用基类
NMHDR& nmh = *(NMHDR*)lParam;
if (nmh.code == HDN_BEGINTRACKA || nmh.code == HDN_BEGINTRACKW
||nmh.code == HDN_DIVIDERDBLCLICKA ||nmh.code == HDN_DIVIDERDBLCLICKW)
{
CRect rc;
GetItemRect(0,rc);
CPoint pt;
GetCursorPos(&pt);
ScreenToClient(&pt);
if(pt.x < rc.Width() + 5)
return *pLResult=TRUE; // eat message to disallow sizing
}
// otherwise, pass to header control for default processing
return CHeaderCtrl::OnChildNotify(message, wParam, lParam, pLResult); //return CHeaderCtrl::OnChildNotify(message, wParam, lParam, pLResult);
}
然后再重载一个clistctrl,加个CMyHeaderCtrl成员
重写void CMyListCtrl::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
ASSERT( GetStyle() & LVS_REPORT ); CListCtrl::PreSubclassWindow();
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) ); //CListCtrl::PreSubclassWindow();
}
CHeaderCtrl* pHeaderCtrl = (CHeaderCtrl*)m_ListCtrl.GetHeaderCtrl();
pHeaderCtrl->EnableWindow( FALSE ) ; 但是这样就固定了全部的列宽,如果只固定一列的话大概还是要自己重载
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam; if((pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA)
&& pHDN->iItem == 6) // Prevent only first (col# 0) from resizing
{
*pResult = TRUE; // disable tracking
return TRUE; // Processed message
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}
我这样写没有用
下断点也没进入?
The new ColorQube MFP uses solid ink technology for a revolution in cost savings. Save up to 62% on color prints and reduce waste by 90%! Calculate how much you could save at FinallyColorIsLess.comPrevent column resizing
Rating: Zafir Anjum (view profile)
August 6, 1998The header control in the ListView control sends notification to the parent window (e.i. the ListView) before it begins resizing a column. We can override the OnNotify() function in the CListCtrl derived class to handle this notification. The code below prevents resizing of all columns. Note that the resize cursor still shows up.BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
switch (((NMHDR*)lParam)->code)
{
case HDN_BEGINTRACKW:
case HDN_BEGINTRACKA:
*pResult = TRUE; // disable tracking
return TRUE; // Processed message
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}If you want to prevent resizing of only one column, you should check for the value in iItem field of the HD_NOTIFY structure. The code below stops only the first column from being resized.
BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
HD_NOTIFY *pHDN = (HD_NOTIFY*)lParam; if((pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGINTRACKA)
&& pHDN->iItem == 0) // Prevent only first (col# 0) from resizing
{
*pResult = TRUE; // disable tracking
return TRUE; // Processed message
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}这里所描述的是可以的