Im having trouble trying to figure out how to change the background color of a button every second. Go from one color to another. This is how I create My Button. or possibly how can you change the color of a HWND instance outside of onPaint if possible?
hButton = CreateWindowEx(WS_EX_TRANSPARENT, "Button","B",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
20, 30, 20, 20, hwnd, (HMENU)ID_BUTTON,
hInst, NULL);
Here is the changing color case inside the WndProc.
case WM_ERASEBKGND:
RECT rc;
GetClientRect(hButton, &rc);
SetBkColor((HDC)wParam, 0x000000ff); // red
ExtTextOut((HDC)wParam, 0, 0, ETO_OPAQUE, &rc, 0, 0, 0);
return 1;
Here is how I set my timer.
if (!SetTimer(hwnd, TIMER1, 20, NULL))
{
MessageBox(hwnd, "No Timers Available", "Info", MB_OK);
return FALSE;
}
The button seems to chagne colors but it is to quickl. I am not sure how to fix this.
I prefer using CMFCButton
in such cases, provided your are using MFC.
The steps are very simple:
Add a member variable (control type) of type CMFCButton. Say you declared variable as CMFCButton m_hButton2;
Modify the OnInitDialog()
function and add these lines:
m_hButton2.m_nFlatStyle = CMFCButton::BUTTONSTYLE_NOBORDERS;
m_hButton2.m_bTransparent = false;
SetTimer(255, 1000, NULL);
Implement the OnTime()
function to change color of choice. I have implemented something like below.
void CMFCApplicationDialogDlg::OnTimer(UINT_PTR nIDEvent)
{
if(nIDEvent == 255)
{
static int nRedColor = 0;
m_hButton2.SetFaceColor(RGB(nRedColor++,0,0), true);
}
CDialogEx::OnTimer(nIDEvent);
}
The above implementation helps me increase Red
part of color gradually every second and after few seconds (after 100 seconds or so) button will start looking Red.
User contributions licensed under CC BY-SA 3.0