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

📄 singletonview.cpp

📁 设计模式之singleton模式例程
💻 CPP
字号:
// SingletonView.cpp : implementation of the CSingletonView class
//

// This file is part of Singleton application to demonstrate the concept of Singleton classes.
// This file is generated by AppWizard and is provided "as is" with no expressed or 
// implied warranty.

#include "stdafx.h"
#include "Singleton.h"

#include "SingletonDoc.h"
#include "SingletonView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSingletonView

IMPLEMENT_DYNCREATE(CSingletonView, CView)

BEGIN_MESSAGE_MAP(CSingletonView, CView)
	//{{AFX_MSG_MAP(CSingletonView)
	ON_COMMAND(ID_MSGHNDLR_MESSAGE1, OnMsghndlrMessage1)
	ON_COMMAND(ID_MSGHNDLR_MESSAGE2, OnMsghndlrMessage2)
	ON_COMMAND(ID_MSGHNDLR_MESSAGE3, OnMsghndlrMessage3)
	ON_COMMAND(ID_MSGHNDLR_COUNT, OnMsghndlrCount)
	ON_COMMAND(ID_MSGHNDLR_TYPE, OnMsghndlrType)
	//}}AFX_MSG_MAP
	ON_MESSAGE( WM_HANDLE_MESSAGE, OnHandleMessage )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSingletonView construction/destruction

CSingletonView::CSingletonView()
{
	// Step 1 - Initialize message handler pointer
	m_pMsgHndlr	= NULL;
	m_csMessage	= _T("");
}

CSingletonView::~CSingletonView()
{
}

BOOL CSingletonView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSingletonView drawing

void CSingletonView::OnDraw( CDC * pDC )
{
	CSingletonDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// Step 1 - Display the message at some location
	pDC->TextOut( 100, 100, m_csMessage );
}

/////////////////////////////////////////////////////////////////////////////
// CSingletonView diagnostics

#ifdef _DEBUG
void CSingletonView::AssertValid() const
{
	CView::AssertValid();
}

void CSingletonView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CSingletonDoc* CSingletonView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSingletonDoc)));
	return (CSingletonDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSingletonView message handlers

void CSingletonView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// Step 1 - Get Message handler pointer from the application
	m_pMsgHndlr = ( ( CSingletonApp * ) AfxGetApp() )->m_pMsgHndlr;
	// Step 2 - Set Message handler window pointer
	m_pMsgHndlr->SetNotifyWindow( this );
}

LRESULT CSingletonView::OnHandleMessage( WPARAM, LPARAM lParam )
{
	LPMSGPACKET lpsbMsgPacket = ( LPMSGPACKET ) lParam;
	// Step 1 - Store the message in the member variable
	m_csMessage = lpsbMsgPacket->m_csMessage;
	// Step 2 - Invalidate the window
	Invalidate();
	return SUCCESS;
}

void CSingletonView::OnMsghndlrMessage1() 
{
	// Step 1 - Display test message 1
	m_pMsgHndlr->HandleMessage( "Test message 1 -> Welcome to Test Message Handler", INFO_TYPE );
}

void CSingletonView::OnMsghndlrMessage2() 
{
	// Step 1 - Display test message 2
	m_pMsgHndlr->HandleMessage( "Test message 2 -> Testing Message Handler", WARNING_TYPE );
}

void CSingletonView::OnMsghndlrMessage3() 
{
	// Step 1 - Display test message 3
	m_pMsgHndlr->HandleMessage( "Test message 3 -> Stop Testing Message Handler", ERROR_TYPE );
}

void CSingletonView::OnMsghndlrCount() 
{
	CHAR szTempString[ 512 ];

	// Step 1 - Get registered message handler count and display it
	memset( szTempString, 0, sizeof( szTempString ) );
	sprintf( szTempString, "Count of registered message handlers is - %d", CMsgHndlr::GetRegCount() );
	AfxMessageBox( szTempString, MB_OK | MB_ICONINFORMATION );
}

void CSingletonView::OnMsghndlrType() 
{
	// Step 1 - Get Message handler type from the application
	AfxMessageBox( CString( "Message handler type - " ) + 
				( ( CSingletonApp * ) AfxGetApp() )->m_csHndlrType, 
				MB_OK | MB_ICONINFORMATION );
}

⌨️ 快捷键说明

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