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

📄 mixfiguredlg.cpp

📁 自己写的一个vc与MATLAB混合编程的例子
💻 CPP
字号:
// MixFigureDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MixFigure.h"
#include "MixFigureDlg.h"
#include "matlab.hpp"
#include "string.h"
#include "mix_inv.hpp"
#include "mix_eigvalue.hpp"
#include "mix_eigvector.hpp"
#include "mix_plot.hpp"

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


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMixFigureDlg dialog

CMixFigureDlg::CMixFigureDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMixFigureDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMixFigureDlg)
	m_A = 1.0f;
	m_edit1 = _T("");
	m_edit2 = _T("");
	m_B = 1.0f;
	m_C = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMixFigureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMixFigureDlg)
	DDX_Text(pDX, IDC_EDIT_A, m_A);
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	DDX_Text(pDX, IDC_EDIT_B, m_B);
	DDX_Text(pDX, IDC_EDIT_C, m_C);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMixFigureDlg, CDialog)
	//{{AFX_MSG_MAP(CMixFigureDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_A, OnDeltaposSpinA)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_DRAWLINE, OnDrawline)
	ON_BN_CLICKED(IDC_EIGVECTOR, OnEigvector)
	ON_BN_CLICKED(IDC_EIGVAL, OnEigval)
	ON_BN_CLICKED(IDC_INV, OnInv)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_B, OnDeltaposSpinB)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_C, OnDeltaposSpinC)
	ON_EN_CHANGE(IDC_EDIT_A, OnChangeEditA)
	ON_BN_CLICKED(IDC_PLOTAREA, OnPlotarea)
	ON_EN_CHANGE(IDC_EDIT_B, OnChangeEditB)
	ON_EN_CHANGE(IDC_EDIT_C, OnChangeEditC)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMixFigureDlg message handlers

BOOL CMixFigureDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	h_a = empty();
	libmmfileInitialize(); 
	libmwsglmInitialize(); 
	mlfHGInitialize(NULL,NULL);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMixFigureDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMixFigureDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMixFigureDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMixFigureDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	// CDialog::OnOK();
}

void CMixFigureDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	// CDialog::OnCancel();
}

void CMixFigureDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default

	// 在退出程序时关闭打开的Figure窗口
	// close('all');
	Vclose(mwArray("all"));

	CDialog::OnCancel();
}

void CMixFigureDlg::OnDeltaposSpinA(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	m_A += -pNMUpDown->iDelta;
	UpdateData(FALSE);
	// 绘制曲线
	DrawLine();

	*pResult = 0;
}



void CMixFigureDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	mlfHGTerminate(); 
	libmwsglmTerminate(); 
	libmmfileTerminate(); 
}

void CMixFigureDlg::OnDrawline() 
{
	// TODO: Add your control notification handler code here
	int bWait = 0;
	// 若还没有创建坐标系则创建坐标系
	if(tobool(isempty(h_a)))
	{
		char FigName[] = "_Figure_";
		double BKColor[] = {1,1,1};
		char strVisible[] = "Off";

		mwArray mwFigName(FigName);
		mwArray mwBKColor(1,3,BKColor);
		mwArray mwVisible(strVisible);
		
		h_a = GenAxis(mwFigName,mwBKColor,mwVisible);

		// 查找Figure窗口
		HWND hFig = ::FindWindow(NULL,FigName);
		if(hFig == NULL)
		{
			AfxMessageBox("未能产生Figure窗口,图形绘制失败!");
			return;
		}

		// 去掉Figure窗口的标题栏和边框
		long lStyle = ::GetWindowLong(hFig,GWL_STYLE);
		::SetWindowLong(hFig,GWL_STYLE,lStyle & (~WS_CAPTION) & (~WS_THICKFRAME));
		::SetWindowPos(hFig,NULL,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);

		// 获取绘图区域的位置和大小
		RECT PlotRec;
		CWnd *PlotArea = GetDlgItem(IDC_PLOTAREA);
		PlotArea->GetWindowRect(&PlotRec);
		long Width = PlotRec.right - PlotRec.left;
		long Height = PlotRec.bottom - PlotRec.top;

		// 设置Figure窗口为绘图区域窗口的子窗口并调整其位置
		::SetParent(hFig,PlotArea->GetSafeHwnd());
		::SetWindowPos(hFig,NULL,1,1,Width-3,Height-3,SWP_NOZORDER | SWP_NOACTIVATE);

		// 绘制曲线
		DrawLine();

		// 设置窗口可见
		SetVisible(h_a,mwArray("On"));

		// 进入窗口等待状态
		bWait = 1;
		mlfHGWaitForFiguresToDie(); 
	}

	// 绘制曲线
	if(bWait == 0)  DrawLine();
}

