
// TestMFCDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "TestMFC.h"
#include "TestMFCDlg.h"
#include "afxdialogex.h"
#include "IdCardWrappageLib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
#pragma comment(lib,"IdCardLibWrappage.lib")

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

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTestMFCDlg 对话框




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

void CTestMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PICTURE, m_filebmp);
}

BEGIN_MESSAGE_MAP(CTestMFCDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CTestMFCDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CTestMFCDlg::OnBnClickedCancel)
	ON_BN_CLICKED(IDC_BUTTON1, &CTestMFCDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CTestMFCDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CTestMFCDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON_SIM_ID, &CTestMFCDlg::OnBnClickedButtonSimId)
	ON_BN_CLICKED(IDC_BUTTON5, &CTestMFCDlg::OnBnClickedButton5)
END_MESSAGE_MAP()


// CTestMFCDlg 消息处理程序

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

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

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

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

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

 CString CTestMFCDlg::GetMsgInf(int State)
{
	switch (State)
	{
	case 0:
		return _T("身份证读取成功:0");
	case -1:
		return _T("读卡器连接失败,请连接读卡器:-1");
	case -2:
		return _T("云解码服务器连接异常,请重试:-2");
	case -3:
		return _T("云解码服务器异常,请重试:-3");
	case -4:
		return _T("身份证解析超时,请重试:-4");
	case -5:
		return _T("身份证最后一包数据异常,请重试:-5");
	case -6:
		return _T("接受服务器数据失败,请重试:-6");
	case -14:
		return _T("没有检测到数据,请重试:-14:");
	case -15:
		return _T("没有检测到SIMID数据,请重试:-15:");
	case -100:
		return _T("没有检测到网络,请开启网络:-100");
	case -200:
		return _T("服务器获取密钥异常,请重试:-200");
	case -300:
		return _T("没有找到指定的密钥服务器,请重试:-300");
	case -401:
		return _T("获取UID失败");
	case -402:
		return _T("申请内存不足");
	case -403:
		return _T("获取IC卡失败");
	case -404:
		return _T("识别为身份证不是IC卡");
	case -405:
		return _T("未知卡");
	default:
		return _T("失败");
	}
}
// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CTestMFCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

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

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTestMFCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CTestMFCDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnOK();
}


void CTestMFCDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}


void CTestMFCDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	
	int ret=KsReadIDCard();
	if (ret!=0)
	{
		CString str = GetMsgInf(ret);
		CString tt;
		tt.Format("%s_%d",str,ret);
		AfxMessageBox(tt);
		return;

	}
	char buf[100] = "";
	//姓名
	CString strName;
	int len = sizeof(buf);
	KsGetName(buf,len);
	strName.Format(_T("%s"),buf);
	GetDlgItem(IDC_EDIT1)->SetWindowText(strName); 
	memset(buf,0,sizeof(buf));
	//姓别
	CString strsex;
	KsGetSex(buf,len);
	strsex.Format(_T("%s"),buf);
	GetDlgItem(IDC_EDIT3)->SetWindowText(strsex); 
	memset(buf,0,sizeof(buf));
	//出生日期
	CString strbirth;
	KsBirthday(buf,len);
	strbirth.Format(_T("%s"),buf);
	GetDlgItem(IDC_EDIT5)->SetWindowText(strbirth); 
	memset(buf,0,sizeof(buf));
	//民族
	CString strethnicity;
	KsGetNation(buf,len);
	strethnicity.Format(_T("%s"), buf);
	GetDlgItem(IDC_EDIT4)->SetWindowText(strethnicity); 
	memset(buf,0,sizeof(buf));
	//地址
	CString straddress;
	KsAddress(buf,len);
	straddress.Format(_T("%s"), buf);
	GetDlgItem(IDC_EDIT6)->SetWindowText(straddress); 
	memset(buf,0,sizeof(buf));
	//身份证号码
	CString strcardNo;
	KsGetPeopleIDCode(buf,len);
	strcardNo.Format(_T("%s"), buf);
	GetDlgItem(IDC_EDIT7)->SetWindowText(strcardNo); 
	memset(buf,0,sizeof(buf));
	//有效日.	
	CString strperiod;
	KsGetStartTime(buf,len);
	strperiod.Format(_T("%s"), buf);
	GetDlgItem(IDC_EDIT9)->SetWindowText(strperiod); 
	memset(buf,0,sizeof(buf));
	CString strendtime;
	KsGetEndTime(buf,len);
	strendtime.Format(_T("%s"), buf);
	GetDlgItem(IDC_EDIT2)->SetWindowText(strendtime); 
	memset(buf,0,sizeof(buf));
	//发证机关
	CString strauthority;
	KsGetDepartment(buf,len);
	strauthority.Format(_T("%s"), buf);
	GetDlgItem(IDC_EDIT8)->SetWindowText(strauthority); 
	memset(buf,0,sizeof(buf));

	//char  szDLLPath[256];
	//GetModuleFileNameA(NULL, szDLLPath, 256);
	////初始化动态库
	//string szTmp = ExtractFilePath(szDLLPath) + "1.bmp";
	KsGetIdCardImgPath(buf,len);
	HBITMAP bmp = (HBITMAP)::LoadImage(0, buf,IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR|LR_LOADFROMFILE);
	m_filebmp.SetBitmap(bmp);   
	DeleteObject(bmp);
}

