我的技术博客已搬家至: http://www.kai-zhou.com , 其他博客已停止更新,欢迎访问查看文章的最新版本.
最近想在VC中单文档框架中删除菜单,状态栏,工具栏 。状态栏,工具栏在CMainFrame::OnCreate中就可以注释调,但是怎么样将菜单删掉呢?百度了一下,不太好找到。只好依靠现有知识,自己找到办法了。通过跟踪调试MFC的源代码居然让我找到了办法。
首先,我们先想办法达到在单文档界面中不显示菜单的效果。
通过现有知识,我们知道MFC是在CMainFrame::OnCreate中生成状态栏,工具栏的。代码如下:
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(
"
未能创建工具栏
"
);
return
-
1
;
//
未能创建
}
if
(
!
m_wndStatusBar
.
Create(this)
||
!
m_wndStatusBar
.
SetIndicators(indicators
,
sizeof
(indicators)
/
sizeof
(UINT))) {
TRACE0(
"
未能创建状态栏
"
);
return
-
1
;
//
未能创建
}
//
TODO: 如果不需要工具栏可停靠,则删除这三行
m_wndToolBar
.
EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(
&
m_wndToolBar);
return
0
; }
所以如果不想要状态栏,工具栏,直接在CMainFrame::OnCreate中将相关代码注释调就行了。那么,菜单相关的代码在哪呢?遍历一遍CMainFrame类,没找到。根据代码相关性,既然状态栏,工具栏是在 CMainFrame::OnCreate中生成的,那么菜单估计也是在这个函数里面生成的。
首先在 if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 处下断点,进入函数内部,代码如下:
int CFrameWnd
::
OnCreate(LPCREATESTRUCT lpcs) {
ENSURE_ARG(lpcs
!=
NULL
); CCreateContext
*
pContext
=
(CCreateContext
*
)lpcs
->
lpCreateParams;
return
OnCreateHelper(lpcs
,
pContext); }
继续进入OnCreateHelper函数,代码如下:
int CFrameWnd
::
OnCreateHelper(LPCREATESTRUCT lpcs
,
CCreateContext
*
pContext) {
if
(CWnd
::
OnCreate(lpcs)
==
-
1
)
return
-
1
;
//
create special children first
if
(
!
OnCreateClient(lpcs
,
pContext)) {
TRACE(traceAppMsg
,
0
,
"
Failed to create client pane/view for frame.
"
);
return
-
1
; }
//
post message for initial message string
PostMessage(WM_SETMESSAGESTRING
,
AFX_IDS_IDLEMESSAGE);
//
make sure the child windows have been properly sized
RecalcLayout();