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

  • 10 Rules that Make or Break Enterprise App Development Projects In today's app-driven world, application development is a top priority. Even so, 68% of enterprise application delivery projects fail. Designing and building applications that pay for themselves and adapt to future needs is incredibly difficult. Executing one successful project is lucky, but making it a repeatable process and strategic advantage? That's where the money is. With help from our most experienced project leads and software engineers, …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds