In this article, you will see a class that provides the ability to move and size controls at run time
Description
The class described here is derived from CStatic
, to provide the ability to move and size controls at run-time. I wrote this specifically for a dialog editor as part of a much larger project. Admittedly, it is of little value except in the application described but as I looked everywhere for this and couldn't find one, I thought other people might be looking too.
As you can see, I tried to recreate the look of the one to be found in the MSVC dialog editor. Moving the cursor over any of the "sizing handles" changes the cursor to indicate that dragging is supported. Once the left mouse button is pressed, you can size the control as desired. Moving the cursor over any other part of the control displays a IDC_SIZEALL
mouse cursor, indicating that moving is supported. Holding the left mouse button down allows you to move the control.
On moving or sizing the target control is sized/moved, allowing any control to be adjusted. Unlike the one provided in MSVC's dialog editor, the target control is not "obscured" by this control, so it can respond itself to mouse events.
The guts of this class reside in the OnMouseMove
function, taking action there depending on:
- Cursor Position - Changes the mouse cursor
- Action Flags - If the left mouse button is down what action to take
Known Problems
If, once dragging or sizing has started, the mouse is moved too rapidly, the control can't keep up and loses focus, and therefore dragging or sizing stops immediately.
How to Use
- Add the files WidgetSizer.cpp & WidgetSizer.h to your project.
- Somewhere in your project, define a global pointer
var
of type CWidgetSizer
. - In your main class implementation file, define this pointer and nullify it.
- In each of your controls, include the header file where the declaration
extern CWidgetSizer *myvar
lives. - Insert code to create the
CWidgetSizer
class where appropriate to your application. For example, when the target control is created.
if (g_pWidgetSizer)
{
delete g_pWidgetSizer;
g_pWidgetSizer = NULL;
}
g_pWidgetSizer = new CWidgetSizer(m_pParent, this);
or when left mouse button down is called:
if (g_pWidgetSizer)
{
if (g_pWidgetSizer->AmITheTarget(this))
return;
delete g_pWidgetSizer;
g_pWidgetSizer = NULL;
}
g_pWidgetSizer = new CWidgetSizer(m_pParent, this);
Request
If anyone has any ideas/suggestions/improvements, please email me so I can improve this.
History
- 2nd February, 2000: Initial version
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.
A list of licenses authors might use can be found here.