📄 用vc++在单文档界面中创建多视图-开发者网络-vc-天极yesky1.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0039)http://www.yesky.com/97/1779597_1.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>用VC++在单文档界面中创建多视图-开发者网络-VC-天极Yesky</TITLE>
<META content=用VC++在单文档界面中创建多视图, name=description>
<META content=用VC++在单文档界面中创建多视图, name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all
href="用VC++在单文档界面中创建多视图-开发者网络-VC-天极Yesky1.files/y_mobile_content.css"
type=text/css rel=stylesheet>
<SCRIPT src="用VC++在单文档界面中创建多视图-开发者网络-VC-天极Yesky1.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3157" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A
href="http://product.yesky.com/">产品报价</A>| <A
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A>
| <A href="http://digital.yesky.com/">数码</A> | <A
href="http://notebook.yesky.com/">笔记本</A> | <A
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A>
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A>
| <A href="http://dh.yesky.com/">数字家庭</A> | <A
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> |
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A>
| <A href="http://pic.yesky.com/">图片</A> | <A
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> |
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A>
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户
src="用VC++在单文档界面中创建多视图-开发者网络-VC-天极Yesky1.files/logo.gif" border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select()
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="用VC++在单文档界面中创建多视图-开发者网络-VC-天极Yesky1.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="用VC++在单文档界面中创建多视图-开发者网络-VC-天极Yesky1.files/flash.js"
type=text/javascript></SCRIPT>
<!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极Yesky</A> > <A
href="http://soft.yesky.com/">软件频道</A> > <A
href="http://dev.yesky.com/">开发者网络</A> > 用VC++在单文档界面中创建多视图</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://www.yesky.com/97/1779597_1.shtml#pls"
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>用VC++在单文档界面中创建多视图</H1></DIV>
<DIV id=conauthor><SPAN>2004-03-23 10:12 </SPAN><SPAN>作者: 周伟 </SPAN><SPAN>出处:
CESDN </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/1779597.shtml" target=_blank>方舟</A>
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN
id=contentAdv></SPAN></DIV> 方法二:动态创建切换法<BR> <BR> 步骤描述:<BR><BR> 1.删除当前的视图<BR><BR> 首先需要获得当前视图的指针,不能使用GetActiveView()和GetActiveDocument()这两个函数,当前视图有可能处在未激活状态,<BR><BR> 所以应该使用EnumChildWindows这个Win32API函数,函数定义如下:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD> BOOL EnumChildWindows(<BR> HWND hWndParent, //
父窗口的句柄<BR> WNDENUMPROC lpEnumFunc, // 用户自定义回调函数<BR> LPARAM lParam //
传给回调函数的自定义参数<BR> );<BR> 回调函数的定义如下:<BR> BOOL CALLBACK
EnumChildProc(<BR> HWND hwnd, // 字窗口的句柄<BR> LPARAM lParam //
自定义参数<BR> );</TD></TR></TBODY></TABLE><BR> EnumChildWindows函数遍历父窗口的所有子窗口,递归调用用户定义的回调函数,当回调函数返回FALSE时,停止遍历,<BR><BR> 至于何时返回FALSE,这根据用户自己需要编写的回调函数来决定。<BR><BR> 删除视图使用DeleteWindow()这个函数,用delete也可以删除,但还要其他底层的操作,这里就不详细介绍了,因为删除视图使用DeleteWindow()最合适、方便了。在删除视图的时候还要注意不能将文档同时自动删除。<BR><BR> 删除视图的代码如下:<BR><BR>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD> { ......<BR> CWnd* pWnd;<BR> CWnd* pWndToDelete;<BR> //
使用EnumChildWindows查找从CView继承的子窗口<BR> ::EnumChildWindows(m_hWnd,
MyWndEnumProc, (LPARAM)&(pWnd));<BR> if(pWnd == NULL)<BR> {//
没有发现子窗口<BR> return FALSE;}<BR> //
发现子窗口,找到级别最高的子窗口,即父窗口为CMainFrame的窗口<BR> while(
lstrcmp(pWnd->GetRuntimeClass()->m_lpszClassName, ″CMainFrame″)
)<BR> {<BR> pWndToDelete = pWnd;<BR> pWnd =
pWnd->GetParent();<BR> }<BR> //
确保视图被删除时文档不被删除<BR> pDoc->m_bAutoDelete = FALSE;}
<P> // 删除视图<BR> pWndToDelete->DestroyWindow();<BR>
pDoc->m_bAutoDelete = TRUE;<BR> ......<BR>
}</P></TD></TR></TBODY></TABLE>
<P> 用户定义的回调函数:<BR></P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD> BOOL CALLBACK MyWndEnumProc(HWND hWnd, LPARAM ppWndLPARAM)<BR>
{<BR> CWnd* pWndChild = CWnd::FromHandlePermanent(hWnd);<BR> CWnd**
ppWndTemp = (CWnd**)ppWndLPARAM;<BR> if( pWndChild &&
pWndChild->IsKindOf(RUNTIME_CLASS(CView)) )<BR> {<BR> //
发现任何从CView继承的子窗口,将子窗口指针传递出去<BR> *ppWndTemp = pWndChild;<BR> // 停止继续搜索
<BR> return FALSE;<BR> }<BR> else<BR> {<BR> *ppWndTemp =
NULL;<BR> // 继续搜索<BR> return TRUE;<BR> }}<BR></TD></TR></TBODY></TABLE>
<P> 2.创建新的视图<BR></P>
<TABLE width="100%" bgColor=#ffffff>
<TBODY>
<TR>
<TD> CDocument* pCurrentDoc = ((CFrameWnd*)
m_pMainWnd)->GetActiveDocument();<BR> // 初始化创建上下文相关指针<BR>
CCreateContext newContext;<BR> newContext.m_pNewViewClass =
RUNTIME_CLASS(CView1);<BR> newContext.m_pCurrentDoc = pCurrentDoc;<BR>
newContext.m_pNewDocTemplate = NULL;<BR> newContext.m_pLastView =
NULL;<BR> newContext.m_pCurrentFrame = NULL;<BR> CView* pNewView =
STATIC_DOWNCAST(CView, CreateView(&newContext));<BR> if( pNewView ==
NULL )<BR> {<BR> return FALSE;<BR> }<BR> //
使用CreateView创建的视图不能自动调用OnInitialUpdate函数,需要人工调用OnInitialUpdate函数或者发送WM_INITIALUPDATE消息<BR>
pNewView->OnInitialUpdate(); <BR> //
使用CreateView创建的视图不会自动显示并且激活,需要人工操作<BR> pNewView->ShowWindow(SW_SHOW);
<BR> SetActiveView(pNewView);<BR> RecalcLayout();</TD></TR></TBODY></TABLE>
<P> 注:RUNTIME_CLASS宏含义<BR><BR> 每一个从CObject类继承的类,在定义DECLARE_DYNAMIC、DECLARE_DYNCREATE、DECLARE_SERIAL三个中任意一个宏时都会产生一个CRuntimeClass结构的静态对象,RUNTIME_CLASS返回的就是这个对象的指针,这个对象包含了其基类和本身在运行时刻的信息。以上是笔者在编写多个视应用在单文档的程序时整理出来的两种方法,因为VC++的强大和灵活,笔者相信还有更加巧妙的方法实现这个功能,希望这篇文章能够起到抛砖引玉的作用,也希望广大VC++编程爱好者对本文能够给予批评和指正。
</P>
<SCRIPT>zmbbs=1;</SCRIPT>
<DIV class=right>共2页。 <A class=page href="http://www.yesky.com/97/1779597.shtml"
target=_self><FONT face=Webdings>9</FONT></A> <A class=page
href="http://www.yesky.com/97/1779597.shtml" target=_self><FONT
face=Webdings>7</FONT></A> <A class=page
href="http://www.yesky.com/97/1779597.shtml" target=_self><B>1</B></A> <A
class=page href="http://www.yesky.com/97/1779597_1.shtml" target=_self><FONT
color=#666666><B>2</B></FONT></A></DIV></DIV><!--广告代码开始-->
<SCRIPT>
var ad_cid;
if (window.location.search.substring(1) != "")
{
ad_cid = window.location.search.substring(1);
} else {
ad_cid = 412;
}
</SCRIPT>
<!--小通栏:c-scolumn顶部500*90--><SPAN id=span_ad1>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-scolumn.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad1").innerHTML=document.getElementById("span_ad1").innerHTML;
document.getElementById("span_ad1").innerHTML="";
</SCRIPT>
<!--c-pip文章正文中间240*200--><SPAN id=span_ad3>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-pip.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT>
if(document.getElementById("contentAdv")) {
document.getElementById("contentAdv").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
} else {
document.getElementById("ad3").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
}</SCRIPT>
<!--c-text-1“您现在的位置”右侧文字--><SPAN id=span_ad2>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-text-1.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad2").innerHTML=document.getElementById("span_ad2").innerHTML;
document.getElementById("span_ad2").innerHTML="";
</SCRIPT>
<!--c-column-2“标题”上面的广告--><SPAN id=span_ad10>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-column-2.js\"><\/script>");</SCRIPT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -