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.

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.

Downloads

Download source - 34 Kb


Comments

  • 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

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

  • Live Event Date: December 18, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this upcoming webcast …

Most Popular Programming Stories

More for Developers

RSS Feeds