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

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds