CListCtrl多表头

Multiline Header Control Inside a CListCtrl



First of all I have to mention that Alon Peleg helped me find the solution to the problem so I feel it is only fair that his name will be mentioned as an author.

On a recent project we did I had to make the header control of a CListCtrl multiline. This small project show how to do it by subclassing the CHeaderCtrl of the CListCtrl.

If you want to use this code just the HeaderCtrlExt.h and HeaderCtrlExt.cpp files into your source code.

In addition on your CListView or CListCtrl derived class add a member variable of type CHeaderCtrlExand a member variable of type CFont.

If you are using a CListCtrl without a view then put the following code in the end of the OnCreate handler of the CListCtrl:

///SET UP THE MULTILINE HEADER CONTROL
//m_NewHeaderFont is of type CFont
m_NewHeaderFont.CreatePointFont(190,"MS Serif");

CHeaderCtrl* pHeader = NULL;
pHeader=GetHeaderCtrl();

if(pHeader==NULL)
   return;

VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd));

//A BIGGER FONT MAKES THE CONTROL BIGGER
m_HeaderCtrl.SetFont(&m_NewHeaderFont);

HDITEM hdItem;

hdItem.mask = HDI_FORMAT;

for(i=0; i < m_HeaderCtrl.GetItemCount(); i++)
{
   m_HeaderCtrl.GetItem(i,&hdItem);

   hdItem.fmt|= HDF_OWNERDRAW;

   m_HeaderCtrl.SetItem(i,&hdItem);
}

If you are using a CListView or any class derived by it then add the following code in theOnInitialUpdate override of the CListView:

///SET UP THE MULTILINE HEADER CONTROL
//m_NewHeaderFont is of type CFont
m_NewHeaderFont.CreatePointFont(190,"MS Serif");

CListCtrl& ListCtrl = GetListCtrl();

CHeaderCtrl* pHeader = NULL;
pHeader=ListCtrl.GetHeaderCtrl();

if(pHeader==NULL)
   return;

VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd));

//A BIGGER FONT MAKES THE CONTROL BIGGER
m_HeaderCtrl.SetFont(&m_NewHeaderFont);

HDITEM hdItem;

hdItem.mask = HDI_FORMAT;

for(i=0; i<m_HeaderCtrl.GetItemCount(); i++)
{
   m_HeaderCtrl.GetItem(i,&hdItem);

   hdItem.fmt|= HDF_OWNERDRAW;

   m_HeaderCtrl.SetItem(i,&hdItem);
}

The only difference between the two parts of code is way we get a pointer to the Header control.

That's it. Enjoy!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值