Konrad
Konrad

Reputation: 41017

Sizing an MFC Window

I have an MFC app which I have been working on for a few weeks now, I want to manually set the dimensions of the main frame when it is loaded, can someone give me a hand with this, specifically where to put the code as well?

Thanks!

Upvotes: 11

Views: 30331

Answers (6)

Michael Haephrati
Michael Haephrati

Reputation: 4245

Thanks to Dynamic Layout, modern MFC applications don't have to do the resizing of its controls and reinvent the wheel. Just set the Moving Type and Sizing Type to fit your needs. enter image description here

Upvotes: 0

Zrn-dev
Zrn-dev

Reputation: 169

BOOL YourProjectApp::InitInstance()
{
        :
        :
    m_pMainWnd->MoveWindow(0, 0, 1900, 1000);  // add this line for fixing the default size of mainWindow
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

Upvotes: -1

Vũ Đức Vĩ
Vũ Đức Vĩ

Reputation: 29

you can use this:

CRect rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
left = -3, right = 3;
rect.top = 100;
rect.bottom = 500;
rect.left = 100;
rect.right = 800;
//or use
CRect cr;
cr.SetRect(POINT{ 100,100 }, POINT{ 500,800 });
MoveWindow(rect);

Upvotes: 0

Roel
Roel

Reputation: 19642

You can also set the size (with SetWindowPos()) from within CMainFrame::OnCreate(), or in the CWinApp-derived class' InitInstance. Look for the line that says pMainFrame->ShowWindow(), and call pMainFrame->SetWindowPos() before that line. That's where I always do it.

Upvotes: 12

Serge
Serge

Reputation: 7694

I think you're looking for PreCreateWindow and that your app isn't dialog based.

It's a virtual member function of CWnd class and it's called by framework just before a window is created. So it's a right place to place your changes.

You should write something like this:

BOOL CMyWindow::PreCreateWindow(CREATESTRUCT& cs)
{
   cs.cy = 640; // width
   cs.cx = 480; // height
   cs.y = 0; // top position
   cs.x = 0; // left position
   // don't forget to call base class version, suppose you derived you window from CWnd
   return CWnd::PreCreateWindow(cs);
}

Upvotes: 3

IanW
IanW

Reputation: 1314

Find your screen size with ..

CRect rect;
SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);
screen_x_size=rect.Width();  
screen_y_size=rect.Height();

use these values to calculate the X and Y size of your window then ..

::SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,main_x_size,main_y_size,SWP_NOZORDER); 

Where main_x_size and main_y_size are your sizes.

Upvotes: 3

Related Questions