Getting a docking toolbar in a dialog is not easy. When I asked Jeff Prosise, he replied that it comes with an SDI app, and Microsoft gives you the code. It is not easy, but it is doable. I did not see Microsoft’s code, but went on to write my own.
I do a small trick. I don’t start with a toolbar; instead, I take a dialog, put controls in it, and make it behave like a toolbar. Many may not like it, but with a dialog pretending to be a toolbar, a lot of GUI gimmicks can be incorporated easily. The main feat is to make it dock on all four sides.
Working
Let me start form the beginning. Take a dialog, namely IDD_LIKETOOLBAR, remove the caption, set its style to child, and put controls in it. Derive a class out of the newly taken dialog and copy/paste the downloaded code. I take four invisible rectangles, one on the top, one on the bottom, one on the right, and one on the left of the main dialog. To see what they look like, click the OK button on the main dialog. If the mouse falls in any one of these rectangles, the toolbar dialog will align itself with that particular rectangle; otherwise, the dialog (IDD_LIKETOOLBAR) will get a caption.
You can remove the commented code in the OnPaint(..) of the main dialog to see the rectangles. Now, intercept OnLButtonDown(), OnMouseMove(), and OnLbuttonUp() of the IDD_LIKETOOLBAR as shown in the code. Track its movements and use DrawDragRect(..) to erase the old rect position and paint a new rectangle in the current mouse position simultaneously.
The starting position of the IDD_LIKETOOLBAR is horizontal, so it’s no problem, but whenever IDD_LIKETOOLBAR needs to be arranged vertically, I call ArrangeControlsVertically() to realign all the controls vertically. Whenever IDD_LIKETOOLBAR needs to be arranged horizontally, I call ArrangeControlsHorizontally() to realign all the controls horizontally. I use SetWindowLong(m_hWnd,GWL_STYLE,…) to put the caption on the IDD_LIKETOOLBAR and if it does not fall in any one of the pre-assigned docking rectangles, again I use SetWindowLong(m_hWnd,GWL_STYLE,…) to remove the caption if it falls in one of the pre-assigned docking rectangles. The final trick I add is that I do not allow the mouse to move when the Lbutton is down beyond the client area. This is done to see that the IDD_LIKETOOLBAR does not go beyond the client area. you are free to do this however you want.
It may seem like a lot of code, but once written, they remain standard for a long long time to come. In my next article, I will use the same technique to make a real toolbar to dock in a dialog application. As the saying goes, no program is without bugs. A few definitely crept in here, too. I think they can be fixed without much ado or tension.