void CMixFigureDlg::DrawLine()
{
	UpdateData(TRUE);
	if(tobool(isempty(h_a)))
		OnDrawline();
	else
		PlotLine(h_a,mwArray(m_A),mwArray(m_B),mwArray(m_C));
}

mwArray CMixFigureDlg::GenAxis(mwArray strFigName, mwArray BKColor, mwArray strVisible)
{
	// h_f = figure("DoubleBuffer","NumberTitle","Off","Name",strFigName, ...
	//              "Menubar","None","Color",BKColor,"Visible",strVisible);
	mwArray h_f = figure(
            mwArray("DoubleBuffer"), mwArray("On"),
            mwArray("NumberTitle"),  mwArray("Off"),
            mwArray("Name"),         strFigName,
            mwArray("Menubar"),      mwArray("None"),
            mwArray("Color"),        BKColor,
            mwArray("Visible"),      strVisible);
	
	// h_a = axes("Parent",h_f,"Box","On");
    mwArray h_a = axes(mwArray("Parent"), h_f, mwArray("Box"), mwArray("On"));
    return h_a;
}

void CMixFigureDlg::PlotLine(mwArray h_a, mwArray A, mwArray B, mwArray C)
{
	double range = 5;

	// x = -5 : 0.01 : 5;
    mwArray x = colon(mwArray(-1*range), mwArray(0.01), mwArray(range));

	// y = (c-a*x)/b;
    mwArray y = A * sin(B * x + C);

	// plot(x,y,"Parent",h_a);
    //Vplot(x, y, mwArray("Parent"), h_a);

	mix_plot(x,y);
}

void CMixFigureDlg::SetVisible(mwArray h_a, mwArray strVisible)
{
    // h_f = get(h_a,"Parent");
	mwArray h_f = get(h_a, mwArray("Parent"));

	// set(h_f,"Visible",strVisible);
    Vset(h_f, mwArray("Visible"), strVisible);
}

void CMixFigureDlg::OnEigvector() 
{

	// TODO: Add your control notification handler code here
	char receive[500];
	char temp[100];
	char *dis,*stopstring;
	double data[100];
	int j=0,n,i=0,length=0;
	GetDlgItemText(IDC_EDIT1,receive,500);
	//获得输入,转化为matlab可辨认的字符串

	while(receive[i++]!=']')
		;

	receive[i]='\0';
	i=0;
	while(receive[i++]!='[')
		;
	while(receive[i]!='\0')
	{
		j=0;
		while(isspace(receive[i])||receive[i]==';'||receive[i]==']')
			i++;

		while(isdigit(receive[i])||receive[i]=='.')
			temp[j++]=receive[i++];
		temp[j]='\0';
		data[length++]=strtod( temp, &stopstring);
		i++;
	}
	//判断是否输入为方阵

	n=(int)sqrt(length);
	if(length!=n*n)
	{
		AfxMessageBox("Error, not the square matrix");
		return;
	}
	//调用,计算特征向量

	mwArray A(n,n,data);
	mwArray T;
	
	A=transpose(A);
	T=mix_eigvector(A);
	T=mat2str(T,2);

	dis=strdup((char*)(T.ToString()));// 
	//添加换行符,转化为字符串输出
	char display[500];
	i=0;
	j=0;
	while(dis[i]!='\0')
	{
		if(dis[i]==';')
		{
			display[j++]=dis[i++];
			display[j++]=13;
			display[j++]='\n';
		}
		else
			display[j++]=dis[i++];
	}
	display[j]='\0';
	SetDlgItemText(IDC_EDIT2,(LPCTSTR)display);
}

