// MeasureDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Measure.h"
#include "MeasureDlg.h"
#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()
/////////////////////////////////////////////////////////////////////////////
// CMeasureDlg dialog
CMeasureDlg::CMeasureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMeasureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMeasureDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//初始化
m_milApplication = m_milSystem = m_milDisplay = M_NULL;
m_milSingleBuf = m_milSingleOverlayBuf = m_milSingleMarker = M_NULL;
m_milMultiBuf = m_milMultiOverlayBuf = m_milMultiMarker = M_NULL;
}
CMeasureDlg::~CMeasureDlg()
{
if (M_NULL != m_milMultiMarker)
{
MmeasFree(m_milMultiMarker);
}
if (M_NULL != m_milMultiBuf)
{
MbufFree(m_milMultiBuf);
}
if (M_NULL != m_milSingleMarker)
{
MmeasFree(m_milSingleMarker);
}
if (M_NULL != m_milSingleBuf)
{
MbufFree(m_milSingleBuf);
}
if (M_NULL != m_milApplication)
{
MappFreeDefault(m_milApplication, m_milSystem, m_milDisplay, M_NULL, M_NULL);
}
}
void CMeasureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMeasureDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMeasureDlg, CDialog)
//{{AFX_MSG_MAP(CMeasureDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDB_READ_SINGLE, OnReadSingle)
ON_BN_CLICKED(IDB_SET_SINGLE, OnSetSingle)
ON_BN_CLICKED(IDB_GET_SINGLE, OnGetSingle)
ON_BN_CLICKED(IDB_READ_MULTI, OnReadMulti)
ON_BN_CLICKED(IDB_SET_MULTI, OnSetMulti)
ON_BN_CLICKED(IDB_GET_MULTI, OnGetMulti)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMeasureDlg message handlers
BOOL CMeasureDlg::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
//分配MIL系统
MappAllocDefault(M_SETUP, &m_milApplication, &m_milSystem, &m_milDisplay, M_NULL, M_NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMeasureDlg::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 CMeasureDlg::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 CMeasureDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMeasureDlg::OnReadSingle()
{
// TODO: Add your control notification handler code here
if (M_NULL != m_milSingleBuf)
{
MbufFree(m_milSingleBuf);
m_milSingleBuf = M_NULL;
}
//导入图像
MbufRestore(TEXT(".\\Image\\Lead.mim"), m_milSystem, &m_milSingleBuf);
//显示图像
MdispZoom(m_milDisplay, 0.8, 0.8);
MdispControl(m_milDisplay, M_CENTER_DISPLAY , M_ENABLE);
MdispSelectWindow(m_milDisplay, m_milSingleBuf, GetDlgItem(IDP_DISP_IMAGE)->GetSafeHwnd());
MdispControl(m_milDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
}
void CMeasureDlg::OnSetSingle()
{
// TODO: Add your control notification handler code here
if (M_NULL != m_milSingleMarker)
{
MmeasFree(m_milSingleMarker);
m_milSingleMarker = M_NULL;
}
//分配Stripe Maker
MmeasAllocMarker(m_milSystem, M_STRIPE, M_DEFAULT, &m_milSingleMarker);
//设置Maker Box
MmeasSetMarker(m_milSingleMarker, M_BOX_ORIGIN, MEAS_BOX_POS_X, MEAS_BOX_POS_Y);
MmeasSetMarker(m_milSingleMarker, M_BOX_SIZE, MEAS_BOX_WIDTH, MEAS_BOX_HEIGHT);
//设置Maker Characteristics
MmeasSetMarker(m_milSingleMarker, M_POLARITY, STRIPE_POLARITY_LEFT, STRIPE_POLARITY_RIGHT);//第一边缘为上升沿,第二边缘为下降沿
MmeasSetMarker(m_milSingleMarker, M_WIDTH, STRIPE_WIDTH, M_NULL);//宽度
MmeasSetMarker(m_milSingleMarker, M_WIDTH_VARIATION, STRIPE_WIDTH_VARIATION, M_NULL);//宽度偏差
MmeasSetMarker(m_milSingleMarker, M_BOX_ANGLE_MODE, M_ENABLE, M_NULL);//角度可变(360)
//在图像的Overlay层上绘制Maker Box
MdispControl(m_milDisplay, M_OVERLAY, M_ENABLE);
MdispInquire(m_milDisplay, M_OVERLAY_ID, &m_milSingleOverlayBuf);
MdispControl(m_milDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
MgraColor(M_DEFAULT, M_COLOR_RED);
MmeasDraw(M_DEFAULT, m_milSingleMarker, m_milSingleOverlayBuf, M_DRAW_BOX, M_DEFAULT, M_MARKER);
}
void CMeasureDlg::OnGetSingle()
{
// TODO: Add your control notification handler code here
//目标图像中寻找Maker,测量结果保存在Maker中
MmeasFindMarker(M_DEFAULT, m_milSingleBuf, m_milSingleMarker, M_DEFAULT);
//获得测量结果(位置、宽度、角度)
double StripeCenterX, /* Stripe X center position.*/
StripeCenterY, /* Stripe Y center position.*/
StripeWidth, /* Stripe width. */
StripeAngle; /* Stripe angle. */
MmeasGetResult(m_milSingleMarker, M_POSITION, &StripeCenterX, &StripeCenterY);
MmeasGetResult(m_milSingleMarker, M_WIDTH, &StripeWidth, M_NULL);
MmeasGetResult(m_milSingleMarker, M_ANGLE, &StripeAngle, M_NULL);
//绘制 Maker Box
MgraColor(M_DEFAULT, M_COLOR_BLACK);
MdispControl(m_milDisplay, M_OVERLAY_CLEAR, M_DEFAULT);//清除显示OverLay层
MmeasDraw(M_DEFAULT, m_milSingleMarker, m_milSingleOverlayBuf, M_DRAW_BOX, M_DEFAULT, M_RESULT);
//绘制测量结果
MgraColor