ex090202view.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 235 行

CPP
235
字号
// Ex090202View.cpp : implementation of the CEx090202View class
//

#include "stdafx.h"
#include "Ex090202.h"

#include "Ex090202Doc.h"
#include "Ex090202View.h"
#include "ShowObjectView.h"
#include "ShowObjectFrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEx090202View

IMPLEMENT_DYNCREATE(CEx090202View, CFormView)

BEGIN_MESSAGE_MAP(CEx090202View, CFormView)
	//{{AFX_MSG_MAP(CEx090202View)
	ON_BN_CLICKED(IDC_START_LEFTTHREAD, OnStartLeftthread)
	ON_BN_CLICKED(IDC_END_LEFTTHREAD, OnEndLeftthread)
	ON_BN_CLICKED(IDC_ADDOBJECT_LEFTTHREAD, OnAddobjectLeftthread)
	ON_BN_CLICKED(IDC_ADDOBJECT_RIGHTTHREAD, OnAddobjectRightthread)
	ON_BN_CLICKED(IDC_DELOBJECT_LEFTTHREAD, OnDelobjectLeftthread)
	ON_BN_CLICKED(IDC_DELOBJECT_RIGHTTHREAD, OnDelobjectRightthread)
	ON_BN_CLICKED(IDC_END_RIGHTTHREAD, OnEndRightthread)
	ON_BN_CLICKED(IDC_START_RIGHTTHREAD, OnStartRightthread)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx090202View construction/destruction

CEx090202View::CEx090202View()
	: CFormView(CEx090202View::IDD)
{
	//{{AFX_DATA_INIT(CEx090202View)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_pLeftThread = NULL ;
	m_pRightThread = NULL;
}

CEx090202View::~CEx090202View()
{
	delete m_pLeftThread ;
	delete m_pRightThread ;
}

void CEx090202View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEx090202View)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

UINT TimeThread(LPVOID lpPara)
{
	CEx090202View * pView = (CEx090202View *)lpPara ;
	while(true)
	{
		Sleep(300);
		if(NULL != pView->m_pLeftThread )
		{
			CFrameWnd * pFrm = (CFrameWnd*)pView->m_pLeftThread->GetMainWnd() ;
			if( NULL != pFrm && ::IsWindow(pFrm->GetSafeHwnd()))
			{
				CShowObjectView * pView = (CShowObjectView *)(pFrm->GetActiveView());
				if(NULL != pView && NULL != pView->m_hWnd)
					pView->PostMessage(WM_SETP_OBJECT,NULL,NULL);
			}
		}
		if(NULL != pView->m_pRightThread )
		{
			CFrameWnd * pFrm = (CFrameWnd*)pView->m_pRightThread->GetMainWnd() ;
			if( NULL != pFrm && ::IsWindow(pFrm->GetSafeHwnd()))
			{
				CShowObjectView * pView = (CShowObjectView *)(pFrm->GetActiveView());
				if(NULL != pView && NULL != pView->m_hWnd)
					pView->PostMessage(WM_SETP_OBJECT,NULL,NULL);
			}
		}
	}
	return 1 ;
}

void CEx090202View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	AfxBeginThread(TimeThread,(LPVOID)this);
}

/////////////////////////////////////////////////////////////////////////////
// CEx090202View printing

BOOL CEx090202View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CEx090202View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEx090202View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CEx090202View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CEx090202View diagnostics

#ifdef _DEBUG
void CEx090202View::AssertValid() const
{
	CFormView::AssertValid();
}

void CEx090202View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CEx090202View message handlers

void CEx090202View::OnStartLeftthread() 
{
	CRect r ;
	GetClientRect(&r);
	r.right /= 3 ;
	ClientToScreen(&r);
	
	m_pLeftThread = new CShowObjectThread ;
	m_pLeftThread->m_hParent = AfxGetMainWnd()->GetSafeHwnd();
	m_pLeftThread->m_rRect = r ;
	m_pLeftThread->CreateThread();	
}

void CEx090202View::OnEndLeftthread() 
{
	if(NULL != m_pLeftThread)
	{	
		m_pLeftThread->GetMainWnd()->PostMessage(WM_CLOSE,NULL,NULL) ;
		m_pLeftThread = NULL ;
	}
}

void CEx090202View::OnAddobjectLeftthread() 
{
	CView * pView = ((CShowObjectFrm*)m_pLeftThread->GetMainWnd())->GetActiveView();
	if(NULL != pView)
		::PostMessage(pView->GetSafeHwnd(),WM_ADD_OBJECT,NULL,NULL) ;
}

void CEx090202View::OnDelobjectLeftthread() 
{
	CView * pView = ((CShowObjectFrm*)m_pLeftThread->GetMainWnd())->GetActiveView();
	if(NULL != pView)
		::PostMessage(pView->GetSafeHwnd(),WM_DEL_OBJECT,NULL,NULL) ;	
}


void CEx090202View::OnStartRightthread() 
{
	CRect r ;
	GetClientRect(&r);
	r.left = r.right / 3 * 2;
	ClientToScreen(&r);
	
	m_pRightThread = new CShowObjectThread ;
	m_pRightThread->m_hParent = AfxGetMainWnd()->GetSafeHwnd();
	m_pRightThread->m_rRect = r ;
	m_pRightThread->CreateThread();			
}

void CEx090202View::OnEndRightthread() 
{
	if(NULL != m_pRightThread )
	{	
		m_pRightThread->GetMainWnd()->PostMessage(WM_CLOSE,NULL,NULL) ;
		m_pRightThread = NULL ;
	}
}

void CEx090202View::OnAddobjectRightthread() 
{
	CView * pView = ((CShowObjectFrm*)m_pRightThread->GetMainWnd())->GetActiveView();
	if(NULL != pView)
		::PostMessage(pView->GetSafeHwnd(),WM_ADD_OBJECT,NULL,NULL) ;	
}

void CEx090202View::OnDelobjectRightthread() 
{
	CView * pView = ((CShowObjectFrm*)m_pRightThread->GetMainWnd())->GetActiveView();
	if(NULL != pView)
		::PostMessage(pView->GetSafeHwnd(),WM_DEL_OBJECT,NULL,NULL) ;	
}

⌨️ 快捷键说明

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