MFC::文档视图

1 文档

1.01 主窗口显示之前修改子窗口的样式

/* 修改窗口名称 */
SetWindowText(_T("Main Frame"));

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	//删除FWS_ADDTOTITLE风格
	cs.style &= ~FWS_ADDTOTITLE;
	return TRUE;
}

1.02 设置主窗口最大最小化

//最大化
m_nCmdShow = SW_SHOWMAXIMIZED;
//最小化
m_nCmdShow = SW_SHOWMINIMIZED;

m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();

1.03 设置主/子窗口最大最小化位置

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	//设置窗口最大化时的大小
	lpMMI->ptMaxSize.x = 400;
	lpMMI->ptMaxSize.y = 300;
	//设置窗口最大化时的位置
	lpMMI->ptMaxPosition.x = 0;
	lpMMI->ptMaxPosition.y = 0;
	CFrameWnd::OnGetMinMaxInfo(lpMMI);
}

1.04 设置子窗口最大最小化

virtual void ActivateFrame(int nCmdShow = -1);

void CChildFrame::ActivateFrame(int nCmdShow)
{
	//最大化
	nCmdShow = SW_SHOWMAXIMIZED;
	//最小化
	nCmdShow = SW_SHOWMINIMIZED
	CMDIChildWnd::ActivateFrame(nCmdShow);
}

1.05 关闭时保存窗口状态/启动时回复窗口状态

void CMainFrame::ActivateFrame(int nCmdShow)
{
	// TODO:  在此添加专用代码和/或调用基类
	if (m_bFirst)
	{
		m_bFirst = FALSE;

		WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
		pWndpl->length = sizeof(WINDOWPLACEMENT);

		CWinApp* pApp = AfxGetApp();

		//恢复窗口位置
		pWndpl->flags = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("FLAGS"), 0);
		pWndpl->showCmd = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("SHOWCMD"), 0);
		nCmdShow = pWndpl->showCmd;
		pWndpl->ptMinPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("MINX"), 0);
		pWndpl->ptMinPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("MINY"), 0);
		pWndpl->ptMaxPosition.x = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("MAXX"), 0);
		pWndpl->ptMaxPosition.y = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("MAXY"), 0);
		pWndpl->rcNormalPosition.top = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("TOP"), 0);
		pWndpl->rcNormalPosition.left = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("LEFT"), 0);
		pWndpl->rcNormalPosition.right = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("RIGHT"), 0);
		pWndpl->rcNormalPosition.bottom = pApp->GetProfileInt(_T("WINDOWPLACEMENT"),
			_T("BOTTOM"), 0);

		//设置窗口位置
		SetWindowPlacement(pWndpl);

		delete pWndpl;
	}
	CFrameWnd::ActivateFrame(nCmdShow);
}
void CMainFrame::OnClose()
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	WINDOWPLACEMENT* pWndpl = new WINDOWPLACEMENT;
	pWndpl->length = sizeof(WINDOWPLACEMENT);

	//获得窗口位置
	GetWindowPlacement(pWndpl);

	CWinApp* pApp = AfxGetApp();

	//保存窗口位置
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("FLAGS"),
		pWndpl->flags);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("SHOWCMD"),
		pWndpl->showCmd);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINX"),
		pWndpl->ptMinPosition.x);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MINY"),
		pWndpl->ptMinPosition.y);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXX"),
		pWndpl->ptMaxPosition.x);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("MAXY"),
		pWndpl->ptMaxPosition.y);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("TOP"),
		pWndpl->rcNormalPosition.left);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("LEFT"),
		pWndpl->rcNormalPosition.top);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("RIGHT"),
		pWndpl->rcNormalPosition.right);
	pApp->WriteProfileInt(_T("WINDOWPLACEMENT"), _T("BOTTOM"),
		pWndpl->rcNormalPosition.bottom);

	delete pWndpl;
	CFrameWnd::OnClose();
}

1.06 删除SDI框架窗口中的菜单

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	if (cs.hMenu != NULL)
	{
 		//删除菜单
		DestroyMenu(cs.hMenu);
		//设置菜单句柄值为NULL
		cs.hMenu = NULL;
	}
	return TRUE;
}

1.07 改变视图的背景颜色

BOOL CCFrameApp2View::OnEraseBkgnd(CDC* pDC)
{
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	//获得客户区大小
	CRect rect;
	GetClientRect(rect);

	//创建画刷
	CBrush m_Brush;
	m_Brush.CreateSolidBrush(RGB(0x99, 0xCC, 0xFF));

	//填充背景
	pDC->FillRect(&rect, &m_Brush);
	return TRUE;
}

1.08 MDI启动时不生成新的子窗口

// 分析标准 shell 命令、DDE、打开文件操作的命令行
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

1.09 排列MDI子框架窗口

void CMainFrame::OnTilehWnd()
{
	//水平显示
	MDITile(MDITILE_HORIZONTAL);
}
void CMainFrame::OnTilevWnd()
{
	//垂直显示
	MDITile(MDITILE_VERTICAL);
}
void CMainFrame::OnCascadeWnd()
{
	//层叠显示
	MDICascade();
}

