mfc向导生成的 默认窗口大小和位置是如何设置的

    技术2022-05-19  17

    mfc向导生成的 默认窗口大小和位置是如何设置的 2010-11-25 18:06

    窗口最终是由Windows API 函数::CreateWindowEx创建,::CreateWindowEx需要窗口的信息,其中就有位置和大小,这些信息从哪里来?下面就分析一下:下面是CFrameWnd中Create函数的声明BOOL Create(LPCTSTR lpszClassName,    LPCTSTR lpszWindowName,    DWORD dwStyle = WS_OVERLAPPEDWINDOW,   const RECT& rect = rectDefault,    CWnd* pParentWnd = NULL,        // != NULL for popups    LPCTSTR lpszMenuName = NULL,    DWORD dwExStyle = 0,    CCreateContext* pContext = NULL);从onst RECT& rect = rectDefault,这就是窗口位置和大小参数 它有一个默认值,是Windows定义好的。我以创建的当文档应用程序Single为例说明 CFrame的Create函数的调用中间无关的函数都删掉了CSingleApp::InitInstance() ->CWinApp::ProcessShellCommand ->CDocTemplate::CreateNewFrame ->CFrameWnd::LoadFrame ->CFrameWnd::Create ->CWnd::CreateEx ->::CreateWindowEx这一串函数在调用CFrameWnd::Create是得到了窗口的位置和大小

     

     

    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs){if( !CFrameWnd::PreCreateWindow(cs) )   return FALSE;// TODO: Modify the Window class or styles here by modifying// the CREATESTRUCT cs

    cs.cx=500;cs.cy=500;//修改默认矩形窗口rect的尺寸大小

    cs.x=0;cs.y=0;//修改默认窗口的位置

     

     

    ‍ return TRUE;}

     

     

     

     

     

    还有很多的办法改变窗口运行的大小:?(需要更新)

    方法一: 在单文档‍CDonghuaApp::InitInstance()中修改

    BOOL CDonghuaApp::InitInstance(){AfxEnableControlContainer();

    。。。。。。

    // The one and only window has been initialized, so show and update it.HWND hWnd=GetDesktopWindow();//取得桌面窗口的句柄GetWindowRect(hWnd,&rect);//设置矩形对象参数m_pMainWnd->MoveWindow(rect.left,rect.top,rect.Width()/2,rect.Height()/2,TRUE);//设置框架窗口的显示位置和大小m_pMainWnd->ShowWindow(SW_SHOW);m_pMainWnd->UpdateWindow();

    return TRUE;}

    方法二:在 ‍CMainFrame::OnCreate()中修改

     

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CFrameWnd::OnCreate(lpCreateStruct) == -1)   return -1;// TODO: Add your specialized creation code hereCWnd *pWnd=GetDesktopWindow();//返回一个桌面窗口指针pWnd->GetWindowRect(&rect);//窗口指针指向矩形对象

       this->MoveWindow(rect.left+100,rect.top+100,rect.Width()/2,rect.Height()/2,TRUE);// this->MoveWindow(rect.left,rect.top,rect.Width()/2,rect.Height()/2,TRUE);// this->MoveWindow(0,0,600,600,TRUE);//设置窗口的位置和大小return 0;}

    ‍以一个sdi的工程举一个例子:   int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)   {   if (CFrameWnd::OnCreate(lpCreateStruct) == -1)   return -1;   if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP   | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||   !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))   {   TRACE0("Failed to create toolbar/n");   return -1; // fail to create   }   if (!m_wndStatusBar.Create(this) ||   !m_wndStatusBar.SetIndicators(indicators,   sizeof(indicators)/sizeof(UINT)))   {   TRACE0("Failed to create status bar/n");   return -1; // fail to create   }   m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);   EnableDocking(CBRS_ALIGN_ANY);   DockControlBar(&m_wndToolBar);   ::ShowWindow(this->m_hWnd,SW_SHOWMAXIMIZED);//代码在这里   return 0;   }   对于MDI的窗口只要分别在主窗口的OnCreate函数和子窗口的PreCreateWindow函数中添加代码就可以

     

    或者在App类的InitInstance函数中改变显示主框架的m_nCmdShow参数如下:

    m_nCmdShow = SW_SHOWMAXIMIZED;pMainFrame->ShowWindow(m_nCmdShow);


    最新回复(0)