Resizing Controls at Runtime

 


With a couple of lines of code, you can give your end-users the ability to resize any control.

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 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 shown in the image above). The first thing, you'll need 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 only two events that are needed to be handled:
  • 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.

  • LButtonDown
    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 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) ;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值