Creating a Rotating Dialog Box


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Environment: VC6 SP4 MS-Win98/2K


Very recently, I read an article on CodeGuru that spoke about making custom-shaped windows. Here is an application that demonstrates this capability by making dialog boxes of three different shapes and switching among those shapes at run time. Additionally, it demonstrates the capability of rotating those custom-shaped windows. The way I have approached the rotation problem may not make certain people very happy (especially the mathematicians-turned-programmers and those programmers who are concerned of the usage of memory). But this application is targeted solely to demonstrate the capability of rotation and the use of the CRgn class to make custom-shaped windows. The computations can surely be optimized by considering the facts of mathematics.

Approach to Making Custom-Shaped Windows

Any rectangular window can be changed to a circle, ellipse, hexagon, pentagon, star, sun, and what not. Just think of a shape and it's possible. All that is needed is a little bit of mathematics. Because custom-shaped windows can be drawn only in their client area, the vertices that enclose the custom-shape have to be either calculated at run time or the size of the window has to be fixed. Also, to make a window custom, either the size of the client area of the window has to be large enough to fit the shape or the shape itself has to be calculated at initialization of the window so that it fits completely in the client area. I took the second approach and built the custom-shape at initialization by deriving all the calculations from the center-of-rectangle of the client area. Once the coordinates of the shape are known, the rest is just a few lines of code. An example follows:

Say, the 'n' vertices are stored in the array of CPoints m_Vertices.

// Declare a variable to hold the region for the window
// This variable has to be in scope until the window is visible
// or active.
// Usually declared as the member of the dialog class
  CRgn m_WndRgn;

// Create the region from the polygon points
  m_WndRgn.CreatePolygonRgn(&m_Vertices, n, WINDING);

// Set the created region
  SetWindowRgn(m_Vertices, TRUE);

This is all is required and you can see the window change its shape.

Approach to Rotating the Window

A custom-shaped window is surely enclosed by a set of vertices that define the coordinates of the shape and all those vertices can be enclosed in a circle. For example, say the custom shape was the star (has ten vertices). The radius of the smallest circle that encloses the star completely will be the distance from the center of the star to either of the five outer-vertices. If the vertices of the shape are calculated with respect to this circle, the complete window can be repositioned at a different angles easily. As in the figure, the vertex points can be calculated with at an interval of 10 degrees so that the window can be rotated by that amount on every time pulse.

For the sole purpose of the ease of the demonstration, I store all the vertices (at an interval of 1 degree) of each custom shape in an array (360 rows and n—the number of vertices—columns) where the index of the array maps the current angle of the shape. Finally, to rotate the window, a timer can be set to modify the window region at regular intervals.

The code in the demo application speaks the rest.

Happy coding!!!


Download demo project - 19 Kb


  • rotate the controls inside the dialog

    Posted by aymanmt on 05/18/2009 12:28pm

    hello Hitesh, I was wondering, is it possible to rotate all the controls inside the dialog, with the dialog??

  • a bug

    Posted by lucubrator on 11/15/2005 10:18pm

    array run over in wheel points setting. should modify the definition of array as following: CPoint m_Wheel[360][DIVISIONS+1]; // To hold the vertices of the wheel shape

  • It's Greate...

    Posted by Legacy on 02/26/2003 08:00am

    Originally posted by: Sungjin MOON


  • To get it working on Win95/98

    Posted by Legacy on 12/02/2002 08:00am

    Originally posted by: Hitesh Shah


    To make it working on windows versions before windows 2000 you will have to comment out two lines of code. One is the macro definition for WinNT in stdafx.h and the other in the InitDialog function that calls the function to make the window appear slowly.

    Comment those two lines and I guess it should be working in windows 95/98 as I check the code and couldn't fins anything else that could make the code uncompatible.

    Please do let me know if you got it running.

    - Hitesh

  • Super-Amilu

    Posted by Legacy on 11/26/2002 08:00am

    Originally posted by: Jayapraksh M

    Good work.

  • It is very interested!

    Posted by Legacy on 11/26/2002 08:00am

    Originally posted by: y5318

    It is very interested!When Rotate,there ia a angle(part) lost

  • Fantastic

    Posted by Legacy on 11/25/2002 08:00am

    Originally posted by: Bala

    Really Super

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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