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. CToolBar disable/enable buttons

CToolBar disable/enable buttons

Scheduled Pinned Locked Moved C / C++ / MFC
comhelptutorial
15 Posts 3 Posters 0 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.
  • R Offline
    R Offline
    Romiks
    wrote on last edited by
    #1

    Hi All, I have used the code from http://www.codeproject.com/docking/ctruecolortoolbar_update.asp article. But I can't understand how to enable/disable buttons. Please help me to do it :confused: Thanks

    M 1 Reply Last reply
    0
    • R Romiks

      Hi All, I have used the code from http://www.codeproject.com/docking/ctruecolortoolbar_update.asp article. But I can't understand how to enable/disable buttons. Please help me to do it :confused: Thanks

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      One way is to use "command enablers"...

      BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
      ...
      ON_UPDATE_COMMAND_UI(ID_BUTTONID, OnUpdateButton)
      ...
      END_MESSAGE_MAP()

      void CMyFrameWnd::OnUpdateButton(CCmdUI* pCmdUI)
      {
      if (some_condition)
      pCmdUI->Enable(TRUE);
      else
      pCmdUI->Enable(FALSE);
      }

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      R 1 Reply Last reply
      0
      • M Mark Salsbery

        One way is to use "command enablers"...

        BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)
        ...
        ON_UPDATE_COMMAND_UI(ID_BUTTONID, OnUpdateButton)
        ...
        END_MESSAGE_MAP()

        void CMyFrameWnd::OnUpdateButton(CCmdUI* pCmdUI)
        {
        if (some_condition)
        pCmdUI->Enable(TRUE);
        else
        pCmdUI->Enable(FALSE);
        }

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        R Offline
        R Offline
        Romiks
        wrote on last edited by
        #3

        Mark thanks for reply, but I have dialog based application, there is no ON_UPDATE_COMMAND_UI :(

        N M 2 Replies Last reply
        0
        • R Romiks

          Mark thanks for reply, but I have dialog based application, there is no ON_UPDATE_COMMAND_UI :(

          N Offline
          N Offline
          Nelek
          wrote on last edited by
          #4

          Then try in OnPaint. if (condition) button->EnableWindow (TRUE); else button->EnableWindow (FALSE); is not the best way, but it will maybe work NOTE: I have not read the article, so im not sure if I'm saying something catastrofical ;) :P

          Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

          1 Reply Last reply
          0
          • R Romiks

            Mark thanks for reply, but I have dialog based application, there is no ON_UPDATE_COMMAND_UI :(

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            :doh: Well, anytime after a toolbar is created and the buttons are added to it, you can enable and diable buttons when you need to: // Disable a button m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON1, FALSE); // Enable a button m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON1, TRUE); Mark

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            R 1 Reply Last reply
            0
            • M Mark Salsbery

              :doh: Well, anytime after a toolbar is created and the buttons are added to it, you can enable and diable buttons when you need to: // Disable a button m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON1, FALSE); // Enable a button m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON1, TRUE); Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              R Offline
              R Offline
              Romiks
              wrote on last edited by
              #6

              Mark, many thanks it works:), but button was not greyed :^).

              M 1 Reply Last reply
              0
              • R Romiks

                Mark, many thanks it works:), but button was not greyed :^).

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                Romiks wrote:

                but button was not greyed

                Looking at the article, it looks like there's a separate image list for disabled. Are the images in that image list the same as the regular images? You could try omitting the last parameter in the call to LoadTrueColorToolBar() - the system should do its default "greying" for disabled buttons. Mark

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                R 1 Reply Last reply
                0
                • M Mark Salsbery

                  Romiks wrote:

                  but button was not greyed

                  Looking at the article, it looks like there's a separate image list for disabled. Are the images in that image list the same as the regular images? You could try omitting the last parameter in the call to LoadTrueColorToolBar() - the system should do its default "greying" for disabled buttons. Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  R Offline
                  R Offline
                  Romiks
                  wrote on last edited by
                  #8

                  Mark, many thanks for help. I have tried to omit the last parameter, but it doesn't work :doh:. I will try to use CButton instead of tool bar.

                  M 1 Reply Last reply
                  0
                  • R Romiks

                    Mark, many thanks for help. I have tried to omit the last parameter, but it doesn't work :doh:. I will try to use CButton instead of tool bar.

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    So the buttons diable but they don't look disabled? Mark

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    R 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      So the buttons diable but they don't look disabled? Mark

                      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                      R Offline
                      R Offline
                      Romiks
                      wrote on last edited by
                      #10

                      Hi Mark, If I ommited the last parameter, buttons are not disabled. :(

                      M 2 Replies Last reply
                      0
                      • R Romiks

                        Hi Mark, If I ommited the last parameter, buttons are not disabled. :(

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        I just downloaded the source - I'll take a look. Mark

                        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                        1 Reply Last reply
                        0
                        • R Romiks

                          Hi Mark, If I ommited the last parameter, buttons are not disabled. :(

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #12

                          Ok.. It all works fine in the authors demo app. The author actually omits the last parametr in the call to CTrueColorToolBar::LoadTrueColorToolBar for the RIGHT toolbar. So, the right toolbar shows button bitmaps grayed by the system. For the left toolbar he uses special bitmaps for disabled buttons that don't have any color, just white/black/grays. All the buttons except the first three on left and right are disabled. If you try to disable them yourself they won't look any different :) To enable them, you can add command enablers. For example, there's 9 buttons on the toolbars. I enabled the 7th and 8th buttons like this (in MainFrm.cpp/.h):

                          // In MainFrm.h

                          // add these declarations to the CMainFrame class
                          afx_msg void OnUpdateButton32777(CCmdUI* pCmdUI);
                          afx_msg void OnUpdateButton32778(CCmdUI* pCmdUI);

                          // In MainFrm.cpp

                          // add this to CMainFrame message map
                          ON_UPDATE_COMMAND_UI(ID_BUTTON32777, OnUpdateButton32777)
                          ON_UPDATE_COMMAND_UI(ID_BUTTON32778, OnUpdateButton32778)

                          // add these handler methods
                          void CMainFrame::OnUpdateButton32777(CCmdUI* pCmdUI)
                          {
                          pCmdUI->Enable(TRUE);
                          }

                          void CMainFrame::OnUpdateButton32778(CCmdUI* pCmdUI)
                          {
                          pCmdUI->Enable(TRUE);
                          }

                          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                          R 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            Ok.. It all works fine in the authors demo app. The author actually omits the last parametr in the call to CTrueColorToolBar::LoadTrueColorToolBar for the RIGHT toolbar. So, the right toolbar shows button bitmaps grayed by the system. For the left toolbar he uses special bitmaps for disabled buttons that don't have any color, just white/black/grays. All the buttons except the first three on left and right are disabled. If you try to disable them yourself they won't look any different :) To enable them, you can add command enablers. For example, there's 9 buttons on the toolbars. I enabled the 7th and 8th buttons like this (in MainFrm.cpp/.h):

                            // In MainFrm.h

                            // add these declarations to the CMainFrame class
                            afx_msg void OnUpdateButton32777(CCmdUI* pCmdUI);
                            afx_msg void OnUpdateButton32778(CCmdUI* pCmdUI);

                            // In MainFrm.cpp

                            // add this to CMainFrame message map
                            ON_UPDATE_COMMAND_UI(ID_BUTTON32777, OnUpdateButton32777)
                            ON_UPDATE_COMMAND_UI(ID_BUTTON32778, OnUpdateButton32778)

                            // add these handler methods
                            void CMainFrame::OnUpdateButton32777(CCmdUI* pCmdUI)
                            {
                            pCmdUI->Enable(TRUE);
                            }

                            void CMainFrame::OnUpdateButton32778(CCmdUI* pCmdUI)
                            {
                            pCmdUI->Enable(TRUE);
                            }

                            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                            R Offline
                            R Offline
                            Romiks
                            wrote on last edited by
                            #13

                            Ok... I have dialog based MFC application ;) There is no ON_UPDATE_COMMAND_UI command :(

                            M 1 Reply Last reply
                            0
                            • R Romiks

                              Ok... I have dialog based MFC application ;) There is no ON_UPDATE_COMMAND_UI command :(

                              M Offline
                              M Offline
                              Mark Salsbery
                              wrote on last edited by
                              #14

                              Romiks wrote:

                              I have dialog based MFC application

                              Oh yeah, I forgot about that, sorry :-O Well I still go back to this: // Disable button ID_BUTTON32777 in m_ToolBar m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON32777, FALSE); // Enable button ID_BUTTON32777 in m_ToolBar m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON32777, TRUE); I'm not sure why that doesn't work for you - I just tested it in a dialog toolbar here. Mark

                              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                              R 1 Reply Last reply
                              0
                              • M Mark Salsbery

                                Romiks wrote:

                                I have dialog based MFC application

                                Oh yeah, I forgot about that, sorry :-O Well I still go back to this: // Disable button ID_BUTTON32777 in m_ToolBar m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON32777, FALSE); // Enable button ID_BUTTON32777 in m_ToolBar m_ToolBar.GetToolBarCtrl().EnableButton(ID_BUTTON32777, TRUE); I'm not sure why that doesn't work for you - I just tested it in a dialog toolbar here. Mark

                                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                                R Offline
                                R Offline
                                Romiks
                                wrote on last edited by
                                #15

                                If you have working dialog based project, could you please send me? my mail:losqut@yandex.ru As for me, buttons are not greyed :(

                                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