0

We have a requirement to change the color of pushbutton/control. I have controls defined in resource file. We have tried multiple ways 1)Using CMFCButton Object I got defination ambiguity errors 2) Using CBUtton CDC* pdcWindow1 = m_Button.GetWindowDC(); CRect rect1; GetClientRect(&rect1); pdcWindow1->FillSolidRect(&rect1, (RGB(0, 0, 255)));

No effect on Button color no error as well.

Inputs which i have got so far : we have used ATLcontrols and to color Button we need MFC Functions, here ATL and MFC libs can't coexist they are causing ambiguity errors as both have same functional definitions.

Is it even possible to color ATL controls without MFC functions.? only solution is --https://jeffpar.github.io/kbarchive/kb/173/Q173974/??

  • 1
    Both ATL and MFC are just (thin) wrappers around WinAPI functionality. If you can't find the ATL method you need you should identify the WinAPI function and use that, One simple way (which ATL supports) is to sub-class the control, and process the `WM_ERASEBKGND` message. – Richard Critten Jan 19 '23 at 13:01

2 Answers2

0

Look of standard Windows GDI buttons is customized according to Button Color Messages:

The system sends a WM_CTLCOLORBTN message to a button's parent window before drawing a button. This message contains a handle to the button's device context and a handle to the child window. The parent window can use these handles to change the button's text and background colors. However, only owner-drawn buttons respond to the parent window processing the message.

In ATL project, you would either handle this notification message in parent window class or use more sophisticated message forwarding (reflection) to reflect this message to button class.

Either way you don't really paint (FillSolidRect), you can just update colors in the message handler. And also pay attention that only owner-drawn buttons expose this functionality.

See also: Owner-drawn button, WM_CTLCOLORBTN and WM_DRAWITEM (clearing an HDC)

Roman R.
  • 68,205
  • 6
  • 94
  • 158
0

This is how I was able to achieve :

HINSTANCE hInstance = GetModuleHandle(NULL);
HANDLE hBitmap = LoadImage(hInstance, MAKEINTRESOURCE(IDB_CURCANCEL_LOGO), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
HANDLE hBitmap1 = LoadImage(hInstance, MAKEINTRESOURCE(IDB_CURLOGIN_LOGO), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);

const HWND hOriginalLoginButton = GetDlgItem(IDOK);
const HWND hOriginalCancelButton = GetDlgItem(IDCANCEL);

SendMessage(hOriginalLoginButton, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap1);
SendMessage(hOriginalCancelButton, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);

Now i am trying to make corners rounded.

Usitha Indeewara
  • 870
  • 3
  • 10
  • 21