CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 1999
    Posts
    18

    Dynamically Created CButton

    I derived a new class from CButton. I also catch the ONPaint() method with in this new class. In another clsss I dynamically created a radio CButton. What I want to do is change the background color of this radio CButton to the system color. Which I can do with the following routine:

    Here is how I create the radio button:


    OP1CButton *theRadioButton = new OP1CButton;
    theRadioButton->Create(theScriptXQuestion->cLabel, WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON, theCEditRect, this, NULL);




    Here is the ONPaint method:


    void OP1CButton::OnPaint()
    {
    CPaintDC dc(this); // device context for painting

    CRect rect;
    GetClientRect(rect);

    //Get system color.
    DWORD systemColor = GetSysColor(COLOR_MENU);

    dc.SetBkColor(systemColor);
    }




    Now what happens is I get a control tat I can't see until I click in the area of the control. Even the text doesn't show up......

    What in the heck am I doing wrong?????

    Thanks
    -LHoffman


  2. #2
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: Dynamically Created CButton

    ..could you post the whole OnPaint-function ? Perhaps I can help you.


  3. #3
    Join Date
    Aug 1999
    Posts
    18

    Re: Dynamically Created CButton

    It in the original post....

    Thanks
    -LHoffman


  4. #4
    Join Date
    Sep 1999
    Location
    Europe / Austria / Innsbruck
    Posts
    442

    Re: Dynamically Created CButton

    To change the colors of controls use the message WM_CTLCOLOR_REFLECT. If you overwrite OnPaint you must paint the whole control yourself, that's why you can't see it. This sample uses a CEdit but it should work with CButton too:

    class CColorEdit : public CEdit
    {
    // usual stuff here
    protected:
    //{{AFX_MSG(CColorEdit)
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };

    BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
    //{{AFX_MSG_MAP(CColorEdit)
    ON_WM_CTLCOLOR_REFLECT()
    //}}AFX_MSG_MAPEND_MESSAGE_MAP()

    HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
    {
    pDC->SetTextColor(RGB(255, 255, 255)); // textcolor: white
    pDC->SetBkMode(TRANSPARENT);
    return (HBRUSH)GetStockObject(BLACK_BRUSH); // backgroundcolor: black
    }






  5. #5
    Join Date
    Aug 1999
    Posts
    18

    Re: Dynamically Created CButton

    Hey Thomas,

    I tried what you suggested and it didn't seen to work. I also tried to implement the ON_WM_CTLCOLOR_REFLECT() I get an error at compile time. This code doesn't even seem to be executing when the CButtons are created. Any other suggestion??? I am am ll ears!!

    Thanks
    -LHoffman


  6. #6
    Join Date
    Aug 1999
    Posts
    18

    Re: Dynamically Created CButton

    Oops.... I got it working using your code example.. But I still need to change the color of the background to the menu system color.

    Thanks
    -LHoffman


  7. #7
    Join Date
    Aug 1999
    Posts
    18

    Never Mind

    Thomas,

    Thanks for the help..... I got it working using your suggestions. Then I found GetSysColorBrush() method and passed in the parameter COLOR_MENU. This gave me what I needed... Once again thanks.

    -LHoffman


  8. #8
    Join Date
    Sep 1999
    Location
    Europe / Austria / Innsbruck
    Posts
    442

    Re: Never Mind

    Changing the standard behaviour of derived controls is often quite tricky. Always use the reflected messages if you want to do so. I'm happy that i could help you! :-)


  9. #9
    Join Date
    Aug 1999
    Posts
    18

    Re: Never Mind

    Thomas,

    once again..... thanks.

    p.s. you bet I will remember to use the reflected messages for future controls.....

    LHoffman
    Email: [email protected]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured