Click here to Skip to main content
Click here to Skip to main content

Resizing Controls at run time

By , 30 Nov 2000
 

Sample Image - resize_at_runtime.gif

Introduction

Suppose you wanted to give the user the ability to modify the size and position on a certain control? This example shows how to implement resizing controls on a dialog box as it is done when drawing controls on a dialog template or using Visual Basic at design time.

In order to accomplish this, we can use the class CRectTracker to manage all the drawing and resizing of a rectangular frame which also has (optional) 6 resize handlers (as in the image above). First thing to do is to invoke a CRectTracker and specify given coordinates:

LPRECT rect = new RECT;
CWnd* wnd = (CWnd*)(GetDlgItem(IDC_EDIT1));
wnd->GetWindowRect(rect);
ScreenToClient(rect);
m_tracker = new CRectTracker(rect,CRectTracker::dottedLine  | 
                CRectTracker::resizeOutside | 
                CRectTracker::hatchedBorder );
m_tracker->Draw(pDC);

There are two events that are needed to be taken care of:

  1. SetCursor
    if (pWnd == this && m_tracker->SetCursor(this, nHitTest))
        return TRUE;

    This is done in order to draw the correct mouse cursors when floating the mouse pointer over the rectangle.

  2. OnLButtonDown
    m_tracker->Track(this, point, TRUE);
    Invalidate(FALSE);
    CDC* pDC = GetDC();
    m_tracker->Draw(pDC);

    This will take care of the drawing of the rectangle with resizing it.

Once you have finished, all you have to do is draw the control with the new rectangle coordinates:

LPRECT rect = new RECT;
CWnd* wnd = (CWnd*)(GetDlgItem(IDC_EDIT1));
rect = LPRECT(m_tracker->m_rect); 
wnd->MoveWindow(rect,TRUE) ;

That's it!

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

About the Author

Amit Nabarro
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCursor goes away when linked with Static Librarymembercdsmith29 Apr '03 - 6:34 
I am having a problem with CRectTracker. When I link with Shared DLL it works fine, when I link with Static Library the cursors (resize and move) do not show up when the mouse is over the tracking rect. I was going crazy thinking I was doing something wrong, but this code has exactly the same problem.
 
Anybody know why the cursors do not show up when statically linked?
 
Thanks!
 
Craig Smith
GeneralRe: Cursor goes away when linked with Static Library Pinmemberlesnikowski9 Jan '04 - 2:20 
Add _AFXDLL in Project's configuration Properties/ "C/C++" / Preprocessor / Preprocessor Definitions.
 
In Code Generation / RunTime Library use "Multi-Threaded Dll" (/MD)
 

GeneralRe: Cursor goes away when linked with Static Library Pinmembertohichoi19 Sep '05 - 20:57 
If you can use dynamically linked MFC dll, just follow up Mr. lesnikowski's suggestion.
 
Otherwise, initialize cursor resource by your hand. For example, simple initialization would be
 

 
extern HCURSOR _afxCursors[10];
 
BOOL _InitCursor()
{
HINSTANCE hInst=NULL;
 
if (!_afxCursors[0])
_afxCursors[0] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZENWSE));
 
if (!_afxCursors[1])
_afxCursors[1] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZENESW));
 
if (!_afxCursors[2])
_afxCursors[2] = _afxCursors[0];
 
if (!_afxCursors[3])
_afxCursors[3] = _afxCursors[1];
 
if (!_afxCursors[4])
_afxCursors[4] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZENS));
 
if (!_afxCursors[5])
_afxCursors[5] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEWE));
 
if (!_afxCursors[6])
_afxCursors[6] = _afxCursors[4];
 
if (!_afxCursors[7])
_afxCursors[7] = _afxCursors[5];
 
if (!_afxCursors[8])
_afxCursors[8] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEALL));
 
if (!_afxCursors[9])
_afxCursors[9] = ::LoadCursor(hInst, MAKEINTRESOURCE(IDC_SIZEALL));
 
return TRUE;
}
 
BOOL CAdjustWBoxDialog::OnInitDialog()
{
...
m_tracker=new CRectTracker(rect, CRectTracker::solidLine|CRectTracker::resizeOutside);
_InitCursor();
...
}

 
Please note that _InitCursor() should be called after instantiation of m_tracker(see the MFC source trckrect.cpp).
 

GeneralRe: Cursor goes away when linked with Static Library PinmemberM A V11 Jul '08 - 6:56 
See this article http://support.microsoft.com/kb/208856[^]
 
CAUSE
When an application is linked to MFC by using the static library, the MFC resources are compiled into the executable of an application. The executable of the application will include the Afxres.rc file. To check this, click Resource Includes on the View menu. The Afxres.rc file has the resources that CRectTracker uses in the MFC source code.
 
These resources are only included when _AFX_NO_TRACKER_RESOURCES is not defined. The BLOCKS32 project has _AFX_NO_TRACKER_RESOURCES defined. Therefore, it does not put the resources that CRectTracker requires in the executable image of the application. Therefore, none of the cursors that are used by CRectTracker will show up when you build the sample by using MFC statically.
 

RESOLUTION
1. On the View menu, click Resource Includes.
2. Delete the following line:
 
#define _AFX_NO_TRACKER_RESOURCES
AnswerRe: Cursor goes away when linked with Static Library PinmemberAnthony Dunk1 Jul '09 - 19:34 
On VS2008, I found that the "Resource Includes" item is under the Edit menu.
 
The line in the .rc was as follows:
 
#define _AFX_NO_SPLITTER_RESOURCES
 
When I deleted this in my application (which is statically linked to MFC), the splitter cursors returned.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 1 Dec 2000
Article Copyright 2000 by Amit Nabarro
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid