
// WaveformJudgement1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "WaveformJudgement1.h"
#include "WaveformJudgement1Dlg.h"
#include "afxdialogex.h"
//-------------------------------------
// Include API-DIG(WDM) header file
//-------------------------------------
#include "cdig.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CWaveformJudgement1Dlg dialog



CWaveformJudgement1Dlg::CWaveformJudgement1Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CWaveformJudgement1Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWaveformJudgement1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_STATUS, m_Status);
	DDX_Control(pDX, IDC_STATIC_JUDGECOUNT_NG, m_NGCount);
	DDX_Control(pDX, IDC_EDIT_INFO, m_Info);
	DDX_Control(pDX, IDC_EDIT_DEVICE, m_DeviceName);
}

BEGIN_MESSAGE_MAP(CWaveformJudgement1Dlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INIT, &CWaveformJudgement1Dlg::OnBnClickedButtonInit)
	ON_BN_CLICKED(IDC_BUTTON_SETTING, &CWaveformJudgement1Dlg::OnBnClickedButtonSetting)
	ON_BN_CLICKED(IDC_BUTTON_START, &CWaveformJudgement1Dlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STATUS, &CWaveformJudgement1Dlg::OnBnClickedButtonStatus)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CWaveformJudgement1Dlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CWaveformJudgement1Dlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CWaveformJudgement1Dlg message handlers

BOOL CWaveformJudgement1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 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

	//---------------------------------
	// Initialize device name
	//---------------------------------
	m_DeviceName.SetWindowText(_T("DIG000"));

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

// 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 CWaveformJudgement1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
	{
		CDialogEx::OnPaint();
	}
}

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


//===============================================
// Init handling
//===============================================
void CWaveformJudgement1Dlg::OnBnClickedButtonInit()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	char	device_name[256];	// Device name
	TCHAR	szDevice_name[256];
	long	ret;				// Return value

	//---------------------------------
	// Init handling
	//---------------------------------
	m_DeviceName.GetWindowText(szDevice_name, sizeof(szDevice_name));
	sprintf_s(device_name, "%S", szDevice_name);
	ret = DigInit(device_name, &m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigInit = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}

	//---------------------------------
	// Reset device
	//---------------------------------
	ret = DigResetDevice(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigResetDevice = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}

	m_Info.SetWindowText(_T("Init : Normality completion"));
}

//===============================================
// Device Setting
//===============================================
void CWaveformJudgement1Dlg::OnBnClickedButtonSetting()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	//---------------------------------
	// Initialize variable
	//---------------------------------
	m_Records = 0;
	m_Resolution = 1024;		// Fixed setting of DIG-100M1002-PCI

	//---------------------------------
	// Set the operation mode : Waveform judgment instrument mode
	//---------------------------------
	ret = DigSetDigitizerMode(m_Id, DIG_MODE_JUDGE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetDigitizerMode = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the emergency halt factor of digitizer : Sampling clock error, Waveform judgment count match,
	//                                              Device memory overflow
	//---------------------------------
	ret = DigSetHaltCondition(m_Id, DIG_HALT_CLOCK_ERROR | DIG_HALT_JUDGE_COUNT | DIG_HALT_DEVMEM_OVERFLOW);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetHaltCondition = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling clock source : Crystal oscillator of board
	//---------------------------------
	ret = DigSetClockSource(m_Id, DIG_ECU_SRC_CLOCK);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetClockSource = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set sampling rate : 1MHz
	//---------------------------------
	ret = DigSetSampleRate(m_Id, 1000000);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetSampleRate = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the length of record : 512
	//---------------------------------
	ret = DigSetRecordLength(m_Id, MAX_SAMPLES);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRecordLength = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the number of records : Infinite
	//---------------------------------
	ret = DigSetRecordNumber(m_Id, 0);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRecordNumber = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set duty ratio : 50%
	//---------------------------------
	ret = DigSetDutyCycle(m_Id, 50.0);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetDutyCycle = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set channel : Enable channel0 only
	//---------------------------------
	// Enable channel0
	ret = DigSetChannel(m_Id, DIG_CHANNEL0, DIG_ENABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	// Disable channel1
	ret = DigSetChannel(m_Id, DIG_CHANNEL1, DIG_DISABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetChannel = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set range : +/-20V
	//---------------------------------
	ret = DigSetRange(m_Id, DIG_CHANNEL0, DIG_RANGE_40VPP);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetRange = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the type of pre-trigger : Auto
	//---------------------------------
	ret = DigSetPreTriggerType(m_Id, DIG_TRIGGERTYPE_AUTO);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPreTriggerType = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the type of post-trigger : Analog edge trigger
	//---------------------------------
	ret = DigSetPostTriggerType(m_Id, DIG_TRIGGERTYPE_AIEDGE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPostTriggerType = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Detailed setting for the post-trigger : TriggerSource : channel0, Trigger level : 0V, Rising edge, 1
	//---------------------------------
	ret = DigSetPostTriggerAiEdge(m_Id, DIG_CHANNEL0, 0.0, DIG_TRIGGER_SLOPE_RISE, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetPostTriggerAiEdge = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Specifies the channel to perform waveform judgment : Channel0
	//---------------------------------
	ret = DigSetJudgeChannel(m_Id, DIG_CHANNEL0, DIG_ENABLE);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetJudgeChannel = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Set the upper bound value for counting the waveform judgment result : Count the FAIL records, The upper bound value of count times is 1
	//---------------------------------
	ret = DigSetJudgeCountLimit(m_Id, DIG_CHANNEL0, DIG_JUDGE_NG, 1);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetJudgeCountLimit = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//----------------------------------
	// Generates the data for waveform judgment automatically : 
	// Margin of horizontal axis : -10V to +10V,
	// Margin of vertical axis : -50V to +50V,
	// New
	//----------------------------------
	ret = DigSetAutoMask(m_Id, DIG_CHANNEL0, 10, 50, DIG_AUTOMASK_NEW);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigSetAutoMask = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Retrieve the data for waveform judgment which has been set to digitizer
	//---------------------------------
	ret = DigGetJudgeData32(m_Id, DIG_CHANNEL0, MAX_SAMPLES, &m_UpperData[0], &m_LowerData[0]);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetJudgeData = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Set device : Normality completion"));
}

//===============================================
// Start judgement
//===============================================
void CWaveformJudgement1Dlg::OnBnClickedButtonStart()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	//---------------------------------
	// Initialize variable
	//---------------------------------
	m_Records = 0;

	//---------------------------------
	// Operation start
	//---------------------------------
	ret = DigStart(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStart = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Start judgement : Normality completion"));

	//---------------------------------
	// Polling until the operation status is changed to be OFF
	//---------------------------------
	long	status;				// Status
	long	ng_count;			// NG count

	do{
		//---------------------------------
		// In order to enable the dialog operation
		//---------------------------------
		MSG msg;
		while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)){
			if (!GetMessage(&msg, NULL, NULL, NULL)){
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		//---------------------------------
		// Get the number of records which are being stored in driver memory
		//---------------------------------
		ret = DigGetDriverMemoryRecords(m_Id, &m_Records);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetDriverMemoryRecords = %d : %s"), ret, szError_string);
			m_Info.SetWindowText(cstr);
			return;
		}
		//---------------------------------
		// Retrieve the acquisition data : Retrieve the data of 1 record from the new data
		//---------------------------------
		if (m_Records > 0 || m_Records == -1){
			ret = DigReadLatestRecords32(m_Id, DIG_RECORD_DATA, 1, &m_Data[0], NULL, NULL);
			if (ret != 0){
				DigGetErrorString(ret, error_string);
				wsprintf(szError_string, _T("%S"), error_string);
				cstr.Format(_T("DigReadLatestRecords32 = %d : %s"), ret, szError_string);
				m_Info.SetWindowText(cstr);
				return;
			}
			//-----------------------------------
			// Draw Graph
			//-----------------------------------
			DrawGraph();
		}
		//---------------------------------
		// Acquires fail times for the waveform judgment
		//---------------------------------
		ret = DigGetJudgeCount(m_Id, &ng_count);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetJudgeCount = %d : %s"), ret, szError_string);
			m_Info.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%d"), ng_count);
		m_NGCount.SetWindowText(cstr);
		//---------------------------------
		// Get the operation status
		//---------------------------------
		ret = DigGetStatus(m_Id, &status);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
			m_Info.SetWindowText(cstr);
			return;
		}
		cstr.Format(_T("%xH"), status);
		m_Status.SetWindowText(cstr);
	} while ((status & DIG_STATUS_BUSY) == DIG_STATUS_BUSY);
	//---------------------------------
	// Acquires fail times for the waveform judgment
	//---------------------------------
	ret = DigGetJudgeCount(m_Id, &ng_count);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetJudgeCount = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%d"), ng_count);
	m_NGCount.SetWindowText(cstr);
	//---------------------------------
	// Acquires the number of records not yet acquired in the driver memory
	//---------------------------------
	ret = DigGetDriverMemoryRecords(m_Id, &m_Records);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetDriverMemoryRecords = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	//---------------------------------
	// Retrieve the acquisition data : Retrieve the data of 1 record from the new data
	//---------------------------------
	if (m_Records > 0 || m_Records == -1){
		ret = DigReadLatestRecords32(m_Id, DIG_RECORD_DATA, 1, &m_Data[0], NULL, NULL);
		if (ret != 0){
			DigGetErrorString(ret, error_string);
			wsprintf(szError_string, _T("%S"), error_string);
			cstr.Format(_T("DigReadLatestRecords32 = %d : %s"), ret, szError_string);
			m_Info.SetWindowText(cstr);
			return;
		}
		//-----------------------------------
		// Draw Graph
		//-----------------------------------
		DrawGraph();
	}
}

//===============================================
// Get status
//===============================================
void CWaveformJudgement1Dlg::OnBnClickedButtonStatus()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value
	long	status;				// Status

	//---------------------------------
	// Get the operation status
	//---------------------------------
	ret = DigGetStatus(m_Id, &status);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigGetStatus = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	cstr.Format(_T("%xH"), status);
	m_Status.SetWindowText(cstr);
	m_Info.SetWindowText(_T("Get status : Normality completion"));
}

//===============================================
// Stop judgement
//===============================================
void CWaveformJudgement1Dlg::OnBnClickedButtonStop()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	//---------------------------------
	// Stop judgement
	//---------------------------------
	ret = DigStop(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigStop = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Stop judgement : Normality completion"));
}

//===============================================
// Exit handling
//===============================================
void CWaveformJudgement1Dlg::OnBnClickedButtonExit()
{
	CString cstr;				// For saving string temporarily
	char	error_string[256];	// Error string
	TCHAR	szError_string[256];
	int		ret;				// Return value

	ret = DigExit(m_Id);
	if (ret != 0){
		DigGetErrorString(ret, error_string);
		wsprintf(szError_string, _T("%S"), error_string);
		cstr.Format(_T("DigExit = %d : %s"), ret, szError_string);
		m_Info.SetWindowText(cstr);
		return;
	}
	m_Info.SetWindowText(_T("Exit : Normality completion"));
}

//===============================================
// Draw graph
//===============================================
void CWaveformJudgement1Dlg::DrawGraph()
{
	float	x_unit;				// Drawing unit of X axis
	int		y_pix;				// Drawing coordinate of Y axis
	CWnd	*graph;				// Graph object
	CRect	rect;				// Rect of graph object
	CDC		*dc;				// Device context
	CDC		mem_dc;				// Device context for buffer
	CBitmap	bitmap;				// Bitmap class object

	//-------------------------------------------
	// Draw the current data in graph
	//-------------------------------------------
	graph = GetDlgItem(IDC_GRAPH);									// Get the handle of graph object
	dc = graph->GetDC();											// Get the device context of graph object
	graph->GetClientRect(&rect);									// Get the rect of graph object
	bitmap.CreateCompatibleBitmap(dc, rect.right, rect.bottom);		// Create the bitmap which is compatible with the rect of graph object
	mem_dc.CreateCompatibleDC(dc);									// Copy the device context
	HBITMAP h_old_bitmap = (HBITMAP)mem_dc.SelectObject(&bitmap);	// Setup the created bitmap in order to be written
	mem_dc.FillSolidRect(&rect, RGB(255, 255, 255));				// Fill the graph rect with white

	CPen pen(PS_SOLID, 1, RGB(255, 0, 0));							// Create red pen
	CPen *oldpen = mem_dc.SelectObject(&pen);						// Select the created pen

	for (int i = 0; i < 3; i++){
		x_unit = (float)rect.Width() / (float)(MAX_SAMPLES);		// Calculate the moving pixel number of X axis for data 1
		switch (i){
			// Set the origin position of upper bound data
		case 0:
			y_pix = (int)((float)rect.Height() * (m_Resolution - m_UpperData[0]) / (float)m_Resolution);	// Calculate the coordinate of Y axis
			break;
			// Set the origin position of lower bound data
		case 1:
			y_pix = (int)((float)rect.Height() * (m_Resolution - m_LowerData[0]) / (float)m_Resolution);	// Calculate the coordinate of Y axis
			break;
			// Set the origin position of retrieving data
		default:
			y_pix = (int)((float)rect.Height() * (m_Resolution - m_Data[0]) / (float)m_Resolution);			// Calculate the coordinate of Y axis
			break;
		}
		if (y_pix >= rect.Height())	y_pix = rect.Height() - 1;		// If the coordinate of Y axis exceeds the drawing rect, modify the drawing position
		if (y_pix <= 0)	y_pix = 1;									// If the coordinate of Y axis is lower than the drawing rect, modify the drawing position
		mem_dc.MoveTo(0, y_pix);									// Move to the initial position
		//---------------------------------------------
		// Repeat the drawing handling only for the times which is equal to the numer of acquisition data
		//---------------------------------------------
		for (int j = 1; j < MAX_SAMPLES; j++){
			switch (i){
				// Set the drawing position of upper bound data
			case 0:
				y_pix = (int)((float)rect.Height() * (m_Resolution - m_UpperData[j]) / (float)m_Resolution);	// Calculate the coordinate of Y axis
				break;
				// Set the drawing position of lower bound data
			case 1:
				y_pix = (int)((float)rect.Height() * (m_Resolution - m_LowerData[j]) / (float)m_Resolution);	// Calculate the coordinate of Y axis
				break;
				// Set the drawing position of retrieving data
			default:
				y_pix = (int)((float)rect.Height() * (m_Resolution - m_Data[j]) / (float)m_Resolution);			// Calculate the coordinate of Y axis
				break;
			}
			// If the coordinate of Y axis exceeds the drawing rect
			if (y_pix >= rect.Height()){
				y_pix = rect.Height() - 1;							// Modify the drawing position
			}
			// If the coordinate of Y axis is lower than the drawing rect
			else if (y_pix <= 0){
				y_pix = 1;											// Modify the drawing position
			}
			mem_dc.LineTo((int)(x_unit * j) + 1, y_pix);			// Draw in buffer
		}
	}
	//----------------------------------------------
	// Transmit to form
	//----------------------------------------------
	dc->BitBlt(0, 0, rect.right, rect.bottom, &mem_dc, 0, 0, SRCCOPY);

	mem_dc.SelectObject(oldpen);									// Select the defualt pen
	mem_dc.SelectObject(&h_old_bitmap);
	graph->ReleaseDC(dc);											// Release the device context
}