This API would simply replace one color with another in a rectangular region
of a given device context:
void ReplaceColor(HDC hDC, CRect rcReplaceArea, COLORREF clrColorReplace, COLORREF clrColorFill)
{
CDC* pDC = CDC::FromHandle(hDC);
CPoint pt = rcReplaceArea.TopLeft();
CDC memDCMonoChrome;
memDCMonoChrome.CreateCompatibleDC(pDC);
CBitmap bmpMonoChrome;
bmpMonoChrome.CreateCompatibleBitmap(&memDCMonoChrome, rcReplaceArea.Width(), rcReplaceArea.Height());
CBitmap* pOldMonoBitmap = memDCMonoChrome.SelectObject(&bmpMonoChrome);
COLORREF nOldBkColor = pDC->SetBkColor(clrColorReplace);
memDCMonoChrome.BitBlt(0, 0, rcReplaceArea.Width(), rcReplaceArea.Height(), pDC, pt.x, pt.y, SRCCOPY);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, rcReplaceArea.Width(), rcReplaceArea.Height());
CBitmap* pOldBitmap = memDC.SelectObject(&bmp);
COLORREF nOldMemDCBkColor = memDC.SetBkColor(clrColorFill);
COLORREF nOldMemDCTextColor = memDC.SetTextColor(RGB(255, 255, 255));
memDC.BitBlt(0, 0, rcReplaceArea.Width(), rcReplaceArea.Height(), &memDCMonoChrome, 0, 0, SRCCOPY);
pDC->BitBlt(pt.x, pt.y, rcReplaceArea.Width(), rcReplaceArea.Height(), &memDC, 0, 0, SRCAND);
memDC.SetTextColor(RGB(0, 0, 0));
memDC.BitBlt(0, 0, rcReplaceArea.Width(), rcReplaceArea.Height(), &memDCMonoChrome, 0, 0, SRCCOPY);
pDC->BitBlt(pt.x, pt.y, rcReplaceArea.Width(), rcReplaceArea.Height(), &memDC, 0, 0, SRCPAINT);
memDC.SetTextColor(nOldMemDCTextColor);
memDC.SetBkColor(nOldMemDCBkColor);
pDC->SetBkColor(nOldBkColor);
memDCMonoChrome.SelectObject(pOldMonoBitmap);
memDC.SelectObject(pOldBitmap);
}