Click here to Skip to main content
15,881,248 members
Articles / Desktop Programming / MFC

A Sizing/Moving Widget

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
1 Feb 2000 81.4K   2.1K   19   9
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.
    C++
    if (g_pWidgetSizer)
    {
    	delete g_pWidgetSizer;
    	g_pWidgetSizer = NULL;
    }
    
    g_pWidgetSizer = new CWidgetSizer(m_pParent, this);

    or when left mouse button down is called:

    C++
    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.


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSetFocus() Pin
Christian Graus14-May-01 17:59
protectorChristian Graus14-May-01 17:59 
GeneralDoes not compile. Pin
Julien14-May-01 17:54
Julien14-May-01 17:54 
GeneralSizing Bug & fix Pin
KJ5-Feb-00 11:58
KJ5-Feb-00 11:58 
GeneralRe: Sizing Bug & fix Pin
Paul Selormey11-Mar-00 18:19
Paul Selormey11-Mar-00 18:19 
Generalfor G-d sake don't use registry for that Pin
Poor WIndows User3-Feb-00 19:07
sussPoor WIndows User3-Feb-00 19:07 
GeneralOther applications Pin
KJ3-Feb-00 15:03
KJ3-Feb-00 15:03 
A project I'm working on requires a site plan that the user can place objects onto. These objects can be moved & resized.

Another use in the same project is for printing badges. The operator can choose fields from the database to print on the badge. They design the layout on the badge template and mailmerge the database to print them all.

Thanks for a great ide
GeneralThis class already exists with MFC Pin
Jerome3-Feb-00 3:28
Jerome3-Feb-00 3:28 
GeneralUpdate to CSizer class Pin
Andrew JM Hall2-Feb-00 23:49
Andrew JM Hall2-Feb-00 23:49 
GeneralPersistency will improve value Pin
Mark2-Feb-00 22:11
Mark2-Feb-00 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.