Tuesday, May 3, 2011

How to move controls from the bottom to the top of an MFC form?

In VC++ 6.0 (MFC), how the the controls eg : Button, EditBox, and Static Text can be move from bottom of the form to top of the form.

From stackoverflow
  • You can use CWnd::MoveWindow() to move controls. CWnd::GetDlgItem() will retrieve a CWnd for a given control id.

    Some pseudocode to be called from inside the class of the window that is parent of the controls:

    RECT windowRect;
    GetClientRect( &windowRect );// Bounds of the current window
    
    CWnd* controlWindow = GetDlgItem( controlId );
    RECT controlRect;
    controlWindow->GetWindowRect( &controlRect );//control rectangle
    ScreenToClient( &controlRect );//control rectangle in the coordinate system of the parent
    
    const int vertOffset = windowRect.top - controlRect.top;//how much to adjust
    controlRect.top += vertOffset;
    controlRect.bottom += vertOffset;
    controlWindow->MoveWindow( &controlRect );
    
    Jeff : the sample code looks good to me -

0 comments:

Post a Comment