Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Sizing/Moving Widget

0.00/5 (No votes)
1 Feb 2000 1  
A class that provides the ability to move and size controls at run-time
In this article, you will see a class that provides the ability to move and size controls at run time

Sample Image - CSizer.gif

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:

  1. Cursor Position - Changes the mouse cursor
  2. 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

  1. Add the files WidgetSizer.cpp & WidgetSizer.h to your project.
  2. Somewhere in your project, define a global pointer var of type CWidgetSizer.
  3. In your main class implementation file, define this pointer and nullify it.
  4. In each of your controls, include the header file where the declaration extern CWidgetSizer *myvar lives.
  5. 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.

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