⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_61883.htm

📁 vc
💻 HTM
字号:
<p>
序号:61883 发表者:最后一根稻草 发表日期:2003-11-22 20:00:10
<br>主题:如何切换视口
<br>内容:我有一ODBC程序,它的视CTestDBView 是由CRecordView继承来的,运行时系统默认运行CTestDBView的界面,然后我又做了一个由CRecordView继承来的类CDlgView,问我该如何做才能通过一个菜单命令使界面切换到CDlgView类的界面上来呢?
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:dstwwx 回复日期:2003-11-22 21:35:58
<br>内容:下面是VC内幕5中的内容<BR>//手上没有代码,只有书,英文的,辛苦点看了。<BR><BR>The EX20C Example—Switching View Classes Without a Splitter<BR>Sometimes you just want to switch view classes under program control and you don't want to be bothered with a splitter window. The EX20C example is an SDI application that switches between CStringView and CHexView in response to selections on the View menu. Starting with what AppWizard generates, all you need to do is add two new menu commands and then add some code to the CMainFrame class. You also need to change the CStringView and CHexView constructors from protected to public. <BR><BR>Resource Requirements<BR>The following two items have been added to the View menu in the IDR_MAINFRAME menu resource. Caption Command ID CMainFrame Function <BR>St&amp;ring View ID_VIEW_STRINGVIEW OnViewStringView <BR>&amp;Hex View ID_VIEW_HEXVIEW OnViewHexView <BR><BR><BR>ClassWizard was used to add the command-handling functions (and corresponding update command UI handlers) to the CMainFrame class. <BR><BR>CMainFrame<BR>The CMainFrame class gets a new private helper function, SwitchToView, which is called from the two menu command handlers. The enum parameter tells the function which view to switch to. Here are the two added items in the MainFrm.h header file: <BR><BR><BR>private:<BR>&nbsp;&nbsp;&nbsp;&nbsp;enum eView { STRING = 1, HEX = 2 };<BR>&nbsp;&nbsp;&nbsp;&nbsp;void SwitchToView(eView nView);<BR>The SwitchToView function (in MainFrm.cpp) makes some low-level MFC calls to locate the requested view and to activate it. Don't worry about how it works. Just adapt it to your own applications when you want the view- switching feature. Add the following code: <BR><BR><BR>void CMainFrame::SwitchToView(eView nView)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CView* pOldActiveView = GetActiveView();<BR>&nbsp;&nbsp;&nbsp;&nbsp;CView* pNewActiveView = (CView*) GetDlgItem(nView);<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (pNewActiveView == NULL) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (nView) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case STRING:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNewActiveView = (CView*) new CStringView;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case HEX:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNewActiveView = (CView*) new CHexView;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CCreateContext context;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.m_pCurrentDoc = pOldActiveView-&gt;GetDocument();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNewActiveView-&gt;Create(NULL, NULL, WS_BORDER,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFrameWnd::rectDefault, this, nView, &amp;context);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pNewActiveView-&gt;OnInitialUpdate();<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;SetActiveView(pNewActiveView);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pNewActiveView-&gt;ShowWindow(SW_SHOW);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pOldActiveView-&gt;ShowWindow(SW_HIDE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pOldActiveView-&gt;SetDlgCtrlID(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pOldActiveView-&gt;GetRuntimeClass() == <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RUNTIME_CLASS(CStringView) ? STRING : HEX);<BR>&nbsp;&nbsp;&nbsp;&nbsp;pNewActiveView-&gt;SetDlgCtrlID(AFX_IDW_PANE_FIRST);<BR>&nbsp;&nbsp;&nbsp;&nbsp;RecalcLayout();<BR>}<BR>Finally, here are the menu command handlers and update command UI handlers that ClassWizard initially generated (along with message map entries and prototypes). The update command UI handlers test the current view's class. <BR><BR><BR>void CMainFrame::OnViewStringView()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;SwitchToView(STRING);<BR>}<BR><BR>void CMainFrame::OnUpdateViewStringView(CCmdUI* pCmdUI)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;pCmdUI-&gt;Enable(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!GetActiveView()-&gt;IsKindOf(RUNTIME_CLASS(CStringView)));<BR>}<BR><BR>void CMainFrame::OnViewHexView()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;SwitchToView(HEX);<BR>}<BR><BR>void CMainFrame::OnUpdateViewHexView(CCmdUI* pCmdUI)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;pCmdUI-&gt;Enable(<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;!GetActiveView()-&gt;IsKindOf(RUNTIME_CLASS(CHexView)));<BR>}<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:最后一根稻草 回复日期:2003-12-21 16:35:43
<br>内容:看了,虽然很艰难,倒底还是能用了,谢谢了。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -