最近在CView上显示一个对话框,后来发现没什么用,但是也要记录下来。
首先,创建一个mfc单文档,view的基类选CView。插入一个对话框,取名CMainDlg,此对话框style属性选child,border选none。
头文件如下:
// CommonPlatformView.h : CCommonPlatformView 类的接口
//
#pragma once
#include "MainDlg.h"
class CCommonPlatformView : public CView
{
protected: // 仅从序列化创建
CCommonPlatformView();
DECLARE_DYNCREATE(CCommonPlatformView)
// 特性
public:
CCommonPlatformDoc* GetDocument() const;
// 操作
public:
// 重写
public:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
// 实现
public:
virtual ~CCommonPlatformView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
CMainDlg *m_mainDlg;
// 生成的消息映射函数
protected:
afx_msg void OnFilePrintPreview();
afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
virtual void OnInitialUpdate();
virtual void OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnSizing(UINT fwSide, LPRECT pRect);
};
#ifndef _DEBUG // CommonPlatformView.cpp 中的调试版本
inline CCommonPlatformDoc* CCommonPlatformView::GetDocument() const
{ return reinterpret_cast<CCommonPlatformDoc*>(m_pDocument); }
#endif
cpp文件如下:
// CommonPlatformView.cpp : CCommonPlatformView 类的实现
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "CommonPlatform.h"
#endif
#include "CommonPlatformDoc.h"
#include "CommonPlatformView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CCommonPlatformView
IMPLEMENT_DYNCREATE(CCommonPlatformView, CView)
BEGIN_MESSAGE_MAP(CCommonPlatformView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CCommonPlatformView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_SIZING()
END_MESSAGE_MAP()
// CCommonPlatformView 构造/析构
CCommonPlatformView::CCommonPlatformView()
{
// TODO: 在此处添加构造代码
}
CCommonPlatformView::~CCommonPlatformView()
{
if (m_mainDlg != NULL)
{
delete m_mainDlg;
m_mainDlg = NULL;
}
}
BOOL CCommonPlatformView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
// CCommonPlatformView 绘制
void CCommonPlatformView::OnDraw(CDC* /*pDC*/)
{
CCommonPlatformDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
}
// CCommonPlatformView 打印
void CCommonPlatformView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CCommonPlatformView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CCommonPlatformView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CCommonPlatformView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
void CCommonPlatformView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CCommonPlatformView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CCommonPlatformView 诊断
#ifdef _DEBUG
void CCommonPlatformView::AssertValid() const
{
CView::AssertValid();
}
void CCommonPlatformView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCommonPlatformDoc* CCommonPlatformView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCommonPlatformDoc)));
return (CCommonPlatformDoc*)m_pDocument;
}
#endif //_DEBUG
// CCommonPlatformView 消息处理程序
int CCommonPlatformView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
CRect rect;
GetClientRect(&rect);
int i = rect.Width();
int j = rect.Height();
m_mainDlg = new CMainDlg;
m_mainDlg->Create(IDD_DIALOG_MAIN,this);
m_mainDlg->MoveWindow(0,0,rect.right - rect.left,rect.bottom - rect.top);
m_mainDlg->ShowWindow(SW_SHOW);
return 0;
}
void CCommonPlatformView::OnInitialUpdate()
{
CView::OnInitialUpdate();
CRect rect;
GetClientRect(&rect);
m_mainDlg = new CMainDlg;
m_mainDlg->Create(IDD_DIALOG_MAIN,this);
m_mainDlg->MoveWindow(0,0,rect.right - rect.left,rect.bottom - rect.top);
m_mainDlg->ShowWindow(SW_SHOW);
// TODO: 在此添加专用代码和/或调用基类
}
void CCommonPlatformView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
// TODO: 在此添加专用代码和/或调用基类
}
void CCommonPlatformView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
CRect rect;
GetClientRect(&rect);
m_mainDlg->MoveWindow(0,0,rect.right - rect.left,rect.bottom - rect.top);
m_mainDlg->ShowWindow(SW_SHOW);
}
void CCommonPlatformView::OnSizing(UINT fwSide, LPRECT pRect)
{
CView::OnSizing(fwSide, pRect);
// TODO: 在此处添加消息处理程序代码
}