1.10 在SDI中切换多视图

void CMainFrame::SwitchView(int nView)
{
	//获得旧视图(活动视图)指针
	CView* pOldView = GetActiveView();
	//获得新视图指针
	CView* pNewView = (CView*)GetDlgItem(nView);
	if (pNewView == NULL)
	{
		if (nView == 1)
		{
			pNewView = new CDemoView1();
		}
		else if (nView == 2)
		{
			pNewView = new CDemoView2();
		}
		else
		{
			return;
		}

		CCreateContext context;
		context.m_pCurrentDoc = pOldView->GetDocument();

		//创建新视图
		pNewView->Create(NULL, NULL, WS_BORDER, CFrameWnd::rectDefault, 
			this, nView, &context);
		
		pNewView->OnInitialUpdate();
	}

	//设置新视图为活动视图
	SetActiveView(pNewView);

	//显示新视图
	pNewView->ShowWindow(SW_NORMAL);
	//隐藏旧视图
	pOldView->ShowWindow(SW_HIDE);

	//设置旧视图ID
	if (pOldView->GetRuntimeClass() == RUNTIME_CLASS(CDemoView1))
	{
		pOldView->SetDlgCtrlID(1);
	}
	else if (pOldView->GetRuntimeClass() == RUNTIME_CLASS(CDemoView2))
	{
		pOldView->SetDlgCtrlID(2);
	}
	//设置新视图ID
	pNewView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);

	//重新布局
	RecalcLayout();
}

void CMainFrame::OnSwitchView1() 
{
	SwitchView(1);
}

1.10 SDI中动态切分窗口

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	//创建动态窗口
	return m_wndSplitter.Create(this, 2, 2, CSize(10, 10), pContext);
}
// CCFrameAppView 消息处理程序
void CCFrameAppView::OnSplitView()
{
	CSplitterWnd* pSplitter = GetParentSplitter(this, FALSE);
	if (pSplitter == NULL)
	{
		return;
	}

	ASSERT(!pSplitter->IsTracking());
	pSplitter->DoKeyboardSplit();
}

1.11 SDI静态态切分窗口

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	//创建静态窗口
	m_wndSplitter.CreateStatic(this, 2, 1);

	//创建视图
	m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CDemoView1), 
		CSize(100, 150), pContext);
	m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CDemoView2), 
		CSize(100, 150), pContext);
		
	return TRUE;
}

1.12 列表视图初始化

/* 初始化工程的时候,选择继承CListView */
void CDemoView::InitList()
{
	//获得列表控件的指针
	CListCtrl* pList = &GetListCtrl();

	//设置列表视图的报表显示方式
	pList->ModifyStyle(LVS_ICON | LVS_SMALLICON | LVS_LIST, LVS_REPORT);

	//设置列表视图显示网格线和整行选中
	pList->SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);

	//在列表视图中插入列
	for (int n = 0; n < 4; n++)
	{
		CString strColumnHeading = _T("");
		strColumnHeading.Format(_T("Column %d"), n);
		pList->InsertColumn(n, strColumnHeading, LVCFMT_LEFT, 100);
	}

	//在列表视图中插入行
	for (int m = 0; m < 10; m++)
	{
		CString strItem = _T("");
		strItem.Format(L"Item %d", m);
		pList->InsertItem(m, strItem);

		for (int n = 1; n < 4; n++)
		{
			CString strText = _T("");
			strText.Format(_T("SubItem %d %d"), m, n);
			pList->SetItemText(m, n, strText);
		}
	}
}

1.13 树视图初始化

void CDemoView::InitTree()
{
	//获得数控件
	CTreeCtrl* pTree = &GetTreeCtrl();
	//为树添加连线和按钮
	pTree->ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
	CString strText = _T("");

	//在树中插入项
	HTREEITEM hRoot = pTree->InsertItem(_T("Root"));
	for (int i = 0; i < 4; i++)
	{
		strText.Format(_T("Item %d"), i);
		//在树中插入项
		HTREEITEM hParent = pTree->InsertItem(strText, hRoot);
		for(int j = 0; j < 5; j++)
		{
			strText.Format(_T("SubItem %d %d"), i, j);
			//在树中插入项
			pTree->InsertItem(strText, hParent);
		}
		//展开树项
		pTree->Expand(hParent, TVE_EXPAND);	
	}
	//展开树项
	pTree->Expand(hRoot, TVE_EXPAND);
	//选择树项
	pTree->Select(hRoot, TVGN_CARET);
}

1.14 HTML视图初始化

void CCHtmlView::OnInitialUpdate()
{
	CHtmlView::OnInitialUpdate();
	Navigate2(_T("http://www.baidu.com/"), NULL, NULL);
}

1.15 获取文档指针

