1

Thanks for your help In solving my problem I try to add a button that can change the background of the dialog to a different color i using visual studio 2010 but i think its might be wrong way to do that

    void PainterDlg::OnBnClickedButton7()
    {
    CBrush m_brush; 
    m_brush.CreateSolidBrush(RGB(255, 255, 255));
    return m_brush;
    }

Or it should look like this

    void PainterDlg::OnBnClickedButton7()
    {
    CBrush m_brush; 
    m_brush.CreateSolidBrush(RGB(255, 255, 255));
    return m_brush;
    }

both ways are not work for me thankS in advance

1
  • 1
    The system cannot guess, what you are trying to accomplish. The WM_COMMAND-handlers do not have a return value. Randomly returning objects will not do anything meaningful. Why don't you start by decomposing your problem into its constituents: 1 Responding to user input. 2 Changing the visual representation. Jan 9, 2018 at 0:11

2 Answers 2

4

That is not so easy with CButton. (you have to draw all yourself in OnDrawItem, OnCtlColor)

A simpler way is to use CMFCButton. Add a Member Variable for your Button (with MFC-ClassWizzard) and change it to CMFCButton. Here an example to change the color button in green.

void CColorButtonSimpleDlg::OnBnClickedMyColorbtn()
{
  // add a Member Variable for your Button
  // Change it to CMFC Button

  // CMFCButton m_myBtn;     declared in Header-File  *.h 
  m_myBtn.EnableWindowsTheming(FALSE);      // (important!)
  m_myBtn.SetFaceColor(RGB(0, 255, 0));     // Change to your desired Background Color
  m_myBtn.SetTextColor(RGB(255, 255, 255)); // Change it to your desired Foreground Color

}

2
  • SetFaceColor works but not SetTextColor, and overall it looks awful.
    – user180574
    Oct 12, 2020 at 17:04
  • Keep it simple! :) I am not an expert for color theory. SetTextColor works. Perhaps you have to invalidate and redraw the button in your example.
    – Tom Tom
    Oct 13, 2020 at 5:27
-1

Nvm found it

    int r,b,g; 
    r=rand()%255;
    b=rand()%255;
    g=rand()%255;
    CBrush myb;
    myb.CreateSolidBrush(RGB(r,b,g));
    dc2.FillRect(&rect,&myb);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.