string CTestMFCDlg::ExtractFilePath(const string& szFile)
{
	if (szFile == "")
		return "";

	size_t idx = szFile.find_last_of("\\:");

	if (-1 == idx)
		return "";
	string szTmp = string(szFile.begin(), szFile.begin()+idx+1); 
	return string(szFile.begin(), szFile.begin()+idx+1); 
}

void CTestMFCDlg::ClearIDData()
{
	//姓名
	GetDlgItem(IDC_EDIT1)->SetWindowText(""); 
	//姓别
	GetDlgItem(IDC_EDIT3)->SetWindowText(""); 
	//出生日期
	GetDlgItem(IDC_EDIT5)->SetWindowText(""); 

	//民族
	GetDlgItem(IDC_EDIT4)->SetWindowText(""); 

	//地址
	GetDlgItem(IDC_EDIT6)->SetWindowText(""); 
	//身份证号码
	GetDlgItem(IDC_EDIT7)->SetWindowText(""); 
	//有效日.	
	GetDlgItem(IDC_EDIT9)->SetWindowText(""); 
	GetDlgItem(IDC_EDIT8)->SetWindowText("");
	GetDlgItem(IDC_EDITSimid)->SetWindowText(""); 
	GetDlgItem(IDC_EDIT10)->SetWindowText(""); 
	GetDlgItem(IDC_EDITic)->SetWindowText(""); 
	m_filebmp.SetBitmap(NULL);
}
void CTestMFCDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	ClearIDData();
}




void CTestMFCDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	char szguid[32]={0};
	int len = 32;
	int ret=KsGetGuidInfo(szguid,32);
	if (ret!=0)
	{
		CString str = GetMsgInf(ret);
		CString tt;
		tt.Format("%s_%d",str,ret);
		AfxMessageBox(tt);
		return;
	}else
	{
		CString strGUID;
		strGUID.Format(_T("%s"), szguid);
		GetDlgItem(IDC_EDIT10)->SetWindowText(strGUID); 
	}
}


void CTestMFCDlg::OnBnClickedButtonSimId()
{
	// TODO: 在此添加控件通知处理程序代码
	char szguid[100] = {0};
	int len = 100;
	int ret = KsGetSimIDInfo(szguid,100);
	if (ret!=0)
	{
		CString str = GetMsgInf(ret);
		CString tt;
		tt.Format("%s_%d",str,ret);
		AfxMessageBox(tt);
		return;
	}else
	{
		CString strGUID;
		strGUID.Format(_T("%s"), szguid);
		GetDlgItem(IDC_EDITSimid)->SetWindowText(strGUID); 
	}
}


void CTestMFCDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	char szic[100] = {0};
	int len = 100;
	int ret = KsGetIcCardInfo(szic,100);
	if (ret!=0)
	{
		CString str = GetMsgInf(ret);
		CString tt;
		tt.Format("%s_%d",str,ret);
		AfxMessageBox(tt);
		return;
	}else
	{
		CString strIC;
		strIC.Format(_T("%s"), szic);
		GetDlgItem(IDC_EDITic)->SetWindowText(strIC); 
	}
}
