VC ++ MFC中的文档视图体系结构 [英] Document View Architecture in VC++ MFC
问题描述
大家好,我是文档视图架构这个概念的新手,所以请任何人帮我解决这个问题,请给出一些编程实例。谢谢你帮忙...
[更新]
任何人请帮帮我...
我想 Google 不会帮助你: mfc文档视图架构教程 [ ^ ]。
http://msdn.microsoft.com/en-us /library/4x1xy43a%28v=vs.110%29.aspx [ ^ ]
肌酸g你的第一个MFC Doc / View应用程序 [ ^ ]
http://www.ucancode.net/Visual_C_MFC_Samples/Multiple-document-views-VC-MFC-Example.htm [ ^ ]
< blockquote>当我开始一个新项目时,我总是在SDI或Dialog之间进行选择,具体取决于我需要的界面布局。不要被文档/视图所左右,我创建了数百个项目,但从未使用过文档功能。生成SDI项目后,我要做的第一件事就是删除生成的CDocument代码。可以将其想象为框架窗口/视图与对话框。
如果你的界面需要是一个复杂的框架窗口,你已经内置了控制栏,你仍然有CFormView类(我使用分配)。如果您的项目是一个简单的实用程序,您可能会选择基于对话框的项目。
应用程序向导为您生成一个骨架项目,但作为极简主义者,我尝试删除所有不必要的代码。我将发布一个关于我用SDI项目做事的方式的例子,我希望它有所帮助。
以下是创建主窗口的另一种方法示例:
// ============================================== ===
//全局变量
// ================================= ================
CMainFrame * g_pMainFrame = NULL;
CString g_cstrWinClass(YourWindowClassName);
BOOL CApp :: InitInstance()
{
//如果应用程序
// manifest指定使用,则Windows XP上需要InitCommonControls() ComCtl32.dll版本6或更高版本启用
//视觉样式。否则,任何窗口创建都将失败。
INITCOMMONCONTROLSEX iccx = {0};
iccx.dwSize = sizeof(iccx);
iccx.dwICC = ICC_STANDARD_CLASSES;
InitCommonControlsEx(& iccx);
CWinApp :: InitInstance();
//创建一个窗口类
WNDCLASS wc = {0};
wc.style = CS_DBLCLKS | CS_GLOBALCLASS;
wc.lpfnWndProc = :: DefWindowProc;
wc.hInstance = AfxGetInstanceHandle();
wc.hIcon = AfxGetApp() - > LoadIcon(IDR_MAINFRAME);
wc.hCursor = AfxGetApp() - > LoadStandardCursor(IDC_ARROW);
wc.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = g_cstrWinClass;
//注册窗口类
if(!:: GetClassInfo(wc.hInstance,wc.lpszClassName,& wc))
{
if(! :: RegisterClass(& wc))
{
TRACE(_T(窗口类无法注册\ n));
返回FALSE;
}
}
//创建主窗口
g_pMainFrame = new CMainFrame;
m_pMainWnd = g_pMainFrame;
//调用CreateWindow()
DWORD dwStyle = WS_OVERLAPPEDWINDOW;
if(!g_pMainFrame-> Create(
g_cstrWinClass,g_cstrWinName,
dwStyle,CRect(0,0,1024,768),NULL,
MAKEINTRESOURCE(IDR_MAINFRAME),NULL ))
返回FALSE;
g_pMainFrame-> CenterWindow();
g_pMainFrame-> ShowWindow(SW_SHOW);
g_pMainFrame-> UpdateWindow();
返回TRUE;
}
这是在框架窗口初始化函数内创建视图的另一种方法:
int CMainFrame :: OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CFrameWnd :: OnCreate(lpCreateStruct)== -1)
return -1 ;
...
//创建主视图
CCreateContext context;
memset(& context,0,sizeof(CCreateContext));
context.m_pNewViewClass = RUNTIME_CLASS(CMainView);
VERIFY(g_pMainView = STATIC_DOWNCAST(CMainView,
CreateView(& context,AFX_IDW_PANE_FIRST)));
g_pMainView-> OnInitialUpdate();
SetActiveView(g_pMainView);
返回0;
}
以下是关闭活动视图的方法:
void CCSMainFrame :: SwitchView(CView * pNewView)
{
ASSERT(pNewView);
CView * pOldView = GetActiveView();
if(pOldView == pNewView)
return;
if(pOldView)
{
//隐藏旧视图
pOldView-> ShowWindow(SW_HIDE);
:: SetWindowLong(pOldView-> m_hWnd,GWL_ID,AFX_IDW_PANE_FIRST + 1);
}
if(pNewView)
{
//显示新视图
pNewView-> BringWindowToTop();
pNewView-> ShowWindow(SW_SHOWNORMAL);
pNewView-> UpdateWindow();
:: SetWindowLong(pNewView-> m_hWnd,GWL_ID,AFX_IDW_PANE_FIRST);
SetActiveView(pNewView);
RecalcLayout();
}
}
Hi All, I am new to this concept of Document View Architecture so please can any one help me to solve this problem and please give some programming example.Thanks for help...
[Update]
Any one please help me ...
I suppose Google won't help you: "mfc document view architecture tutorial"[^].
http://msdn.microsoft.com/en-us/library/4x1xy43a%28v=vs.110%29.aspx[^]
Creating your first MFC Doc/View application[^]
http://www.ucancode.net/Visual_C_MFC_Samples/Multiple-document-views-VC-MFC-Example.htm[^]
When I am starting a new project I always choose between SDI or Dialog based depending on the interface layout I will need. Do not get sidetracked by "Document/View", I have created hundreds of projects but never once used the "Document" capabilities. One of the first things I do after generating a SDI project is remove the generated CDocument code. Think of it more as "Frame Window / View" vs "Dialog" based.
If your interface needs to be a complex, with frame windows you have built in control bars and you still have the CFormView class (which I use allot). If your project is a simple utility you would probably choose the Dialog based project.
The AppWizard generates a skeleton project for you but as a minimalist I try and remove all unnecessary code. I will post an example of the way I do things with SDI projects, I hope it helps a little.
Here is an example of an alternative way to create your main window:
//================================================= // Global variables //================================================= CMainFrame* g_pMainFrame = NULL; CString g_cstrWinClass("YourWindowClassName"); BOOL CApp::InitInstance() { // InitCommonControls() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. INITCOMMONCONTROLSEX iccx = {0}; iccx.dwSize = sizeof(iccx); iccx.dwICC = ICC_STANDARD_CLASSES; InitCommonControlsEx(&iccx); CWinApp::InitInstance(); // Create a window class WNDCLASS wc = {0}; wc.style = CS_DBLCLKS|CS_GLOBALCLASS; wc.lpfnWndProc = ::DefWindowProc; wc.hInstance = AfxGetInstanceHandle(); wc.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); wc.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszClassName = g_cstrWinClass; // Register the window class if(!::GetClassInfo(wc.hInstance, wc.lpszClassName, &wc)) { if(!::RegisterClass(&wc)) { TRACE(_T("Window class could not be registered\n")); return FALSE; } } // Create main window g_pMainFrame = new CMainFrame; m_pMainWnd = g_pMainFrame; // Call CreateWindow() DWORD dwStyle = WS_OVERLAPPEDWINDOW; if(!g_pMainFrame->Create( g_cstrWinClass, g_cstrWinName, dwStyle, CRect(0,0,1024,768), NULL, MAKEINTRESOURCE(IDR_MAINFRAME), NULL)) return FALSE; g_pMainFrame->CenterWindow(); g_pMainFrame->ShowWindow(SW_SHOW); g_pMainFrame->UpdateWindow(); return TRUE; }
Here is an alternative way to create your view(s) inside your frame window init function:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if(CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; ... // Create the main view CCreateContext context; memset(&context, 0, sizeof(CCreateContext)); context.m_pNewViewClass = RUNTIME_CLASS(CMainView); VERIFY(g_pMainView = STATIC_DOWNCAST(CMainView, CreateView(&context, AFX_IDW_PANE_FIRST))); g_pMainView->OnInitialUpdate(); SetActiveView(g_pMainView); return 0; }
Here is how to switch out your active view:
void CCSMainFrame::SwitchView(CView* pNewView) { ASSERT(pNewView); CView* pOldView = GetActiveView(); if(pOldView == pNewView) return; if(pOldView) { // Hide the old view pOldView->ShowWindow(SW_HIDE); ::SetWindowLong(pOldView->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST+1); } if(pNewView) { // Show the new view pNewView->BringWindowToTop(); pNewView->ShowWindow(SW_SHOWNORMAL); pNewView->UpdateWindow(); ::SetWindowLong(pNewView->m_hWnd, GWL_ID, AFX_IDW_PANE_FIRST); SetActiveView(pNewView); RecalcLayout(); } }
这篇关于VC ++ MFC中的文档视图体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!