// MixFigureDlg.cpp : implementation file
//本程序尝试将MATLAB的Figure窗口嵌入到VC窗口中,
//实现MATLAB窗口与VC 窗口的结合
#include "stdafx.h"
#include "MixFigure.h"
#include "MixFigureDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//这里省略了部分通用代码..
/////////////////////////////////////////////////////////////////////////////
// CMixFigureDlg dialog
CMixFigureDlg::CMixFigureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMixFigureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMixFigureDlg)
m_A = 1.0f;
m_Omega = 1.0f;
m_Theta = 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_EDIT_OMEGA, m_Omega);
DDX_Text(pDX, IDC_EDIT_THETA, m_Theta);
//}}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_NOTIFY(UDN_DELTAPOS, IDC_SPIN_OMEGA, OnDeltaposSpinOmega)
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_THETA, OnDeltaposSpinTheta)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_DRAWLINE, OnDrawline)
//}}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
// CDialog::OnClose();
// 在退出程序时关闭打开的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::OnDeltaposSpinOmega(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
m_Omega += -pNMUpDown->iDelta;
UpdateData(FALSE);
// 绘制曲线
DrawLine();
*pResult = 0;
}
void CMixFigureDlg::OnDeltaposSpinTheta(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
m_Theta += -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()
{
// Author : SCIE
// 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
PlotSin(h_a,mwArray(m_A),mwArray(m_Omega),mwArray(m_Theta));
}
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::PlotSin(mwArray h_a, mwArray A, mwArray Omega, mwArray Theta)
{
double pi = 3.141592653589793;
// t = -pi : 0.01 : pi;
mwArray t = colon(mwArray(-pi), mwArray(0.01), mwArray(pi));
// y = A * sin(Omega * t + Theta);
mwArray y = A * sin(Omega * t + Theta);
// plot(t,y,"Parent",h_a);
Vplot(t, y, mwArray("Parent"), h_a);
}
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);
}

评论