Can anyone give me some guidance as to implementing an ownerdraw header
control. I currently have an owner draw CListCtrl and I would like to alter
the appearance of the header (Different font and background color). Any
ideas would be appreciated. I've checked www.codeguru.com, but to no avail.
Thanks in advance,
Greg.
As first you should get header control and set its flags to 'owner-draw'...
// Declare something like this somewhere and initialize it...
CHeaderCtrl *m_pHeaderCtrl;
...
// Initialization part - this code can be called from OnCreate or some
similar function...
if (GetStyle() & LVS_REPORT)
{
// Then get header control...
CHeaderCtrl *listHeader = (CHeaderCtrl*)GetWindow(GW_CHILD);
if (listHeader)
{
// ...create your own header and attach it to the header control of
the list...
int nCol = listHeader->GetItemCount();
HD_ITEM hdrItem;
m_pHeaderCtrl = new CHeaderCtrl();
m_pHeaderCtrl->Attach(listHeader->m_hWnd);
for (int i = 0; i < nCol; i++)
{
hdrItem.mask = HDI_FORMAT;
if (m_pHeaderCtrl->GetItem(i, &hdrItem))
{
// Finally, add owner-draw flag to all or just to headers
you would like to customize...
hdrItem.fmt |= HDF_OWNERDRAW;
m_pHeaderCtrl->SetItem(i, &hdrItem);
}
}
}
}
...
You must also override OnDrawItem and do some 'owner' drawing...
...
void CSomeListCtrl::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT
lpDrawItemStruct)
{
if ((lpDrawItemStruct->CtlType == ODT_HEADER) && m_pHeaderCtrl)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// Handle state of the header bar
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
// Draw some stuff here...
}
else if (lpDrawItemStruct->itemState &
ODS_SOMETHING_ELSE_I_DONT_REMEMBER)
{
// ...and/or here...
}
else ...
...
}
}
P.S. Don't forget to detach and delete m_pHeaderCtrl at the end
(OnDestroy)...
I hope this helps, Saso.
I'll give it a try.
Greg.
>Can anyone give me some guidance as to implementing an ownerdraw header
>control. I currently have an owner draw CListCtrl and I would like to alter
>the appearance of the header (Different font and background color). Any
>ideas would be appreciated. I've checked www.codeguru.com, but to no avail.
Check codeguru.com again, under the Advanced UI section, the CJ60Lib
extension library includes an owner-draw header control.
-Anatoly
--
DTLink Software
http://www.dtlink.com
Home of Personal Stock Monitor and Personal Stock Monitor GOLD
Ziff-Davis Award Winner 1998