C++ 调整 MFC 窗口的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/178326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sizing an MFC Window
提问by Konrad
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?
我有一个 MFC 应用程序,我已经开发了几个星期,我想在加载时手动设置主框架的尺寸,有人可以帮我解决这个问题,特别是代码放在哪里?
Thanks!
谢谢!
回答by Roel
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.
您还可以SetWindowPos()
从 内部CMainFrame::OnCreate()
或 -CWinApp
派生类' 中设置大小(使用)InitInstance
。查找显示pMainFrame->ShowWindow()
,并pMainFrame->SetWindowPos()
在该行之前调用的行。这就是我总是这样做的地方。
回答by IanW
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 ..
使用这些值来计算窗口的 X 和 Y 大小,然后..
::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.
凡main_x_size
和main_y_size
是你的尺寸。
回答by Serge
I think you're looking for PreCreateWindowand that your app isn't dialog based.
我认为您正在寻找PreCreateWindow并且您的应用程序不是基于对话框的。
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.
它是 CWnd 类的虚拟成员函数,在创建窗口之前由框架调用。因此,这是放置更改的正确位置。
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);
}