Recently learning MFC, watching is xinxin Sun VC + + video, feel good. When I see the Nineth class, when I talk about the toolbar, I do a little function: The toolbar is hidden and displayed, but there is a bug that a suspended toolbar is hidden and then displayed, it becomes docked. Here I again the hidden and display of the toolbar has been implemented again, remove the bug, to do with the MFC in the menu bar View-like toolbar menu items the same function.
In fact, about the hidden and display of the toolbar, only need to consider three states: (1) in the docking state of the display and hide the toolbar, (2) in the suspended state of the display and hide the toolbar, (3) in the suspended state, by clicking the Close button to close the toolbar, and then display the toolbar problem.
For the second case described above, remember that the toolbar is in the window coordinates (x, y) of the moment it will be hidden, which can be done when the menu item is clicked to hide the toolbar. Also for the third case, remember the coordinates of the toolbar at the closing moment, where we can create a class Cmytoolbar, which inherits from CToolBar, and adds wm_ to this class. Windowposchanged Message handler function onwindowposchanged. This message is triggered whenever the size or position of the toolbar changes, so we will first interpret the message as if it is suspended in its processing function, and then save it if the position of the current window is taken.
Well, it says so much now, start with the code. First look at the variable members you want to add in CMainFrame.h.
Then add the corresponding function for the toolbar menu item in CMainFrame.cpp, with the following code:
Finally, we will create a Cmytoolbar class that inherits from the CToolBar class, where CMyToolbar.h adds the following code:
Then implement the OnWindowPosChanged function in the Cmytoolbar
Here, the Code section is all over. Run Results Demo:
MFC toolbar hidden and displayed