I need to change the background brush for a window, when a user enters dragging an OLE object.
My code is as follows.
SetClassLongA
is actually called. hWnd
, DragBrush
are OK.
But the background of the window hWnd
does not changed. What am I doing wrong?
Is it right to use SetClassLongA
in order to change the window background?
// IDropTarget implementation
virtual HRESULT __stdcall DragEnter(IDataObject* pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override {
std::cout << " OLE -> DragEnter." << std::endl;
*pdwEffect = DROPEFFECT_COPY;
HBRUSH DragBrush = CreateHatchBrush(HS_CROSS, RGB(255, 0, 0));
if (DragBrush == NULL) {
std::cout << "Error creating dragging brush_2" << std::endl;
}
DWORD OldBrush = SetClassLongA(hWnd, GCLP_HBRBACKGROUND, (LONG) DragBrush);
BOOL ok1 = InvalidateRect(hWnd, NULL, TRUE);
BOOL ok2 = UpdateWindow(hWnd);
// ok1=ok2=1 OK
return S_OK;
}
You can see remarks, it says: Use the SetClassLong function with care. For example, it is possible to change the background color for a class by using SetClassLong, but this change does not immediately repaint all windows belonging to the class.
So you need to use GetClientRect
and FillRect
to redraw the client area.
I used Visual Studio's project sample. See, here is your purple window before redraw:
When we add GetClientRect
and FillRect
. It shows correct color as I made the read one:
The code in WM_PAINT message:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
HBRUSH hBrush = CreateSolidBrush(RGB(220, 0, 0));
ULONG_PTR res = SetClassLongPtr(hWnd, GCLP_HBRBACKGROUND, (LONG)hBrush);
//DeleteObject(hBrush);
RECT rect;
GetClientRect(hWnd, &rect);
int res1= FillRect(hdc, &rect, hBrush);
EndPaint(hWnd, &ps);
}