Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Button Color In MFC

Button Color In MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
22 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 002comp

    No, I am not handling this Message .But i tried to inherit Class from CButton and tried to set color. But no use. And i want to change the color at runtime also.So,for that Do I need to Override Drawitem? What its Actual Working ? Some Ideas ?? Regards Yogesh

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #9

    yogeshs wrote:

    No, I am not handling this Message .But i tried to inherit Class from CButton and tried to set color.
     
    But no use.

    I think you have to use ON_NOTIFY_REFLECT() in the button's message map, or ON_NOTIFY() in the parent (i.e., dialog).

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

    1 Reply Last reply
    0
    • D David Crow

      JohnCz wrote:

      WM_CTLCOLORBTN does not work for buttons, since it draws face and borders.

      So MSDN's "The parent window can change the button's text and background colors" statement is wrong?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

      J Offline
      J Offline
      JohnCz
      wrote on last edited by
      #10

      This is out of context. Can you send me a link to the full statement?

      JohnCz

      enhzflepE 1 Reply Last reply
      0
      • J JohnCz

        This is out of context. Can you send me a link to the full statement?

        JohnCz

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #11

        Here[^]

        J 1 Reply Last reply
        0
        • enhzflepE enhzflep

          Here[^]

          J Offline
          J Offline
          JohnCz
          wrote on last edited by
          #12

          MSDN states also (below the line you have quoted): "By default, the DefWindowProc function selects the default system colors for the button. Buttons with the BS_PUSHBUTTON, BS_DEFPUSHBUTTON, or BS_PUSHLIKE styles do not use the returned brush. Buttons with these styles are always drawn with the default system colors. Drawing push buttons requires several different brushes-face, highlight, and shadow-but the WM_CTLCOLORBTN message allows only one brush to be returned. To provide a custom appearance for push buttons, use an owner-drawn button. For more information, see Creating Owner-Drawn Controls." This is also incorrect, as you can see from the sample code at the bottom. Radio and check boxes use static background color. If you change styles to "push like" color of the face will also not change. As you can see parent window is not able to change button’s face color of the pushbutton. In conclusion, MSDN is incorrect (not the first time).

          JohnCz

          0 1 Reply Last reply
          0
          • J JohnCz

            MSDN states also (below the line you have quoted): "By default, the DefWindowProc function selects the default system colors for the button. Buttons with the BS_PUSHBUTTON, BS_DEFPUSHBUTTON, or BS_PUSHLIKE styles do not use the returned brush. Buttons with these styles are always drawn with the default system colors. Drawing push buttons requires several different brushes-face, highlight, and shadow-but the WM_CTLCOLORBTN message allows only one brush to be returned. To provide a custom appearance for push buttons, use an owner-drawn button. For more information, see Creating Owner-Drawn Controls." This is also incorrect, as you can see from the sample code at the bottom. Radio and check boxes use static background color. If you change styles to "push like" color of the face will also not change. As you can see parent window is not able to change button’s face color of the pushbutton. In conclusion, MSDN is incorrect (not the first time).

            JohnCz

            0 Offline
            0 Offline
            002comp
            wrote on last edited by
            #13

            Hello I used CButon class to create button and then i override the DrawItem in Window class. But its not Coming on Drawitem,where I am updating the color of Button. Thanks

            0 1 Reply Last reply
            0
            • 0 002comp

              Hello I used CButon class to create button and then i override the DrawItem in Window class. But its not Coming on Drawitem,where I am updating the color of Button. Thanks

              0 Offline
              0 Offline
              002comp
              wrote on last edited by
              #14

              Hey Friends i got something That is working but need some help from you. Here is the way.I created a new class CColorButton inherit from CButton. And override functions paint and draw to set color. But when I am attaching my ButtonId to object of CCOlorbutton,it is giving some assertion. I am trying like this CColorbutton* pBtn = new CColorButton(); pBtn->Attach(GetDlgItemId( IDC_BUTTON_ID + ndx )->GetSafeHwnd()); Regards Yogesh

              J D 2 Replies Last reply
              0
              • 0 002comp

                Hey Friends i got something That is working but need some help from you. Here is the way.I created a new class CColorButton inherit from CButton. And override functions paint and draw to set color. But when I am attaching my ButtonId to object of CCOlorbutton,it is giving some assertion. I am trying like this CColorbutton* pBtn = new CColorButton(); pBtn->Attach(GetDlgItemId( IDC_BUTTON_ID + ndx )->GetSafeHwnd()); Regards Yogesh

                J Offline
                J Offline
                JohnCz
                wrote on last edited by
                #15

                yogeshs, I think you are confusing two things: Windows window with MFC object that is not a window. MFC object encapsulates window handle and only after window handle is valid, such an object represent Windows window. There are three ways to make MFC object alive (assign a valid handle): Calling member function Create for a given class Calling Attach Subclassing window Only calling Create and Subclassing will allow MFC object handling messages of the attached window. Calling Attach will not allow process any messages processing, since window procedure has not been replaced by the object’s window procedure. You should post a snippet that shows a function where it assert. Your code snippet does not make much sense since the meaning of the GetDlgItemId is unknown. Regardless, it looks like you are trying to attach an object that is already alive, meaning Windows window has MFC object attached to it. MFC code does it for you. Please answer one question that I have already asked: What version of Visual Studio you are using? Maybe taking it from here will be more beneficial for you.

                JohnCz

                0 1 Reply Last reply
                0
                • 0 002comp

                  Hey Friends i got something That is working but need some help from you. Here is the way.I created a new class CColorButton inherit from CButton. And override functions paint and draw to set color. But when I am attaching my ButtonId to object of CCOlorbutton,it is giving some assertion. I am trying like this CColorbutton* pBtn = new CColorButton(); pBtn->Attach(GetDlgItemId( IDC_BUTTON_ID + ndx )->GetSafeHwnd()); Regards Yogesh

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #16

                  yogeshs wrote:

                  CColorbutton* pBtn = new CColorButton();
                  pBtn->Attach(GetDlgItemId( IDC_BUTTON_ID + ndx )->GetSafeHwnd());

                  It's highly unlikely that you would need to do this. Why not just replace CButton with CColorbutton in your dialog's .h file?

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  0 1 Reply Last reply
                  0
                  • J JohnCz

                    yogeshs, I think you are confusing two things: Windows window with MFC object that is not a window. MFC object encapsulates window handle and only after window handle is valid, such an object represent Windows window. There are three ways to make MFC object alive (assign a valid handle): Calling member function Create for a given class Calling Attach Subclassing window Only calling Create and Subclassing will allow MFC object handling messages of the attached window. Calling Attach will not allow process any messages processing, since window procedure has not been replaced by the object’s window procedure. You should post a snippet that shows a function where it assert. Your code snippet does not make much sense since the meaning of the GetDlgItemId is unknown. Regardless, it looks like you are trying to attach an object that is already alive, meaning Windows window has MFC object attached to it. MFC code does it for you. Please answer one question that I have already asked: What version of Visual Studio you are using? Maybe taking it from here will be more beneficial for you.

                    JohnCz

                    0 Offline
                    0 Offline
                    002comp
                    wrote on last edited by
                    #17

                    I am using Vs2008 professional. Thanks For Your Reply. Trying To Understand. Thanks Yogesh

                    J 1 Reply Last reply
                    0
                    • 0 002comp

                      I am using Vs2008 professional. Thanks For Your Reply. Trying To Understand. Thanks Yogesh

                      J Offline
                      J Offline
                      JohnCz
                      wrote on last edited by
                      #18

                      OK, that is good so far. If you have SP1 installed, why not to follow my previous advice (from my earlier response): If you are using VS 2008 with feature pack or with SP1 use CMFCButton class. To set button face color, call SetFaceColor member. You have to set m_bTransparent to FALSE anf disable teaming by calling EnableWindowsTheming member and passing FALSE as parameter. This is much easier than reinventing a wheel.

                      JohnCz

                      0 1 Reply Last reply
                      0
                      • J JohnCz

                        OK, that is good so far. If you have SP1 installed, why not to follow my previous advice (from my earlier response): If you are using VS 2008 with feature pack or with SP1 use CMFCButton class. To set button face color, call SetFaceColor member. You have to set m_bTransparent to FALSE anf disable teaming by calling EnableWindowsTheming member and passing FALSE as parameter. This is much easier than reinventing a wheel.

                        JohnCz

                        0 Offline
                        0 Offline
                        002comp
                        wrote on last edited by
                        #19

                        Hello JohnCz I tried to use CMFCButton but there is no class in VS2008 prof. Do i need to link up something? Thanks Again. Regards Yogesh

                        D J 2 Replies Last reply
                        0
                        • D David Crow

                          yogeshs wrote:

                          CColorbutton* pBtn = new CColorButton();
                          pBtn->Attach(GetDlgItemId( IDC_BUTTON_ID + ndx )->GetSafeHwnd());

                          It's highly unlikely that you would need to do this. Why not just replace CButton with CColorbutton in your dialog's .h file?

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                          0 Offline
                          0 Offline
                          002comp
                          wrote on last edited by
                          #20

                          Hello david I didnt get your point. Regards Yogesh

                          1 Reply Last reply
                          0
                          • 0 002comp

                            Hello JohnCz I tried to use CMFCButton but there is no class in VS2008 prof. Do i need to link up something? Thanks Again. Regards Yogesh

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #21

                            yogeshs wrote:

                            I tried to use CMFCButton...

                            How? When you added a button to the dialog template and went to assign a variable to it, the Add Member Variable Wizard defaulted to CButton. All you need to do is change that to CMFCButton. The appropriate header file (i.e., afxbutton.h) would have been added automatically, if it was not already included.

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                            1 Reply Last reply
                            0
                            • 0 002comp

                              Hello JohnCz I tried to use CMFCButton but there is no class in VS2008 prof. Do i need to link up something? Thanks Again. Regards Yogesh

                              J Offline
                              J Offline
                              JohnCz
                              wrote on last edited by
                              #22

                              Hi yogeshs, You have to learn how to be patient and read all answers and documentation without skipping some parts. I have already mentioned twice: If you are using VS 2008 with feature pack or with SP1 use CMFCButton class. You need Visual Studio SP1 installed. Web installer[^] ISO image[^] Than you can subclass button using CMFCCustButton. Subclassing is easy if you use wizard: Right click on the button in the resource editor and choose Add Variable. The only option you have is CButton, so select it. After wizard closes, open dialog’s header file and change CButton to CMFCMenuButton.

                              JohnCz

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups