分享知识

与大家一起分享知识

C++博客 首页 新随笔 联系 聚合 管理
  19 Posts :: 3 Stories :: 45 Comments :: 0 Trackbacks

在利用 VC MFC 编程的时候,我们往往能碰到这种情况:在一个模拟程序中(如有些游戏),需要实时显示一些属性的值(文字或者图形),这个时候我们可以重新打开一个对话框(可以是一个模态或者非模态的)来显示这些信息,本文以模态对话框为例,简单实现这个技术。以下代码阴影部分是我利用 ClassWizard 加的,蓝色部分是我手工加的。其余是用 MFC 向导生成的:
源代码下载地址:http://www.cppblog.com/Files/sscchh-2000/NotifyDialog.rar 

// NotifyDialogView.h : interface of the CNotifyDialogView class

//

/////////////////////////////////////////////////////////////////////////////

 

#if !defined(AFX_NOTIFYDIALOGVIEW_H__8242946E_9215_46D8_8EAC_DFC7452C8076__INCLUDED_)

#define AFX_NOTIFYDIALOGVIEW_H__8242946E_9215_46D8_8EAC_DFC7452C8076__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

 

#include "CountDlg.h"

 

class CNotifyDialogView : public CScrollView

{

protected: // create from serialization only

       CNotifyDialogView();

       DECLARE_DYNCREATE(CNotifyDialogView)

 

// Attributes

public:

       CNotifyDialogDoc* GetDocument();

 

       CCountDlg *m_pCountDlg;

// Operations

public:

 

// Overrides

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(CNotifyDialogView)

       public:

       virtual void OnDraw(CDC* pDC);  // overridden to draw this view

       virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

       protected:

       virtual void OnInitialUpdate(); // called first time after construct

       virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);

       virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);

       virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

       //}}AFX_VIRTUAL

 

// Implementation

public:

       virtual ~CNotifyDialogView();

#ifdef _DEBUG

       virtual void AssertValid() const;

       virtual void Dump(CDumpContext& dc) const;

#endif

 

protected:

 

// Generated message map functions

protected:

       //{{AFX_MSG(CNotifyDialogView)

       afx_msg void OnTimer(UINT nIDEvent);

       afx_msg void OnCountDialog();

       afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

       afx_msg void OnDestroy();

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

};

 

#ifndef _DEBUG  // debug version in NotifyDialogView.cpp

inline CNotifyDialogDoc* CNotifyDialogView::GetDocument()

   { return (CNotifyDialogDoc*)m_pDocument; }

#endif

 

/////////////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_NOTIFYDIALOGVIEW_H__8242946E_9215_46D8_8EAC_DFC7452C8076__INCLUDED_)

 

// NotifyDialogView.cpp : implementation of the CNotifyDialogView class

//

 

#include "stdafx.h"

#include "NotifyDialog.h"

 

#include "NotifyDialogDoc.h"

#include "NotifyDialogView.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

// CNotifyDialogView

 

IMPLEMENT_DYNCREATE(CNotifyDialogView, CScrollView)

 

BEGIN_MESSAGE_MAP(CNotifyDialogView, CScrollView)

       //{{AFX_MSG_MAP(CNotifyDialogView)

       ON_WM_TIMER()

       ON_COMMAND(ID_COUNT_DIALOG, OnCountDialog)

       ON_WM_CREATE()

       ON_WM_DESTROY()

       //}}AFX_MSG_MAP

       // Standard printing commands

       ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)

       ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)

       ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

// CNotifyDialogView construction/destruction

 

CNotifyDialogView::CNotifyDialogView()

{

       // TODO: add construction code here

       m_pCountDlg = NULL;

}

 

CNotifyDialogView::~CNotifyDialogView()

{

}

 

BOOL CNotifyDialogView::PreCreateWindow(CREATESTRUCT& cs)

{

       // TODO: Modify the Window class or styles here by modifying

       //  the CREATESTRUCT cs

 

       return CScrollView::PreCreateWindow(cs);

}

 

/////////////////////////////////////////////////////////////////////////////

// CNotifyDialogView drawing

 

void CNotifyDialogView::OnDraw(CDC* pDC)

{

       CNotifyDialogDoc* pDoc = GetDocument();

       ASSERT_VALID(pDoc);

       // TODO: add draw code for native data here

}

 

void CNotifyDialogView::OnInitialUpdate()

{

       CScrollView::OnInitialUpdate();

 

       CSize sizeTotal;

       // TODO: calculate the total size of this view

       sizeTotal.cx = sizeTotal.cy = 100;

       SetScrollSizes(MM_TEXT, sizeTotal);

}

 

/////////////////////////////////////////////////////////////////////////////

// CNotifyDialogView printing

 

BOOL CNotifyDialogView::OnPreparePrinting(CPrintInfo* pInfo)

{

       // default preparation

       return DoPreparePrinting(pInfo);

}

 

void CNotifyDialogView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

       // TODO: add extra initialization before printing

}

 

void CNotifyDialogView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

{

       // TODO: add cleanup after printing

}

 

/////////////////////////////////////////////////////////////////////////////

// CNotifyDialogView diagnostics

 

#ifdef _DEBUG

void CNotifyDialogView::AssertValid() const

{

       CScrollView::AssertValid();

}

 

void CNotifyDialogView::Dump(CDumpContext& dc) const

{

       CScrollView::Dump(dc);

}

 

CNotifyDialogDoc* CNotifyDialogView::GetDocument() // non-debug version is inline

{

       ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNotifyDialogDoc)));

       return (CNotifyDialogDoc*)m_pDocument;

}

#endif //_DEBUG

 

/////////////////////////////////////////////////////////////////////////////

// CNotifyDialogView message handlers

 

void CNotifyDialogView::OnTimer(UINT nIDEvent)

{

       // TODO: Add your message handler code here and/or call default

       if(m_pCountDlg)

       {

              m_pCountDlg->m_dwCount++;

              m_pCountDlg->ShowValue();

       }

 

       CScrollView::OnTimer(nIDEvent);

}

 

void CNotifyDialogView::OnCountDialog()

{

       // TODO: Add your command handler code here

       CCountDlg countDlg(this);

       countDlg.DoModal();

}

 

int CNotifyDialogView::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

       if (CScrollView::OnCreate(lpCreateStruct) == -1)

              return -1;

      

       // TODO: Add your specialized creation code here

       SetTimer(1,100,NULL);

 

       return 0;

}

 

void CNotifyDialogView::OnDestroy()

{

       CScrollView::OnDestroy();

      

       // TODO: Add your message handler code here

       KillTimer(1);

}

 

#if !defined(AFX_COUNTDLG_H__C5A72F95_781F_47DC_95D1_A0E9A8473756__INCLUDED_)

#define AFX_COUNTDLG_H__C5A72F95_781F_47DC_95D1_A0E9A8473756__INCLUDED_

 

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// CountDlg.h : header file

//

#include "stdafx.h"

 

/////////////////////////////////////////////////////////////////////////////

// CCountDlg dialog

 

class CCountDlg : public CDialog

{

// Construction

public:

       CCountDlg(CWnd* pParent = NULL);   // standard constructor

 

// Dialog Data

       //{{AFX_DATA(CCountDlg)

       enum { IDD = IDD_DIALOG_COUNT };

       DWORDm_dwCount;

       //}}AFX_DATA

 

public:

       CView *m_pView;

 

// Overrides

       // ClassWizard generated virtual function overrides

       //{{AFX_VIRTUAL(CCountDlg)

       protected:

       virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

       //}}AFX_VIRTUAL

public:    

       void ShowValue();

      

 

// Implementation

protected:

       void ReleaseViewPointer();

       // Generated message map functions

       //{{AFX_MSG(CCountDlg)

       afx_msg void OnDestroy();

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

};

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_COUNTDLG_H__C5A72F95_781F_47DC_95D1_A0E9A8473756__INCLUDED_)

 

// CountDlg.cpp : implementation file

//

 

#include "stdafx.h"

#include "NotifyDialog.h"

#include "CountDlg.h"

 

#include "MainFrm.h"

#include "ChildFrm.h"

#include "NotifyDialogDoc.h"

#include "NotifyDialogView.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

/////////////////////////////////////////////////////////////////////////////

// CCountDlg dialog

 

 

CCountDlg::CCountDlg(CWnd* pParent /*=NULL*/)

       : CDialog(CCountDlg::IDD, pParent)

{

       //{{AFX_DATA_INIT(CCountDlg)

       m_dwCount = 0;

       //}}AFX_DATA_INIT

       m_pView = (CNotifyDialogView*)pParent;

       ((CNotifyDialogView*)m_pView)->m_pCountDlg = this;

}

 

 

void CCountDlg::DoDataExchange(CDataExchange* pDX)

{

       CDialog::DoDataExchange(pDX);

       //{{AFX_DATA_MAP(CCountDlg)

       DDX_Text(pDX, IDC_EDIT_COUNT, m_dwCount);

       //}}AFX_DATA_MAP

}

 

 

BEGIN_MESSAGE_MAP(CCountDlg, CDialog)

       //{{AFX_MSG_MAP(CCountDlg)

       ON_WM_DESTROY()

       //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

/////////////////////////////////////////////////////////////////////////////

// CCountDlg message handlers

 

void CCountDlg::ShowValue()

{

       UpdateData(FALSE);

}

 

void CCountDlg::ReleaseViewPointer()

{

       ((CNotifyDialogView*)m_pView)->m_pCountDlg = NULL;

}

void CCountDlg::OnDestroy()

{

       CDialog::OnDestroy();

      

       // TODO: Add your message handler code here

       ReleaseViewPointer();

}

posted on 2006-04-28 21:47 史传红 阅读(3816) 评论(2)  编辑 收藏 引用 所属分类: C/C++细节知识

Feedback

# re: VC中视图(View)通知对话框(Dialog)的方法 2006-04-28 23:11 flyingxu
其实可以用简洁的语言代替所有的代码
而且,如果真的想在一个对话框中显示实时的值,现在的程序结构上不好。  回复  更多评论
  

# re: VC中视图(View)通知对话框(Dialog)的方法 2006-04-29 20:15 史传红
@flyingxu

由于我以前遇到这个问题没有解决,现在看了深入浅出MFC这本书,总结出来的,你能给出一个好的结构吗?我的意思是说把文档的数据在一个对话框中实时表现出来,谢谢。
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理