
// StopWatchDlg.cpp : t@C
//

#include "stdafx.h"
#include "StopWatch.h"
#include "StopWatchDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//---------------------------------------------------------
// TIMER wb_[t@C
//---------------------------------------------------------
#include "ctimerw.h"

// CStopWatchDlg _CAO



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

void CStopWatchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_edit_device_name);
	DDX_Control(pDX, IDC_STATIC_PRODUCT_NAME, m_static_product_name);
	DDX_Control(pDX, IDC_STATIC_TIME, m_static_time);
	DDX_Control(pDX, IDC_EDIT_RET, m_edit_ret);
}

BEGIN_MESSAGE_MAP(CStopWatchDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_TIM_INIT, &CStopWatchDlg::OnBnClickedButtonTimInit)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_START, &CStopWatchDlg::OnBnClickedButtonCountStart)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_LAP, &CStopWatchDlg::OnBnClickedButtonCountLap)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_RESET, &CStopWatchDlg::OnBnClickedButtonCountReset)
	ON_BN_CLICKED(IDC_BUTTON_COUNT_STOP, &CStopWatchDlg::OnBnClickedButtonCountStop)
	ON_BN_CLICKED(IDC_BUTTON_TIM_EXIT, &CStopWatchDlg::OnBnClickedButtonTimExit)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CStopWatchDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CStopWatchDlg bZ[W nh[

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

	// ̃_CAÕACRݒ肵܂BAvP[ṼC EBhE_CAOłȂꍇA
	//  Framework ́A̐ݒIɍs܂B
	SetIcon(m_hIcon, TRUE);			// 傫ACR̐ݒ
	SetIcon(m_hIcon, FALSE);		// ACR̐ݒ

	// TODO: ɒǉ܂B

	return TRUE;  // tH[JXRg[ɐݒ肵ꍇATRUE Ԃ܂B
}

// _CAOɍŏ{^ǉꍇAACR`悷邽߂
//  ̃R[hKvłBhLg/r[ fg MFC AvP[V̏ꍇA
//  ́AFramework ɂĎIɐݒ肳܂B

void CStopWatchDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // `̃foCX ReLXg

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// NCAg̎lp`̈̒
		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;

		// ACR̕`
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// [U[ŏEBhEhbOĂƂɕ\J[\擾邽߂ɁA
//  VXe̊֐Ăяo܂B
HCURSOR CStopWatchDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//==============================================================================
// TimInit
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonTimInit()
{
	char	DeviceName[256], ProductName[256], ErrorString[256];
	TCHAR	szDeviceName[256], szProductName[256], szBuf[256];
	long	ret;

	//-----------------------------------
	// foCX擾
	//-----------------------------------
	m_edit_device_name.GetWindowText(szDeviceName, 256);
	sprintf_s(DeviceName, "%S", szDeviceName);
	//-----------------------------------
	// 
	//-----------------------------------
	ret = TimInit(DeviceName, ProductName);
	//-----------------------------------
	// G[
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimInit ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		wsprintf(szDeviceName, _T("%S"), DeviceName);
		m_edit_device_name.SetWindowText(szDeviceName);
		wsprintf(szProductName, _T("%S"), ProductName);
		m_static_product_name.SetWindowText(szProductName);
	}
}

//==============================================================================
// JEgJn
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountStart()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// JEgJn
	//-----------------------------------
	ret = TimCountStart(&CountId);
	//-----------------------------------
	// G[
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountStart ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
}

//==============================================================================
// bv^C擾
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountLap()
{
	unsigned long	Microseconds;
	char			ErrorString[256];
	TCHAR			szBuf[256];
	long			ret;

	//-----------------------------------
	// bv^C擾
	//-----------------------------------
	ret = TimCountLap(CountId, &Microseconds);
	//-----------------------------------
	// G[
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountLap ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		wsprintf(szBuf, _T("%d"), Microseconds);
		m_static_time.SetWindowText(szBuf);
	}
}

//==============================================================================
// JEgZbg
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountReset()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// JEgZbg
	//-----------------------------------
	ret = TimCountReset(CountId);
	//-----------------------------------
	// G[
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountReset ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
}

//==============================================================================
// JEg~
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonCountStop()
{
	unsigned long	Microseconds;
	char			ErrorString[256];
	TCHAR			szBuf[256];
	long			ret;

	//-----------------------------------
	// JEg~
	//-----------------------------------
	ret = TimCountStop(CountId, &Microseconds);
	//-----------------------------------
	// G[
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimCountStop ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
	if (ret == TIM_ERR_SUCCESS){
		wsprintf(szBuf, _T("%d"), Microseconds);
		m_static_time.SetWindowText(szBuf);
	}
}

//==============================================================================
// TimExit
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonTimExit()
{
	char	ErrorString[256];
	TCHAR	szBuf[256];
	long	ret;

	//-----------------------------------
	// I
	//-----------------------------------
	ret = TimExit();
	//-----------------------------------
	// G[
	//-----------------------------------
	TimGetErrorString(ret, ErrorString);
	wsprintf(szBuf, _T("TimExit ret = %d %S"), ret, ErrorString);
	m_edit_ret.SetWindowText(szBuf);
}

//==============================================================================
// I
//==============================================================================
void CStopWatchDlg::OnBnClickedButtonExit()
{
	OnOK();
}
