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. How to Add afunction for Toolbar Button in VC++7 (not in VC++6)

How to Add afunction for Toolbar Button in VC++7 (not in VC++6)

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
6 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.
  • E Offline
    E Offline
    Exceter
    wrote on last edited by
    #1

    :eek:

    X 1 Reply Last reply
    0
    • E Exceter

      :eek:

      X Offline
      X Offline
      xxhimanshu
      wrote on last edited by
      #2

      ;)hi, this is what you are looking for.. Since Microsoft got rid of ClassWizard in VC++ 7, quite a lot of jobs have become much more difficult. This article shows how to hook up the normal click events for toolbars in VC7, and how to disable buttons programmatically, depending on context. First, let us assume we have an SDI application created using AppWizard. The buttons are called ID_TBRED, ID_TBGREEN, and ID_TBBLUE respectively. Creating Event Functions for the Buttons If we built the program above just by adding in the toolbar buttons, they would by default be disabled, which is not very useful. Now we want to add an event function to each button so they do something when clicked. First, let's create the functions for each of them. If you are following my SDI example, it is likely you will want to define the functions in your view. Open the header file for your view (double-click the class name in Class View), this is CDisableToolbarView in the example. Okay; we need to write the declarations for our three functions; these would be as so: afx_msg void OnToolbarRedClick(); afx_msg void OnToolbarGreenClick(); afx_msg void OnToolbarBlueClick(); That's all we need in our header, so we can close that and open our cpp file. For each of the three functions declared above, we need to create a function defintition like the one below. void CDisableToolbarView::OnToolbarRedClick() { MessageBox("Red clicked"); } We've now finished creating the event functions. As they stand, VC++ doesn't know what events to hook our functions up to, so we need to specify this. Toward the top of the cpp file you will see a block of code starting with BEGIN_MESSAGE_MAP. Within these lines we can hook the click events up to our functions. Between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP lines, add the following three lines: ON_BN_CLICKED(ID_TBRED,OnToolbarRedClick) ON_BN_CLICKED(ID_TBGREEN,OnToolbarGreenClick) ON_BN_CLICKED(ID_TBBLUE,OnToolbarBlueClick) By building the application, you should now see the three buttons enabled, and clicking them should show the respective message box. That's all there is to it! cheers Himanshu

      R E 2 Replies Last reply
      0
      • X xxhimanshu

        ;)hi, this is what you are looking for.. Since Microsoft got rid of ClassWizard in VC++ 7, quite a lot of jobs have become much more difficult. This article shows how to hook up the normal click events for toolbars in VC7, and how to disable buttons programmatically, depending on context. First, let us assume we have an SDI application created using AppWizard. The buttons are called ID_TBRED, ID_TBGREEN, and ID_TBBLUE respectively. Creating Event Functions for the Buttons If we built the program above just by adding in the toolbar buttons, they would by default be disabled, which is not very useful. Now we want to add an event function to each button so they do something when clicked. First, let's create the functions for each of them. If you are following my SDI example, it is likely you will want to define the functions in your view. Open the header file for your view (double-click the class name in Class View), this is CDisableToolbarView in the example. Okay; we need to write the declarations for our three functions; these would be as so: afx_msg void OnToolbarRedClick(); afx_msg void OnToolbarGreenClick(); afx_msg void OnToolbarBlueClick(); That's all we need in our header, so we can close that and open our cpp file. For each of the three functions declared above, we need to create a function defintition like the one below. void CDisableToolbarView::OnToolbarRedClick() { MessageBox("Red clicked"); } We've now finished creating the event functions. As they stand, VC++ doesn't know what events to hook our functions up to, so we need to specify this. Toward the top of the cpp file you will see a block of code starting with BEGIN_MESSAGE_MAP. Within these lines we can hook the click events up to our functions. Between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP lines, add the following three lines: ON_BN_CLICKED(ID_TBRED,OnToolbarRedClick) ON_BN_CLICKED(ID_TBGREEN,OnToolbarGreenClick) ON_BN_CLICKED(ID_TBBLUE,OnToolbarBlueClick) By building the application, you should now see the three buttons enabled, and clicking them should show the respective message box. That's all there is to it! cheers Himanshu

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

        Himanshu, do you know you can do it with the Class Wizard ? So you need not to add all the MFC stuff "by hand"... ~RaGE();

        X 1 Reply Last reply
        0
        • R Rage

          Himanshu, do you know you can do it with the Class Wizard ? So you need not to add all the MFC stuff "by hand"... ~RaGE();

          X Offline
          X Offline
          xxhimanshu
          wrote on last edited by
          #4

          ;)what if there is no classwizard...boom... have you seen it is vc++7 not 6..and it doesn't support class wizard..so you have to do it manually..:) cheers Himanshu

          1 Reply Last reply
          0
          • X xxhimanshu

            ;)hi, this is what you are looking for.. Since Microsoft got rid of ClassWizard in VC++ 7, quite a lot of jobs have become much more difficult. This article shows how to hook up the normal click events for toolbars in VC7, and how to disable buttons programmatically, depending on context. First, let us assume we have an SDI application created using AppWizard. The buttons are called ID_TBRED, ID_TBGREEN, and ID_TBBLUE respectively. Creating Event Functions for the Buttons If we built the program above just by adding in the toolbar buttons, they would by default be disabled, which is not very useful. Now we want to add an event function to each button so they do something when clicked. First, let's create the functions for each of them. If you are following my SDI example, it is likely you will want to define the functions in your view. Open the header file for your view (double-click the class name in Class View), this is CDisableToolbarView in the example. Okay; we need to write the declarations for our three functions; these would be as so: afx_msg void OnToolbarRedClick(); afx_msg void OnToolbarGreenClick(); afx_msg void OnToolbarBlueClick(); That's all we need in our header, so we can close that and open our cpp file. For each of the three functions declared above, we need to create a function defintition like the one below. void CDisableToolbarView::OnToolbarRedClick() { MessageBox("Red clicked"); } We've now finished creating the event functions. As they stand, VC++ doesn't know what events to hook our functions up to, so we need to specify this. Toward the top of the cpp file you will see a block of code starting with BEGIN_MESSAGE_MAP. Within these lines we can hook the click events up to our functions. Between the BEGIN_MESSAGE_MAP and END_MESSAGE_MAP lines, add the following three lines: ON_BN_CLICKED(ID_TBRED,OnToolbarRedClick) ON_BN_CLICKED(ID_TBGREEN,OnToolbarGreenClick) ON_BN_CLICKED(ID_TBBLUE,OnToolbarBlueClick) By building the application, you should now see the three buttons enabled, and clicking them should show the respective message box. That's all there is to it! cheers Himanshu

            E Offline
            E Offline
            Exceter
            wrote on last edited by
            #5

            Great thanx a lot this was the what I was looking for I have another question related to this: how to add functions such as in VC++6 OnDraw() for SDI and MDI, InitInstance(),.. thanx.

            X 1 Reply Last reply
            0
            • E Exceter

              Great thanx a lot this was the what I was looking for I have another question related to this: how to add functions such as in VC++6 OnDraw() for SDI and MDI, InitInstance(),.. thanx.

              X Offline
              X Offline
              xxhimanshu
              wrote on last edited by
              #6

              :)I guess thats something like this.. virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid); public: virtual BOOL InitInstance(); i guess that should work fine with your code.. cheers Himanshu

              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