SetWindowPos v CREATESTRUCT

Ladies and gentleman

This is really just for education on the nuances of MFC.

I was playing around with creating windows of different sizes and positions. Although I can achieve this with SetWindowPos, I have tried a number of times with CREATESTRUCT but it has only worked once for me.

The following work in only one of my projects:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
  if( !CFrameWnd::PreCreateWindow(cs) )  return FALSE;

  cs.style=WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;    // no min/max
  cs.x=10;
  cs.y=10;
  cs.cx=100;
  cs.cy=100;
  cs.hMenu=NULL;

  return TRUE;
}


In all other projects, I can change the style but not the position and size. The only thing I can imagine is that ia dependent on the initial MFC project set-up and what underlying Application is picked. I have experiment but still can't find one that allows the above code to work again. Am I missing something really obvious.

As I said I can affect the changes through SetWindowPos but I would like to understand why CREATESTRUCT doesn't always work. I would also be gratefully if someone could explain when you would consider changing the code in:

BOOL CGridClickView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}


Ben
The MSDN example alters the struct first and then calls on tot he base class, i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// alter the styles of the main frame window
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
   // Create a window without min/max buttons or sizable border
   cs.style = WS_OVERLAPPED | WS_SYSMENU | WS_BORDER;

   // Size the window to 1/3 screen size and center it
   cs.cy = ::GetSystemMetrics(SM_CYSCREEN) / 3;
   cs.cx = ::GetSystemMetrics(SM_CXSCREEN) / 3;
   cs.y = ((cs.cy * 3) - cs.cy) / 2;
   cs.x = ((cs.cx * 3) - cs.cx) / 2;

   return CFrameWnd::PreCreateWindow(cs);
}


Have you tried it this way around??
Last edited on
No this makes no difference.

I have now tried on numerous different Application set-ups and the only set-up that seems to allow sizing and position through CREATESTRUCT is when User Interface Features - Use Classic Menus is selected. My original code then works. This makes me think that selecting menu bars, toolbars or ribbons forces some later automatic sizing to occur after the PreCreateWindow.
Topic archived. No new replies allowed.