2
votes

I've tried MoveWindow(50,50,150,200,TRUE) in CMyFormView::OnInitialUpdate();

Also, I have tried following code in

CWinApp::InitInstance();
RECT desktop;

const HWND hDesktop = ::GetDesktopWindow();
::GetWindowRect(hDesktop,&desktop);

MoveWindow(hDesktop,0,0,900,400,TRUE);  

I am having no luck resizing the form(dialog).
I would appreciate any suggestions.

3

3 Answers

3
votes

In an SDI program the dialog is sized by the mainframe window to fill the client area. Resize the frame window and the dialog will follow. Put this in the formview's OnInitialUpdate

AfxGetMainWnd()->MoveWindow(....);
1
votes

KEY THINGS:

  1. MINIMUM SIZE - YOU SET - The dialog template - or form view - you make IS the absolute minimum size CFormview will use

  2. MAXIMUM SIZE - AUTOMATICALLY SIZED - The dialog template has no maximum. CFormview dynamically stretches it out to fit the CMainFrame window.

  3. STARTING THE APPLICATION TO THE TEMPLATE SIZE Naturally you might want the CMainFrame window to be as compact as possible, this is achieved using 3 mandatory lines of code in your CFormView::OnInitialUpdate()

void CSimpleSDIView::OnInitialUpdate() {

CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit(); // FORCES CMainframe to be as small as the dialog template

}

  1. FORCE CMainFrame to LIMIT MINIMUM SIZE - If necessary. Using WM_GETMINMAXINFO Here's the code:

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI) {

lpMMI->ptMinTrackSize.x = 500; // absolute minimum width for CMainFrame
lpMMI->ptMinTrackSize.y = 500; // absolute minimum height for CMainFrame

CFrameWnd::OnGetMinMaxInfo(lpMMI);

}

  1. SET TO IDEAL SIZE This can achieved in CFormView ::OnInitialUpdate by using the following code (Step 3 is still required):

    AfxGetMainWnd()->MoveWindow(0, 0, 1024, 600, 1);
    
  2. TWEAK CONTROL POSITIONS With the advent of the ribbon in MFC things are looking good, but using steps 1 - 5 doesn't work without extra tweaking. Basically, the ribbon sends the main window an extra resize as it would seem, so even if you sent a minimum template size, things get a little out of proportion due to the extra resizing -- mainly because a group within a ribbon can be collapsed into a single icon. ** BEFORE DOING STEP 5** you might want to add the following code:

if(::GetSystemMetrics(SM_CXSCREEN) > 1024) {

    RECT r;
    CWnd *someControl = GetDlgItem(IDC_SOMECONTROL);
    someControl->GetWindowRect(&r);
    ScreenToClient(&r);

        r.right += 300;//r.right += 30;
            r.bottom += 150;
            someControl->MoveWindow(&r)
        }

EXPLANATION:

The short answer, you don't! Basically, I had the same issue, made a dialog box template for use in a CFormView derived class. When I wanted to stretch the CMainFrame window out like any normal window, I wanted the content of the formview to resize accordingly. That worked fine with some code, but the problem came when I made the window very small. When I did that, I would get these ugly scrollbars. My thought was the scrollbars were there because they are trying to respect the original dialog template size. That thought is correct! For example, you make your dialog box 500 x 500 and you resize small less than that, you will get scrollbars - since CFormview respects that the application MUST BE AT A MINIMUM 500x500. The minimum size is governed by the template you make and you can not dynamically make it smaller.

0
votes

Usually, child window's size, position is changed in child's init code. If you change this in parent window code, you always have to check whether the child window handle value is valid. I input this code WM_CREATE handler of CAboutDlg.

int CAboutDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  Add your specialized creation code here

    RECT desktop;

    const HWND hDesktop = ::GetDesktopWindow();
    ::GetWindowRect(hDesktop,&desktop);

    MoveWindow(&desktop,TRUE);

    return 0;
}