Embedding progress bar in VC ListCtrl

xiaoxiao2021-03-06  15

The progress bar is embedded in the ListCtrl in the VC, and the screenshots are as follows:

In fact, it is very easy. The following is the code of the self-painted listCtrl, first inherited ClistCtrl, then add functions: void cprocessList :: OnCustomDraw (nmhdr * pnmhdr, lresult * presult) {// Draw Each Item.set Txt Color, BKColor. ... NMLVCUSTOMDRAW * pLVCD = reinterpret_cast (pNMHDR); // Take the default processing unless we set this to something else below * pResult = CDRF_DODEFAULT;.. // First thing - check the draw stage If it's the control's prepaint // stage, then tell Windows we want messages for every item if (pLVCD-> nmcd.dwDrawStage == CDDS_PREPAINT) {* pResult = CDRF_NOTIFYITEMDRAW;}. else if (pLVCD-> nmcd.dwDrawStage == CDDS_ITEMPREPAINT) {// This is the notification message for an item We'll request // notifications before each subitem's prepaint stage * pResult = CDRF_NOTIFYSUBITEMDRAW;..} else if (pLVCD-> nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) {// This is The Prepaint Stage for A Subitem. Here's WHERE We set the // iTem's text and background colors. Our return value will tell // windows to draw the sub Item Itself, But it will use the new color = static_cast (PLVCD-> nmcd.dwItemspec); int NSUBITEM = PLVCD-> isubitem; if (nsubitem! = 2) // Here I only redraw second column return;

COLORREF crText = :: GetSysColor (COLOR_WINDOWFRAME); COLORREF crBkgnd = :: GetSysColor (COLOR_WINDOW); CDC * pDC = CDC :: FromHandle (pLVCD-> nmcd.hdc); CRect rect; GetSubItemRect (nItem, nSubItem, LVIR_BOUNDS, rect) ; if (GetItemState (nItem, LVIS_SELECTED)) DrawText (nItem, nSubItem, pDC, :: GetSysColor (COLOR_HIGHLIGHT), :: GetSysColor (COLOR_HIGHLIGHT), rect); else DrawText (nItem, nSubItem, pDC, crText, crBkgnd, rect) ; * presult = cdrf_skipdefault; // we've painted everything.}

Then add message mapping for this function: ON_NOTIFY_REFLECT (NM_CUSTOMDRAW, ONCUSTOMDRAW) Finally, we work hard for the drawing of the model, and the progress exists in ItemData. void CProcessList :: DrawText (int nItem, int nSubItem, CDC * pDC, COLORREF crText, COLORREF crBkgnd, CRect & rect) {ASSERT (pDC); pDC-> FillSolidRect (& rect, crBkgnd); int nProcess = GetItemData (nItem); CRect PROCRECT = Rect; PDC-> Rectangle (ProCRect);

ProcRect.Eft = 1; procRect.bottom - = 1; procRect.top = 1; procRect.right = procRect.Lect Rect.width () * nprocess / 100; CBRUSH Brush (RGB (255, 0, 0) PDC-> FillRect (& ProcRect, & brush); CString Str; str.format ("% D %%", nprocess); if (! Str.isempty ()) {uint nformat = dt_vcenter | dt_singeline | dt_center; PDC- > Setbkmode (transparent); PDC-> setTextColor (CRText); PDC-> setBkcolor (CRBKGND); PDC-> DrawText (Str, & Rect, nformat);}}

how about it? Very simple, actually using the VC development interface is very fast, although it is still that the MFC package has a Java interface library package so good ...

转载请注明原文地址:https://www.9cbs.com/read-46207.html

New Post(0)