不知道为何,写过的一个blog不见了,于是重新简单的整理下。本来有篇blog需要互相辅正的,不过没办法了。
创建时修改,即在CMainFrame的成员函数PreCreateWindow函数来实现。
1.创建时修改程序的外观:修改初始化的窗口大小等等属性。
在CMainFrame函数中,设置cs的各个属性,cs是一个结构体,设置cx,cy表示它的大小,设置其他的表示另外的大小。
注:原书由一个错误,可是,可以通过配套的源代码来看出。
就是书上是: cs.style |= ~FWS_ADDTOTITLE;
而代码上是: cs.style = WS_OVERLAPPEDWINDOW;
如果按照书上的代码,就会假死,每次启动都失去响应,而且标题没有名称。
如果是代码上的 cs.style = WS_OVERLAPPEDWINDOW;则不会出现这个结果。
修改的源代码如下:
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
cs.cx = 500;
cs.cy = 300;
// cs.style = WS_OVERLAPPEDWINDOW;
cs.style |= ~FWS_ADDTOTITLE;
cs.lpszName = "Helloworld";
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return TRUE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
注:可以通过修改CMainFrame的成员函数PreCreateWindow函数的CREATESTRUCT结构体来个性化窗口的外观。在单文档应用程序中,默认的窗口类型是WS_OVERLAPPEDWINDOW和FWS_ADDTOTILE,其中,FWS_ADDTOTITLE是一个MFC类型,将文档的标题添加到窗口标题。所以要设置窗口标题的内容,必须将FWS_ADDTOTITLE属性去掉。
但是,将属性设置改为&之后,就不会出现那个问题了。怪了…………
特别注意的是,如果不讲属性设置为~FWS_ADDTOTITLE,就不会更改标题的名称,即使设置了lpszName也不行
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
具体的属性设置见:
http://www.cppblog.com/deercoder/archive/2010/02/17/107962.html2.创建后设置的话,需要在CMainFrame和View中的OnCreate函数中,实现SetClassLong函数的使用。
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
SetClassLong(m_hWnd,GCL_HICON,(LONG)LoadIcon(NULL,IDI_ERROR)); //更改窗口图标,同前面的AfxRegisterWndClass函数
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int CTest14View::OnCreate(LPCREATESTRUCT lpCreateStruct)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(GRAY_BRUSH));
SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
详见《Visual C++编程范例开发金典》 P96--99
posted on 2010-02-17 21:46
deercoder 阅读(446)
评论(0) 编辑 收藏 引用 所属分类:
MFC程序设计入门