#include "stdafx.h"
#define ROW_SHADE 90
extern FARPROC prevWndFunc;
//=========================================================
//-> create a lighter shade (by fPercent %) of a given color
COLORREF colorShade(COLORREF c, BYTE fPercent) {
return RGB((GetRValue (c) * fPercent / 100.0),
(GetGValue (c) * fPercent / 100.0),
(GetBValue (c) * fPercent / 100.0));
}
//====================================================
//-> re-draw rows with the appropriate background color
void PaintAlternatingRows(HWND hWnd) {
int iItems, iTop;
RECT rectDestin, // temporary storage
rectUpd, // rectangle to update
rect; // row rectangle
POINT pt; // >>>--->.<---<<<
COLORREF c; // temporary storage
// get the rectangle to be updated
GetUpdateRect(hWnd, &rectUpd, FALSE);
// allow default processing first
CallWindowProc((WNDPROC)prevWndFunc, hWnd, WM_PAINT, 0, 0);
// set the row horizontal dimensions
SetRect(&rect, rectUpd.left, 0, rectUpd.right, 0);
// number of displayed rows
iItems = ListView_GetCountPerPage(hWnd);
// first visible row
iTop = ListView_GetTopIndex(hWnd);
ListView_GetItemPosition(hWnd, iTop, &pt);
for(int i=iTop ; i<=iTop+iItems ; i++) {
char szPCount[MIN_BUFF] = {0};
// set row vertical dimensions
rect.top = pt.y;
ListView_GetItemPosition(hWnd, i+1, &pt);
rect.bottom = pt.y;
// if row rectangle intersects update rectangle then it requires re-drawing
if(IntersectRect(&rectDestin, &rectUpd, &rect)) {
// change text background colour accordingly
c = (i % 2) ?
colorShade(GetSysColor(COLOR_WINDOW), ROW_SHADE) :
GetSysColor(COLOR_WINDOW);
ListView_SetTextBkColor(hWnd, c);
//-------------------------------------------------------------------------------------------->
ListView_GetItemText(hWnd, i, 5, szPCount, sizeof(szPCount));
if(strcmp(szPCount, "OffLine!")) { //---++++++---> String Match = 0 <-> So...
ListView_SetTextColor(hWnd, CLR_DEFAULT); //-> OnLine Printers Stay Black
}else{ //-------------------------------------------------> ...and...
ListView_SetTextColor(hWnd, RGB(255, 0, 0)); // OffLine Printers Turn Red
}
//-------------------------------------------------------------------------------------------->
// invalidate the row rectangle then...
InvalidateRect(hWnd, &rect, FALSE);
// ...force default processing
CallWindowProc((WNDPROC)prevWndFunc, hWnd, WM_PAINT, 0, 0);
}
}
}
// re-draw row backgrounds with the appropriate background colour
void EraseAlternatingRowBkgnds(HWND hWnd, HDC hDC) {
int iItems, iTop;
HBRUSH brushCol1, // 1st color
brushCol2; // 2nd color
RECT rect; // row rectangle
POINT pt;
// create colored brushes
brushCol1 = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
brushCol2 = CreateSolidBrush(colorShade(GetSysColor(COLOR_WINDOW), ROW_SHADE));
// get horizontal dimensions of row
GetClientRect(hWnd, &rect);
// number of displayed rows
iItems = ListView_GetCountPerPage(hWnd);
// first visible row
iTop = ListView_GetTopIndex(hWnd);
ListView_GetItemPosition(hWnd, iTop, &pt);
for(int i=iTop; i<=iTop+iItems; i++) {
// set row vertical dimensions
rect.top = pt.y;
ListView_GetItemPosition(hWnd, i+1, &pt);
rect.bottom = pt.y;
// fill row with appropriate color
FillRect(hDC, &rect, (i % 2) ? brushCol2 : brushCol1);
}
// cleanup
DeleteObject(brushCol1);
DeleteObject(brushCol2);
}
//=======================================================================================
//-----------------------------------------------------------> SubClassed Window Procedure
LRESULT CALLBACK ListViewWndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) {
switch(iMessage) {
case WM_PAINT:
// intercept the WM_PAINT message which is called each time an area
// of the control's client area requires re-drawing
PaintAlternatingRows(hWnd);
return 0;
case WM_ERASEBKGND:
// intercept the WM_ERASEBKGRN message which is called each time an area
// of the control's client area background requires re-drawing
EraseAlternatingRowBkgnds(hWnd, (HDC) wParam);
return 0;
}
// continue with default message processing
return CallWindowProc((WNDPROC)prevWndFunc, hWnd, iMessage, wParam, lParam);
}