Problems with WM_ERASEBKGND in DialogBox
Posted on 2004-10-06
First up: I am not using MFC, I am using plain a old Win32 DialogBox(...).
I read an article saying I could create a gradient background by handling the WM_ERASEBKGND message in my dialogue's message proc. I've done that and it looks good but the problem is as soon as I move my dialogue everything goes bad. Other controls don't repaint properly if at all and if move the dialogue so that any part of it leaves the viewport then I get artifacts everywhere (everywhere on my moniter, not just the dialog).
Prior to this I didn't overload WM_ERASEBKGND, instead I just returned a solid brush from WM_CTLCOLORDLG and everything was happy (though boring). What am I doing wrong?
Here is a truncated version of my msg proc:
INT_PTR CShell::MsgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
//if(HDC(wParam) != GetDC(Instance()->hWnd)) return FALSE;
RECT rect, row;
BYTE r, g, b;
row.left = 0;
row.right = rect.right;
//--- loop through 1 pixel high rows ---//
for(int i = 0; i < rect.bottom; ++i)
r = static_cast<BYTE>(r1 + (i * (r2-r1) / rect.bottom));
g = static_cast<BYTE>(g1 + (i * (g2-g1) / rect.bottom));
b = static_cast<BYTE>(b1 + (i * (b2-b1) / rect.bottom));
row.top = i;
row.bottom = i+1;
FillRect(reinterpret_cast<HDC>(wParam), &row, CreateSolidBrush(RGB(r, g, b)));