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