//
// Function name: drawprogress ()
// Function: Customize the progress bar and draw the progress bar in the rect of the specified HDC. It is applicable to the application of inserting the progress bar in the list control.
// Parameters:
// Sourcerect --- subitem rectangle in the list control (the progress bar drawn by yourself must be smaller than one pixel)
// HDC --- the HDC in the List Control
// Nprogresspercent -- Progress value, from 0 to 100)
//
Void drawprogress (HDC, rect sourcerect, int nprogresspercent)
{
Rect; // The rectangle of the entire progress bar, which is a unit smaller than the source rectangle (you can set the size as needed)
Rect = sourcerect;
Rect. Left + = 1;
Rect. Top + = 1;
Rect. Right-= 1;
Rect. Bottom-= 1;
Rect leftrect, rightrect; // each rectangle on the left and right is used to display different colors in the progress bar text.
Leftrect = rect;
Leftrect. Left + = 1;
Leftrect. Top + = 1;
Leftrect. Bottom-= 1;
Rightrect = leftrect;
Int W = (rect. Right-rect. Left-2) * nprogresspercent)/100; // nprogresspercent variable, expressed as a percentage from 0
Leftrect. Right = leftrect. Left + W;
Rightrect. Left = leftrect. Right;
// Draw a progress bar ********************************* *******
// Border
Hbrush hbrushred = createsolidbrush (RGB (224, 0, 0); // color Customization
Framerect (HDC, & rect, hbrushred );
Deleteobject (hbrushred );
// Fill
Hbrush hbrushgreen = createsolidbrush (RGB (0,224, 0); // custom color
Fillrect (HDC, & leftrect, hbrushgreen );
Deleteobject (hbrushgreen );
// Draw progress text *********************************** ***************
Tchar szformatstr [10];
_ Snprintf (szformatstr, 10, text ("% d %"), nprogresspercent); // you can see why nprogresspercent is between 0 and 100.
// Set the text background color to transparent
Setbkmode (HDC, transparent );
Rect textrect = rect; // text area
Hrgn hrgnleft = createrectrgnindirect (& leftrect );
Selectcliprgn (HDC, hrgnleft );
Settextcolor (HDC, RGB (0, 0,255); // text color
Drawtext (HDC, szformatstr,-1, & textrect, dt_center | dt_vcenter | dt_singleline );
Deleteobject (hrgnleft );
Hrgn hrgnright = createrectrgnindirect (& rightrect );
Selectcliprgn (HDC, hrgnright );
Settextcolor (HDC, RGB (255, 0, 0); // text color
Drawtext (HDC, szformatstr,-1, & textrect, dt_center | dt_vcenter | dt_singleline );
Deleteobject (hrgnright );
Selectcliprgn (HDC, null );
}
2. Respond to the nm_customdraw message correctly.
The code snippet is as follows:
Case wm_policy: // notification sent from the list control
// Process notification messages.
Switch (lpnmhdr) lparam)-> code)
{
Case nm_customdraw: // custom list control
{
LpnmlvcustomdrawLplvcd = (Lpnmlvcustomdraw) Lparam;
Switch (lplvcd-> nmcd. dwdrawstage)
{
Case cdds_prepaint:
Setwindowlong (hdlg, dwl_msgresult, cdrf_policysubitemdraw );
Return cdrf_policysubitemdraw;
Case cdds_itemprepaint:
Setwindowlong (hdlg, dwl_msgresult, cdrf_policysubitemdraw );
Return cdrf_policysubitemdraw;
Case cdds_subitem | cdds_itemprepaint:
{
Int nitemspec = lplvcd-> nmcd. dwitemspec;
Int nsubitem = lplvcd-> isubitem;
// Process the Second Progress Subitem (which column can be customized)
Rect;
Listview_getsubitemrect (hwndlistview, nitemspec, 1, lvir_bounds, & rect );
// Todo: Process N at the appropriate position, make it within the range of 0 to 100, and then update the rect area.
Drawprogress (lplvcd-> nmcd. HDC, rect, N );
Setwindowlong (hdlg, dwl_msgresult, cdrf_dodefault); // This line is required for the dialog box.
Return cdrf_dodefault;
}
}
}
Return false; // if other operations are not processed, they are returned to Windows
Sample Code for processing N:
// Refresh the area after changing the value of N at a certain position to forward the progress bar, as shown in
N ++;
Rect;
Listview_getsubitemrect (hwndlistview, index, 1, lvir_bounds, & rect); // index is the project index.
Invalidaterect (hwndlistview, & rect, false );
Updatewindow (hwndlistview );