void CMixFigureDlg::OnEigval() 
{
	// TODO: Add your control notification handler code here
	char receive[500];
	char temp[100];
	char *dis,*stopstring;
	double data[100];
	int j=0,n,i=0,length=0;
	GetDlgItemText(IDC_EDIT1,receive,500);
	//获得输入,转化为matlab可辨认的字符串
	while(receive[i++]!=']')
		;

	receive[i]='\0';
	i=0;
	while(receive[i++]!='[')
		;
	while(receive[i]!='\0')
	{
		j=0;
		while(isspace(receive[i])||receive[i]==';'||receive[i]==']')
			i++;

		while(isdigit(receive[i])||receive[i]=='.')
			temp[j++]=receive[i++];
		temp[j]='\0';
		data[length++]=strtod( temp, &stopstring);
		i++;
	}
	//判断是否输入为方阵
	n=(int)sqrt(length);
	if(length!=n*n)
	{
		AfxMessageBox("Error, not a square matrix");
		return;
	}
	mwArray A(n,n,data);
	mwArray T;
	
	A=transpose(A);
	//计算特征值 
	T=mix_eigvalue(A);
	T=mat2str(T,3);
	//添加换行符,转化为字符串输出
	dis=strdup((char*)(T.ToString()));
	SetDlgItemText(IDC_EDIT2,(LPCTSTR)dis);
}

void CMixFigureDlg::OnInv() 
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
	char receive[500];
	char temp[100];
	char *dis,*stopstring;
	double data[100];
	int j=0,n,i=0,length=0;
	GetDlgItemText(IDC_EDIT1,receive,500);
	//获得输入,转化为matlab可辨认的字符串
	while(receive[i++]!=']')
		;

	receive[i]='\0';
	i=0;
	while(receive[i++]!='[')
		;
	while(receive[i]!='\0')
	{
		j=0;
		while(isspace(receive[i])||receive[i]==';'||receive[i]==']')
			i++;

		while(isdigit(receive[i])||receive[i]=='.')
			temp[j++]=receive[i++];
		temp[j]='\0';
		data[length++]=strtod( temp, &stopstring);
		i++;
	}
	//判断是否输入为方阵
	n=(int)sqrt(length);
	if(length!=n*n)
	{
		AfxMessageBox("Error, not the square matrix");
		return;
	}
	mwArray A(n,n,data);
	mwArray T;
	
	A=transpose(A);
	//求逆
	T=mix_inv(A);
	T=mat2str(T,2);

	dis=strdup((char*)(T.ToString()));// 
	//添加换行符,转化为字符串输出
	char display[500];
	i=0;
	j=0;
	while(dis[i]!='\0')
	{
		if(dis[i]==';')
		{
			display[j++]=dis[i++];
			display[j++]=13;
			display[j++]='\n';
		}
		else
			display[j++]=dis[i++];
	}
	display[j]='\0';
	SetDlgItemText(IDC_EDIT2,(LPCTSTR)display);
}

void CMixFigureDlg::OnChangeEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not

	
}

void CMixFigureDlg::OnChangeEdit2() 
{
	// TODO: If this is a RICHEDIT control, the control will not

}

void CMixFigureDlg::OnDeltaposSpinB(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	m_B += -pNMUpDown->iDelta;
	UpdateData(FALSE);
	// 绘制曲线
	DrawLine();
	*pResult = 0;
}

void CMixFigureDlg::OnDeltaposSpinC(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	m_C += -pNMUpDown->iDelta;
	UpdateData(FALSE);
	// 绘制曲线
	DrawLine();

	*pResult = 0;
	
}

void CMixFigureDlg::OnChangeEditA() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CMixFigureDlg::OnPlotarea() 
{
	// TODO: Add your control notification handler code here
	
}

void CMixFigureDlg::OnChangeEditB() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CMixFigureDlg::OnChangeEditC() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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