Putting a Docking Toolbar in a Dialog App

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.


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.


Download source - 34 Kb


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As the mobile enterprise marketplace expands and customer needs grow more diverse, Samsung recognizes that solution partners and developers play an essential role by continually innovating to meet their customers' needs. Samsung works to provide these developers and partners with the latest tools and resources needed to create these solutions. Read this program guide to learn how the Samsung Enterprise Alliance Program provides partners and developers with Samsung enterprise software development kits (SDKs) …

  • Thanks to the Internet of Things (IoT), physical assets are turning into participants in real-time global digital markets. The countless types of assets around us will become as easily indexed, searched and traded as any online commodity. While some industries will be tougher to transform than others – those with physical limitations, such as manufacturing, will be harder to digitize – untold economic opportunities exist for growth and advancement. Our research shows this will create a new "Economy …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date