testmatcom_mfcview.cpp

来自「《精通matlab与c++混合编程》的光盘内容」· C++ 代码 · 共 268 行

CPP
268
字号
// testmatcom_mfcView.cpp : implementation of the CTestmatcom_mfcView class
//

#include "stdafx.h"
#include "testmatcom_mfc.h"

#include "testmatcom_mfcDoc.h"
#include "testmatcom_mfcView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_mfcView

IMPLEMENT_DYNCREATE(CTestmatcom_mfcView, CView)

BEGIN_MESSAGE_MAP(CTestmatcom_mfcView, CView)
	//{{AFX_MSG_MAP(CTestmatcom_mfcView)
	ON_WM_SIZE()
	ON_COMMAND(ID_DRAWNOISE, OnDrawnoise)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_mfcView construction/destruction

CTestmatcom_mfcView::CTestmatcom_mfcView()
{
	// TODO: add construction code here
	isInitMatcom=0;
	m_hFlag=0;	
}

CTestmatcom_mfcView::~CTestmatcom_mfcView()
{
	if(isInitMatcom)
	{
		exitM();
		isInitMatcom=0;
	}
}

BOOL CTestmatcom_mfcView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_mfcView drawing

void CTestmatcom_mfcView::OnDraw(CDC* pDC)
{
	CTestmatcom_mfcDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_mfcView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_mfcView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CTestmatcom_mfcView message handlers

void CTestmatcom_mfcView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class	
	if(!isInitMatcom) 
	{
		initM(MATCOM_VERSION);	

		isInitMatcom=1;
		m_h = winaxes(m_hWnd);
		this->m_hFlag=1;
		axesposition(10,10,100,100);		
		double bounddata[4]={0,2*3.1415926,-1,1};
		Mm mbound;
		M_VECTOR(mbound,bounddata);

		//axis
        title((CL(TM("SIN函数图形"))));
        xlabel((CL(TM("x"))));
        ylabel((CL(TM("y"))));		
        set(m_h,(CL(TM("Color")),TM("black")));
        set(m_h,(CL(TM("Box")),TM("on")));
		//axis(mbound);

		Mm x,y;
		x=linspace(0,2*pi,100);		
		y=msin(x);
		m_data = y;
		plot((CL(m_data),TM("y")));		

		m_h1 = winaxes(m_hWnd);		
		Mm pos;
		pos=(BR(240),240,200,200);
		set(m_h1,TM("RealPosition"),pos);		
		Mm color;
		color = zeros(1,3);
		color.r(1)=0;color.r(2)=0;color.r(3)=0;		
		set(m_h,TM("color"),color);		
		m_data1 = randn(1,50);
		plot((CL(m_data1),TM("b")));			
		double * phandle=NULL;
		phandle = m_h1.addr();
		int nrow,ncol;
		nrow = m_h1.rows();
		ncol = m_h1.cols();		

		title(CL(TM("当前的图形")));
	}	
}

void CTestmatcom_mfcView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here	
	//figure(CL(1));
	if(this->m_hFlag>0)
	{
		//axes(m_h);		
		if(IsMatcomHanleValid(m_h))
		{
			Mm pos;
			pos = zeros(1,4);
			pos.r(1)=0;pos.r(2)=0;
			pos.r(3)=cx;pos.r(4)=cy/2;
			set(m_h,TM("RealPosition"),pos);
		}
		if(IsMatcomHanleValid(m_h1))
		{
			Mm pos;
			pos = zeros(1,4);
			pos.r(1)=0;pos.r(2)=cy/2;
			pos.r(3)=cx;pos.r(4)=cy/2;
			set(m_h1,TM("RealPosition"),pos);
		}		
		//axesposition(0,0,cx,cy);
	}
}

void CTestmatcom_mfcView::OnDrawnoise() 
{
	// TODO: Add your command handler code here
	if(this->isInitMatcom)
	{
		Mm isHold;
		m_data = randn(1,200);		
		axes(CL(m_h));		
		hold(TM("off"));
		isHold = ishold();
		if((int)(isHold.r(1)))
		{
			
		}
		else
		{
			clf();
			m_h = winaxes(m_hWnd);
			m_h1 = winaxes(m_hWnd);
			CRect rect;
			GetClientRect(&rect);
			int cx=rect.Width();
			int cy=rect.Height();
			if(IsMatcomHanleValid(m_h))
			{
				Mm pos;
				pos = zeros(1,4);
				pos.r(1)=0;pos.r(2)=0;
				pos.r(3)=cx;pos.r(4)=cy/2;
				set(m_h,TM("RealPosition"),pos);
			}
			axes(CL(m_h));
			plot((CL(m_data),TM("y")));
			title((CL(TM("随机数图形"))));
			xlabel((CL(TM("x"))));
			ylabel((CL(TM("y"))));		
			set(m_h,(CL(TM("Color")),TM("black")));
			set(m_h,(CL(TM("Box")),TM("on")));

			if(IsMatcomHanleValid(m_h1))
			{
				Mm pos;
				pos = zeros(1,4);
				pos.r(1)=0;pos.r(2)=cy/2;
				pos.r(3)=cx;pos.r(4)=cy/2;
				set(m_h1,TM("RealPosition"),pos);
			}
			axes(CL(m_h1));
			plot(CL(m_data1));
			
			//Invalidate(true);
		}
	}
}

bool CTestmatcom_mfcView::IsMatcomHanleValid(Mm handle)
{
	if(this->isInitMatcom)
	{
		Mm len,isH;		
		isH = ishandle(handle);
		len = length(isH);
		if(((int)(len.r(1)))&&((int)(isH.r(1))))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

⌨️ 快捷键说明

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