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

📄 matlabenginetestview.cpp

📁 《精通matlab与c++混合编程》的光盘内容
💻 CPP
字号:
// matlabenginetestView.cpp : implementation of the CMatlabenginetestView class
//

#include "stdafx.h"
#include "matlabenginetest.h"

#include "matlabenginetestDoc.h"
#include "matlabenginetestView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMatlabenginetestView

IMPLEMENT_DYNCREATE(CMatlabenginetestView, CFormView)

BEGIN_MESSAGE_MAP(CMatlabenginetestView, CFormView)
	//{{AFX_MSG_MAP(CMatlabenginetestView)
	ON_BN_CLICKED(IDC_CloseEngine, OnCloseEngine)
	ON_BN_CLICKED(IDC_DrawSinc, OnDrawSinc)
	ON_BN_CLICKED(IDC_StartEngine, OnStartEngine)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_HIDEENGINECHECK, OnHideenginecheck)
	ON_BN_CLICKED(IDC_EVALUATESTRING, OnEvaluatestring)
	ON_WM_SIZE()	
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMatlabenginetestView construction/destruction

CMatlabenginetestView::CMatlabenginetestView()
	: CFormView(CMatlabenginetestView::IDD)
{
	//{{AFX_DATA_INIT(CMatlabenginetestView)
	m_nIsCheck = FALSE;
	m_cmdedit = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_ep = NULL;
	memset(m_outbuff,0,_MAX_BUFF_CHAR_NUM*sizeof(char));
}

CMatlabenginetestView::~CMatlabenginetestView()
{
	if(m_ep!=NULL)
	{
		engClose(m_ep);
	}
}

void CMatlabenginetestView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMatlabenginetestView)
	DDX_Control(pDX, IDC_OUTPUTEDIT, m_outputedit);
	DDX_Check(pDX, IDC_HIDEENGINECHECK, m_nIsCheck);
	DDX_Text(pDX, IDC_CMDEDIT, m_cmdedit);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CMatlabenginetestView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	m_outputedit.SetWindowText("");
}

/////////////////////////////////////////////////////////////////////////////
// CMatlabenginetestView printing

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

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMatlabenginetestView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMatlabenginetestView message handlers

void CMatlabenginetestView::OnCloseEngine() 
{
	// TODO: Add your control notification handler code here
	engClose(m_ep);
	m_ep=NULL;
}

void CMatlabenginetestView::OnDrawSinc() 
{
	// TODO: Add your control notification handler code here
	if(m_ep!=NULL)
	{
		engEvalString(m_ep,"x1=0.01:0.01:10*pi");
		engEvalString(m_ep,"x2=-10*pi:0.01:-0.01");
		engEvalString(m_ep,"y1=sin(x1)./x1");
		engEvalString(m_ep,"y2=sin(x2)./x2");
		engEvalString(m_ep,"plot([x2 x1],[y2 y1])");
		//产生一个错误,从而验证MATLAB引擎的输出已经被捕获
		engEvalString(m_ep,"1/0");
		m_outputedit.SetWindowText(m_outbuff);
	}
	else
	{
		AfxMessageBox("请启动MATLAB引擎",MB_OK,NULL);
	}
}

void CMatlabenginetestView::OnStartEngine() 
{
	// TODO: Add your control notification handler code here
	m_ep = engOpen(NULL);
	engOutputBuffer(m_ep,m_outbuff,_MAX_BUFF_CHAR_NUM);
}

void CMatlabenginetestView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here			
	// Do not call CFormView::OnPaint() for painting messages	
}

void CMatlabenginetestView::OnHideenginecheck() 
{
	// TODO: Add your control notification handler code here
	if(m_ep==NULL) return;
	UpdateData(true);
	engSetVisible(m_ep,!m_nIsCheck);	
}

void CMatlabenginetestView::OnEvaluatestring() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	LPSTR pstr = m_cmdedit.GetBuffer(m_cmdedit.GetLength());
	if(m_ep!=NULL)
	{
		engEvalString(m_ep,(const char*)pstr);
		m_outputedit.SetWindowText(m_outbuff);
	}
	else
	{
		AfxMessageBox("请启动MATLAB引擎",MB_OK,NULL);
	}
}

void CMatlabenginetestView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(::IsWindow(m_outputedit.GetSafeHwnd()))
	{		
		m_outputedit.MoveWindow(0,140,cx,cy-140,true);
	}
}

⌨️ 快捷键说明

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