Search code examples
c++windowswinapigdi

How to draw text over filled rectangle by XOR-ing its color with GDI?


I'm not sure if I'm expressing it right with words. The best way is to show it with an image. I'm trying to fill a rectangle while inverting the text color in it. Just like an edit control does:

enter image description here

So here's a drawing code (called from WM_PAINT):

//Erase background
::FillRect(hDC, &rcClient, ::GetSysColorBrush(COLOR_WINDOW));

//Draw text
::DrawText(hDC, text, text.size(), &rc, 
    DT_LEFT | DT_TOP | DT_EDITCONTROL | DT_NOPREFIX | DT_SINGLELINE);

//Draw highlighted rect & invert text
HGDIOBJ hOldBrush = ::SelectObject(hDC, ::GetSysColorBrush(COLOR_HIGHLIGHT));
::PatBlt(hDC, rcDrawFrame.left, rcDrawFrame.top, rcDrawFrame.Width(), rcDrawFrame.Height(), DSTINVERT);
::SelectObject(hDC, hOldBrush);

But for some reason the background rectangle comes out as black, and I know that COLOR_HIGHLIGHT is blue on this system:

enter image description here

So what am I doing wrong here?


Solution

  • You might want to consider a different strategy; drawing the text twice with inverted clipped region.