Home > Software engineering >  The List Control controls the custom painting problem
The List Control controls the custom painting problem

Time:10-04

Such as title, opens the Onwer the Draw Fixed style, then rewrite the CListCtrl class, to respond to the NM_CustomDraw this Notify messages, and, in the code have CDRF_NOTIFYSUBITEMDRAW this return messages, but the only item index is incoming, children always is 0, so do you have any good solution?

CodePudding user response:

In the http://www.codeproject.com search CListCtrl

CodePudding user response:

 
Void CReportCtrl: : OnCustomDraw (NMHDR * pNMHDR, LRESULT * pResult)
{
LPNMLVCUSTOMDRAW LPLVCD=(LPNMLVCUSTOMDRAW pNMHDR);

If (LPLVCD - & gt; NMCD. DwDrawStage==CDDS_PREPAINT)
{
* pResult=CDRF_NOTIFYITEMDRAW;
}
Else if (LPLVCD - & gt; NMCD. DwDrawStage==CDDS_ITEMPREPAINT)
{
* pResult=CDRF_NOTIFYSUBITEMDRAW;
}
Else if (LPLVCD - & gt; NMCD. DwDrawStage==(CDDS_ITEMPREPAINT | CDDS_SUBITEM))
{
CItemData * p=(CItemData *) (CListCtrl: : GetItemData (LPLVCD - & gt; NMCD. DwItemSpec));
ASSERT (p!=NULL);
ASSERT (LPLVCD - & gt; ISubItem & gt;=0 & amp; & LPLVCD - & gt; ISubItem & lt; P - & gt; ATextColors. GetSize ());
LPLVCD - & gt; ClrText=p - & gt; ATextColors [LPLVCD - & gt; iSubItem];
LPLVCD - & gt; ClrTextBk=p - & gt; ABkColors [LPLVCD - & gt; iSubItem];
* pResult=CDRF_DODEFAULT;
}
}

CodePudding user response:

 
//////////////////////////////////////////////////////////////
LRESULT ListViewCustomDraw (HWND HWND, LPARAM LPARAM)
{
LPNMHDR PNMH=(lParam LPNMHDR);

If (PNMH - & gt; Code!=NM_CUSTOMDRAW)
{
Return (ListViewNotify (HWND, lParam));
}

LPNMLVCUSTOMDRAW lpNMCustomDraw=(lParam LPNMLVCUSTOMDRAW);

Int nResult=CDRF_DODEFAULT;

If (CDDS_PREPAINT==lpNMCustomDraw - & gt; NMCD. DwDrawStage)
{
NResult=CDRF_NOTIFYITEMDRAW;
}
Else if (CDDS_ITEMPREPAINT==lpNMCustomDraw - & gt; NMCD. DwDrawStage)
{
NResult=CDRF_NOTIFYSUBITEMDRAW | CDRF_NOTIFYPOSTPAINT;
}
Else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM)==lpNMCustomDraw - & gt; NMCD. DwDrawStage)
{
NResult=CDRF_SKIPDEFAULT;

Const DWORD dwStyle=DT_LEFT | DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS;

HDC HDC=lpNMCustomDraw - & gt; NMCD. HDC;
SetBkMode (HDC, TRANSPARENT);
Int nItem=(int) lpNMCustomDraw - & gt; NMCD. DwItemSpec;
Int nSubItem=lpNMCustomDraw - & gt; The iSubItem;

BOOL bItemSelected=ListView_GetItemState (HWND, nItem LVIS_SELECTED);

The RECT subItemRect;
ListView_GetSubItemRect (HWND, nItem nSubItem LVIR_BOUNDS, & amp; SubItemRect);
//
HBRUSH BRSH=0;
If (bItemSelected)
{//OutputDebugString (" bItemSelected \ n ");
BRSH=CreateSolidBrush (RGB (255, 128, 128));
FillRect (HDC, & amp; SubItemRect BRSH);
}
The else
{//not Selected
BRSH=CreateSolidBrush (RGB (51 + nItem * 30, 153, 255 - nItem * 30));
FillRect (HDC, & amp; SubItemRect BRSH);
}
If (BRSH) DeleteObject (BRSH);
//
If (nSubItem==0)
{//OutputDebugString (" BMP \ n ");
The RECT iconRect;
ListView_GetSubItemRect (HWND, nItem nSubItem LVIR_ICON, & amp; IconRect);
OffsetRect (& amp; IconRect, 1, 0);
HBITMAP oldbmp=(HBITMAP) SelectObject (g_hMemDC g_hbmNormal);
BitBlt (HDC, iconRect. Left, iconRect. Top, 16, 16, g_hMemDC, 0, 0, SRCCOPY);
SelectObject (HDC, oldbmp);
}
//
TCHAR szText [260].
ListView_GetItemText (HWND, nItem nSubItem szText, 260);
OffsetRect (& amp; SubItemRect, 18, 0);
DrawText (HDC, szText, strlen (szText), & amp; SubItemRect dwStyle);
Return nResult;
}
Else if (CDDS_ITEMPOSTPAINT==lpNMCustomDraw - & gt; NMCD. DwDrawStage)
{//the draw (horizantal line)
HPEN HPEN=CreatePen (PS_SOLID, 1, RGB (0255, 0));
HPEN holdpen=(HPEN) SelectObject (lpNMCustomDraw - & gt; NMCD HDC, hpen);
The RECT CRC;
GetClientRect (HWND, & amp; CRC);
The RECT irc.
ListView_GetItemRect (HWND lpNMCustomDraw - & gt; NMCD dwItemSpec, & amp; The irc, LVIR_BOUNDS);
HDC HDC=lpNMCustomDraw - & gt; NMCD. HDC;
MoveToEx (HDC, 0, irc. Top, 0).
LineTo (HDC, CRC. Right - CRC. Left, irc, top);
SelectObject (HDC, holdpen);
//
Return CDRF_DODEFAULT;
}
Return nResult;
}
  • Related