void CDemoView::GetDocPoint()
{
	//获得文档指针
	CDemoDoc* pDoc = GetDocument();
	if (pDoc == NULL)
	{
		return;
	}

	CRect rect;
	GetClientRect(rect);
	CString strText = _T("");
	strText.Format(_T("在视图中获得文档指针:0x%08X"), pDoc);

	//文档中输出文字信息
	CDC* pDC = GetDC();
	pDC->SetTextAlign(TA_CENTER);
	pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText);
}

1.16 获取当前视图指针

void CDemoView::OnDraw(CDC* pDC)
{
	CRect rect;
	GetClientRect(rect);
	CString strText = _T("");
	strText.Format(_T("视图指针:0x%08X"), this);
	pDC->SetTextAlign(TA_CENTER);
	pDC->TextOut(rect.Width() / 2, rect.Height()  / 3, strText);
}

1.17 获取当前文档指针

void CMainFrame::OnTest() 
{
	//获得当前子框架
	CMDIChildWnd* pChildFrame = (CMDIChildWnd*)GetActiveFrame();
	if (pChildFrame == NULL)
	{
		return;
	}

	//获得当前视图
	CDemoView* pView = (CDemoView*)pChildFrame->GetActiveView();
	if (pView == NULL)
	{
		return;
	}

	//获得当前文档
	CDemoDoc* pDoc = (CDemoDoc*)pChildFrame->GetActiveDocument();
	if (pDoc == NULL)
	{
		return;
	}

	CRect rect;
	pView->GetClientRect(rect);
	CString strText = _T("");
	strText.Format(_T("在框架中获得当前文档指针:0x%08X"), pDoc);
	CDC* pDC = pView->GetDC();
	pDC->SetTextAlign(TA_CENTER);
	pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText);
}

1.18 创建新的框架子窗口

void CMainFrame::OnTest() 
{
	CDemoApp* pApp = (CDemoApp*)AfxGetApp();
	if (pApp == NULL)
	{
		return;
	}
	CMultiDocTemplate* pDocTemplate = pApp->m_pDocTemplate;
	CDocument* pDoc = pApp->m_pDoc;
	
	//创建新框架窗口
	CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(pDoc, NULL);
	if (pFrame == NULL)
	{
		return;
	}
	//初始化框架窗口
	pDocTemplate->InitialUpdateFrame(pFrame, pDoc);
}

1.19 单文档-多视图结构

//创建文档模板1
	CMultiDocTemplate* pDocTemplate1;
	pDocTemplate1 = new CMultiDocTemplate(
		IDR_DEMOTYPE,
		RUNTIME_CLASS(CDemoDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CDemoView1));
	AddDocTemplate(pDocTemplate1);

//创建文档模板2
	CMultiDocTemplate* pDocTemplate2;
	pDocTemplate2 = new CMultiDocTemplate(
		IDR_DEMOTYPE,
		RUNTIME_CLASS(CDemoDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CDemoView2));
	AddDocTemplate(pDocTemplate2);

	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

//文档模板1新建一个文档
	CDemoDoc* pDoc = (CDemoDoc*)pDocTemplate1->OpenDocumentFile(NULL);
	if (pDoc == NULL)
	{
		return FALSE;
	}

//文档模板2创建与文档相关联的框架窗口
	CFrameWnd* pFrame = pDocTemplate2->CreateNewFrame(pDoc, pMainFrame);
	if (pFrame == NULL)
	{
		return FALSE;
	}
	//文档模板2初始化框架窗口
	pDocTemplate2->InitialUpdateFrame(pFrame, pDoc);

	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

//水平平铺窗口
	pMainFrame->MDITile(MDITILE_HORIZONTAL);

1.20 在文档模板中遍历文档

void CDemoView1::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	CRect rect;
	GetClientRect(rect);
	CString strText = _T("");
	strText.Format(_T("文档%d"), lHint);
	CDC* pDC = GetDC();
	pDC->SetTextAlign(TA_CENTER);
	pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText);
}
void CDemoView2::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	CRect rect;
	GetClientRect(rect);
	CString strText = _T("");
	strText.Format(_T("文档%d"), lHint);
	CDC* pDC = GetDC();
	pDC->SetTextAlign(TA_CENTER);
	pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText);
}

1.21 文档中遍历视图

void CDemoDoc::OnTest()
{
	int n = 0;

	//获得第一个视图的位置
	POSITION pos = GetFirstViewPosition();
	while(pos != NULL) 
	{
		n++;
		//获得下一个视图
		CView* pView = GetNextView(pos);
		if (pView == NULL)
		{
			continue;
		}
		CRect rect;
		pView->GetClientRect(rect);
		CString strText = _T("");
		strText.Format(_T("视图%d"), n);
		CDC* pDC = pView->GetDC();
		pDC->SetTextAlign(TA_CENTER);
		pDC->TextOut(rect.Width() / 2, rect.Height() / 2, strText);
	}
}
posted @ 2018-08-15 16:57  osbreak  阅读(994)  评论(0编辑  收藏  举报