DLCardToolDlg.cpp 22.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806

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

#include "stdafx.h"
#include "DLCardTool.h"
#include "DLCardToolDlg.h"
#include "afxdialogex.h"
#include <vector>
#include "password_machine.h"
#include "win_http.h"
#include "DLDes.h"
#include <boost/filesystem.hpp>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


static const char* MF_FILE_DIR = "3F00";
static const char* DF01_DIR = "DF01";

static const char BASIC_INFO_FILE_ID = 0x5;
static const char SERVICE_INFO_FILE_ID = 0x15;

static const char* K_DCCK_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0";
static const char* K_F0015_DACK_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6";
static const char* K_F0015_DEAK_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7";
static const char* K_F0015_DAMK1_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8";

static const char* K_F0005_DCMK1 = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4";


const CString kSuccessText = L"升级成功!";
const CString kFailedText = L"升级失败!";
const CString kNotNeedUpdate = L"卡片是最新配置,无需升级。";

// CDLCardToolDlg 对话框



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

void CDLCardToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_RICHEDIT_NOTICE, m_cNotice);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN, m_cExplain);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN1, m_cExplain1);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN2, m_cExplain2);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN3, m_cExplain3);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN4, m_cExplain4);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN5, m_cExplain5);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN6, m_cExplain6);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN7, m_cExplain7);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN8, m_cExplain8);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN9, m_cExplain9);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN10, m_cExplain10);
	DDX_Control(pDX, IDC_STATIC_EXPLAIN11, m_cExplain11);
	DDX_Control(pDX, IDC_BUTTON_CARDUPDATE, m_cUpdateButton);
	DDX_Control(pDX, IDC_STATIC_CARDNUM, m_cStaticCardNum);
	DDX_Control(pDX, IDC_STATIC_CARD_NUM_TEXT, m_cStaticCardNumText);
	DDX_Control(pDX, IDC_STATIC_RESULT, m_cResult);
	DDX_Control(pDX, IDC_STATIC_RESULT_TEXT, m_cResultText);
	DDX_Control(pDX, IDC_STATIC_IMG, m_cStaticImg);
	DDX_Control(pDX, IDC_STATIC_RESULT2, m_cResultText2);
}

BEGIN_MESSAGE_MAP(CDLCardToolDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CARDUPDATE, &CDLCardToolDlg::OnBnClickedButtonCardupdate)
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CDLCardToolDlg 消息处理程序

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

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


	SetWindowText(L"地利卡片升级工具");
	CRect rtDlg;
	rtDlg.top = rtDlg.left = 0;
	//保持黄金比例,完美!
	rtDlg.bottom = 600;
	rtDlg.right = 616;
	MoveWindow(&rtDlg);

	m_cConfig.ReadConfig();

	SetCtrlText();
	SetCtrlImg();
	SetCtrlFont();
	ResizeCtrl();

	Invalidate(TRUE);// 重绘界面,解决界面控件显示不全的问题

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CDLCardToolDlg::OnPaint()
{
	CPaintDC dc(this); // 用于绘制的设备上下文  
	CPen *pOldPen = dc.SelectObject(&m_cPen);//保存DC原始画笔  

	//CRect rtExplain;
	//m_cExplain9.GetWindowRect(&rtExplain);
	//CRect rtPanel;
	//GetClientRect(&rtPanel);

	//dc.MoveTo(26, rtExplain.bottom + 24);         //设置DC当前点  
	//dc.LineTo(rtPanel.right - 26, rtExplain.bottom + 24);            //绘制直线  
	//dc.SelectObject(pOldPen);       //回复DC原画笔  

	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 CDLCardToolDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

template <typename T>
std::string DecToHex(T d, bool show_base = false)
{
	std::stringstream ss;
	if (show_base)
		ss << "0x";
	ss << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << static_cast<int>(d);
	return ss.str();
}

void CDLCardToolDlg::OnBnClickedButtonCardupdate()
{
	SetMsgLog(L"卡片升级。");

	int iErrorCode = 0;
	bool bSuccess = true;
	do
	{
		SetMsgLog(L"连接读卡器。");
		iErrorCode = DLCConnectDevice();
		if (iErrorCode)
		{
			SetMsgLog(L"打开读卡器失败!", LOG_ERROR, iErrorCode);
			SetResultTest(kFailedText, L"原因:打开读卡器失败!");
			bSuccess = false;
			break;
		}

		SetMsgLog(L"寻找卡片。");
		iErrorCode = DLCFindCard();
		if (iErrorCode)
		{
			SetMsgLog(L"寻卡失败!", LOG_ERROR, iErrorCode);
			SetResultTest(kFailedText, L"原因:寻卡失败!");
			bSuccess = false;
			break;
		}

		SetMsgLog(L"读取基本信息。");
		BasicInfo biCardInfo;
		try
		{
			biCardInfo = ReadBasicInfo();
		}
		catch (std::exception &ex)
		{
			std::string sError = ex.what();
			SetMsgLog(L"读取卡片基本信息失败!", LOG_ERROR, atoi(sError.c_str()));
			SetResultTest(kFailedText, L"原因:读取卡片基本信息失败!");
			bSuccess = false;
			break;
		}

		SetMsgLog(CString("卡号:") + str_2_wstr(biCardInfo.sCardNum).c_str());

		SetMsgLog(L"查询卡片状态。");
		CardStateInfo csiCardStateInfo;
		csiCardStateInfo = CheckCardState(biCardInfo);

		m_cStaticCardNumText.SetWindowTextW(str_2_wstr(biCardInfo.sCardNum).c_str());


		if (csiCardStateInfo.bAlreadyUpdate)
		{
			SetMsgLog(kNotNeedUpdate, LOG_ERROR);
			SetResultTest(kNotNeedUpdate, L"");
			bSuccess = false;
			break;
		}

		SetMsgLog(L"获取主控秘钥。");
		std::string sMainDCCK;
		try
		{
			GetMainKey(m_cConfig.m_bUsePWM, biCardInfo.sCardOldSn, sMainDCCK);
		}
		catch (std::exception &ex)
		{
			std::string sError = ex.what();
			SetMsgLog(str_2_wstr(sError).c_str(), LOG_ERROR);
			SetResultTest(kFailedText, L"获取主控秘钥失败!");
			bSuccess = false;
			break;
		}

		unsigned char aucSize[4] = { 0 };
		memcpy(aucSize, "0324", 4);
		iErrorCode = DLCCreateFile((const unsigned char*)"3f00", (const unsigned char*)sMainDCCK.c_str(), (const unsigned char*)"df20", NULL, 1);
		if (iErrorCode)
		{
			SetMsgLog(CString("创建文件夹失败!错误码:" + iErrorCode), LOG_ERROR);
			SetResultTest(kFailedText, L"创建文件夹失败!");
			bSuccess = false;
			break;
		}

		iErrorCode = DLCCreateFile((const unsigned char*)"df20", (const unsigned char*)sMainDCCK.c_str(), (const unsigned char*)"0025", aucSize, 3);
		if (iErrorCode)
		{
			SetMsgLog(CString("创建文件失败!错误码:" + iErrorCode), LOG_ERROR);
			SetResultTest(kFailedText, L"创建文件失败!");
			bSuccess = false;
			break;
		}
		memcpy(aucSize, "0709", 4);
		iErrorCode = DLCCreateFile((const unsigned char*)"df20", (const unsigned char*)sMainDCCK.c_str(), (const unsigned char*)"0026", aucSize, 3);
		if (iErrorCode)
		{
			SetMsgLog(CString("创建文件失败!错误码:" + iErrorCode), LOG_ERROR);
			SetResultTest(kFailedText, L"创建文件失败!");
			bSuccess = false;
			break;
		}

	} while (0);

	SetMsgLog(L"关闭读卡器。");
	DLCDisconnectDevice();

	if (bSuccess)
	{
		SetMsgLog(L"升级成功!", LOG_ERROR);
		SetResultTest(kSuccessText, L"");
	}
}


void CDLCardToolDlg::GetMainKey(bool bUsePWM, std::string sSN, std::string &sDCCK)
{

	if (bUsePWM)
	{
		sDCCK = m_cPWDMachine.GetPassword("0000", sSN, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
	}
	else
	{
		sDCCK = K_DCCK_DV;
	}
}

void CDLCardToolDlg::SetMsgLog(CString sText, LOG_TYPE emLogType /*= LOG_TRACE*/, int iErrorCode /*= 0*/)
{
	CString sErrorCode;
	if (iErrorCode)
	{
		sErrorCode.Format(L",错误码: %d", iErrorCode);
	}
	CString sLogType = emLogType == LOG_TRACE ? L"<trace> " : L"<error> ";
	
	Log(sLogType + sText + sErrorCode);
}

HBRUSH CDLCardToolDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	if (pWnd == &m_cExplain2 || pWnd == &m_cExplain4)
	{
		pDC->SetTextColor(RGB(236, 72, 34));
	}
	else if (pWnd == &m_cExplain10)
	{
		pDC->SetTextColor(RGB(223, 78, 33));
	}
	else if (pWnd == &m_cExplain || pWnd == &m_cExplain6 || pWnd == &m_cExplain8
		|| pWnd == &m_cExplain9 || pWnd == &m_cExplain11 || pWnd == &m_cStaticCardNum
		|| pWnd == &m_cResult)
	{
		pDC->SetTextColor(RGB(102, 102, 102));
	}
	else if (pWnd == &m_cExplain1 || pWnd == &m_cExplain3 || pWnd == &m_cExplain5)
	{
		pDC->SetTextColor(RGB(51, 51, 51));
	}
	else if (pWnd == &m_cExplain7)
	{
		pDC->SetTextColor(RGB(47, 166, 35));
	}
	else if (pWnd == &m_cResultText && m_bSetRed)
	{
		pDC->SetTextColor(RGB(223, 78, 33));
		m_bSetRed = false;
	}

	return hbr;
}

CardStateInfo CDLCardToolDlg::CheckCardState(BasicInfo biCardInfo)
{
	CardStateInfo stuCardState;

	std::string sDCCK;

	if (m_cConfig.m_bUsePWM)
	{
		sDCCK = m_cPWDMachine.GetPassword("0000", biCardInfo.sCardOldSn, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
	}
	else
	{
		sDCCK = std::string("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0");
	}
	
	if (!DLCSelectFile((unsigned char*)"df20"))
	{
		stuCardState.bAlreadyUpdate = true;
	}
	else
	{
		stuCardState.bAlreadyUpdate = false;
	}

	return stuCardState;
}

//CardStateInfo CDLCardToolDlg::ReadJson(CString sResponseBody)
//{
//	CardStateInfo stuServiceData;
//
//	static const std::string JSON_TAG_STATUS = "status";
//	static const std::string JSON_TAG_SECRETSTR = "secretStr";
//	
//	static const std::string JSON_VAL_UNSTORAGE = "0";
//	static const std::string JSON_VAL_STORAGE = "1";
//	static const std::string JSON_VAL_USE = "2";
//
//	namespace PT = boost::property_tree;
//	bool login_res = false;
//	try //Parse the configuration file
//	{
//		PT::ptree ptree;
//		std::stringstream ss;
//		ss << wstr_2_str(sResponseBody);
//		PT::read_json(ss, ptree);
//		std::string sStatus = ptree.get<std::string>(JSON_TAG_STATUS);
//		if (sStatus.compare(JSON_VAL_UNSTORAGE) == 0)
//		{
//			stuServiceData.csCardState = CS_UNSTORAGE;
//		}
//		else if (sStatus.compare(JSON_VAL_STORAGE) == 0)
//		{
//			stuServiceData.csCardState = CS_STORAGE;
//		}
//		else if (sStatus.compare(JSON_VAL_USE) == 0)
//		{
//			stuServiceData.csCardState = CS_USE;
//			stuServiceData.sServiceData = ptree.get<std::string>(JSON_TAG_SECRETSTR).c_str();
//		}
//		
//		
//	}
//	catch (...)
//	{
//		throw std::exception("解析服务器返回的信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!", LOG_ERROR);
//	}
//
//	return stuServiceData;
//}

BasicInfo CDLCardToolDlg::ReadBasicInfo()
{
	BasicInfo biCardBasicInfo;

	unsigned char aucDirMF[4] = { 0 };
	unsigned char aucCardBasicInfo[88] = { 0 };
	memcpy(aucDirMF, MF_FILE_DIR, 4);
	int iErrorCode = DLCReadCardData(aucDirMF, BASIC_INFO_FILE_ID, 0, 88, NULL, aucCardBasicInfo); //实际上取出了所有基本信息

	std::string sCardnum((char *)aucCardBasicInfo, 0, 12);
	std::string sOldSn((char*)aucCardBasicInfo, 56, 88);

	biCardBasicInfo.sCardNum = sCardnum;
	biCardBasicInfo.sCardOldSn = sOldSn;

	return biCardBasicInfo;
}

//std::string CDLCardToolDlg::CalculateKey(BasicInfo biCardBasicInfo, KEY_TYPE ktKeyType, int iEncryptType)
//{
//	std::string sNewKey;
//	if (iEncryptType == 0)
//	{
//		std::string sKeyType;
//		switch (ktKeyType)
//		{
//		case KT_DACK:
//		case KT_DCCK:
//		{
//			sKeyType = "0000";
//			break;
//		}
//		case KT_DEAK:
//		{
//			sKeyType = "0001";
//			break;
//		}
//		case KT_DAMK:
//		{
//			sKeyType = "0002";
//			break;
//		}
//		}
//		sNewKey = m_cPWDMachine.GetPassword(sKeyType, biCardBasicInfo.sC, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
//	}
//	else
//	{
//		const char* pDCCKDL = "43434bff43434bff";
//		const char* pDACKDL = "41434bff41434bff";
//		const char* pDEAKDL = "45414bff45414bff";
//		const char* pDAMKDL = "414d4b31414d4b31";
//
//		unsigned char aucCardSNHex[16] = { 0 };
//		unsigned char aucDCCKDL[8] = { 0 }, aucDACKDL[8] = { 0 }, aucDEAKDL[8] = { 0 }, aucDAMKDL[8] = { 0 };
//		StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
//		StringToHex((unsigned char*)pDACKDL, 16, aucDACKDL);
//		StringToHex((unsigned char*)pDEAKDL, 16, aucDEAKDL);
//		StringToHex((unsigned char*)pDAMKDL, 16, aucDAMKDL);
//		StringToHex((unsigned char*)biCardBasicInfo.sCardNewSn.c_str(), 32, aucCardSNHex);
//
//		unsigned char aucNewKeyHex[16] = { 0 }, aucNewKey[32] = { 0 };
//
//		switch (ktKeyType)
//		{
//		case KT_DCCK:
//		{
//			Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewKeyHex);
//			break;
//		}
//		case KT_DACK:
//		{
//			Encrypt(aucCardSNHex, 16, aucDACKDL, aucNewKeyHex);
//			break;
//		}
//		case KT_DEAK:
//		{
//			Encrypt(aucCardSNHex, 16, aucDEAKDL, aucNewKeyHex);
//			break;
//		}
//		case KT_DAMK:
//		{
//			Encrypt(aucCardSNHex, 16, aucDAMKDL, aucNewKeyHex);
//			break;
//		}
//		}
//
//		HexToChar(aucNewKeyHex, 16, aucNewKey);
//		std::string sKey((char*)aucNewKey, 32);
//		sNewKey = sKey;
//	}
//	
//
//	return sNewKey;
//}

//void CDLCardToolDlg::InstallKey(std::string sDir, std::string sNewKey, std::string sMainKey, KEY_TYPE ktKeyType, int iKeyID)
//{
//	int iErrorCode = 0;
//
//	iErrorCode = InstallDLKey((unsigned char*)sDir.c_str(), iKeyID, (unsigned char*)sNewKey.c_str(), ktKeyType, (unsigned char*)sMainKey.c_str());
//	if (iErrorCode)
//	{
//		std::stringstream ssErrorCode;
//		ssErrorCode << iErrorCode;
//		std::string sErrorCode = ssErrorCode.str();
//		throw std::exception(ssErrorCode.str().c_str());
//	}
//}

//void CDLCardToolDlg::WriteData(std::string sDir, int iFileID, int iOffest, int iLen, std::string sKey, std::string sData)
//{
//	unsigned char aucDir[2] = { 0 }, aucKey[16] = { 0 };
//
//	int iError = WriteDLCardData((unsigned char*)sDir.c_str(), iFileID, iOffest, iLen, (unsigned char*)sData.c_str(), (unsigned char*)sKey.c_str());
//	if (iError)
//	{
//
//		std::stringstream ssErrorCode;
//		ssErrorCode << iError;
//		throw std::exception(ssErrorCode.str().c_str());
//	}
//}



void CDLCardToolDlg::ResizeCtrl()
{
	const int kLeftStage = 26;
	const int kBmgLenth = 16;
	const int kUpStage = 24;

	CRect rtPanel;
	GetClientRect(&rtPanel);

	const int kExplainHeight = 20;
	const int kEcplainHeightBig = 25;
	const int kExplainWidth = 16;
	const int kExplainWidthBig = 20;

	CRect rtExplain;
	rtExplain.top = kUpStage;
	rtExplain.left = kLeftStage + kBmgLenth;
	rtExplain.right = rtPanel.right;
	rtExplain.bottom = kUpStage + kEcplainHeightBig;
	m_cExplain.MoveWindow(&rtExplain);

	CRect rtImg = rtExplain;
	rtImg.left = rtExplain.left - kEcplainHeightBig;
	rtImg.right = rtExplain.left;
	m_cStaticImg.MoveWindow(&rtImg);


	CRect rtExplain1;
	rtExplain1.top = rtExplain.bottom + 24;
	rtExplain1.left = rtExplain.left + 24;
	rtExplain1.bottom = rtExplain1.top + kEcplainHeightBig;
	rtExplain1.right = rtExplain1.left + kExplainWidthBig;
	m_cExplain1.MoveWindow(&rtExplain1);

	CRect rtExplain2 = rtExplain1;
	rtExplain2.left = rtExplain1.right;
	rtExplain2.right = rtExplain2.left + kExplainWidthBig * 9;
	m_cExplain2.MoveWindow(&rtExplain2);

	CRect rtExplain3 = rtExplain2;
	rtExplain3.left = rtExplain2.right;
	rtExplain3.right = rtExplain3.left + kExplainWidthBig * 2;
	m_cExplain3.MoveWindow(&rtExplain3);

	CRect rtExplain4 = rtExplain3;
	rtExplain4.left = rtExplain3.right;
	rtExplain4.right = rtExplain4.left + kExplainWidthBig * 7;
	m_cExplain4.MoveWindow(&rtExplain4);

	CRect rtExplain5 = rtExplain4;
	rtExplain5.left = rtExplain4.right;
	rtExplain5.right = rtExplain5.left + kExplainWidthBig * 7;
	m_cExplain5.MoveWindow(&rtExplain5);

	CRect rtExplain6 = rtExplain1;
	rtExplain6.top = rtExplain6.bottom + 24;
	rtExplain6.bottom = rtExplain6.top + kExplainHeight;
	rtExplain6.right = rtExplain6.left + kExplainWidth * 6;
	m_cExplain6.MoveWindow(&rtExplain6);

	CRect rtExplain7 = rtExplain6;
	rtExplain7.left = rtExplain6.right;
	rtExplain7.right = rtExplain7.left + kExplainWidth * 5;
	m_cExplain7.MoveWindow(&rtExplain7);

	CRect rtExplain8 = rtExplain7;
	rtExplain8.left = rtExplain7.right;
	rtExplain8.right = rtExplain8.left + kExplainWidth * 8;
	m_cExplain8.MoveWindow(&rtExplain8);

	CRect rtExplain9 = rtExplain6;
	rtExplain9.top = rtExplain9.bottom + 14;
	rtExplain9.bottom = rtExplain9.top + kExplainHeight;
	rtExplain9.right = rtExplain9.left + kExplainWidth * 6;
	m_cExplain9.MoveWindow(&rtExplain9);

	CRect rtExplain10 = rtExplain9;
	rtExplain10.left = rtExplain9.right;
	rtExplain10.right = rtExplain10.left + kExplainWidth * 5;
	m_cExplain10.MoveWindow(&rtExplain10);

	CRect rtExplain11 = rtExplain10;
	rtExplain11.left = rtExplain10.right;
	rtExplain11.right = rtExplain11.left + kExplainWidth * 10;
	m_cExplain11.MoveWindow(&rtExplain11);

	CRect rtStaticCardNum;
	rtStaticCardNum.top = rtExplain11.bottom + 24 + 30;
	rtStaticCardNum.bottom = rtStaticCardNum.top + 36;
	rtStaticCardNum.left = kLeftStage;
	rtStaticCardNum.right = rtStaticCardNum.left + kExplainWidth * 4;
	m_cStaticCardNum.MoveWindow(rtStaticCardNum);

	CRect rtEditCardNum = rtStaticCardNum;
	rtEditCardNum.left = rtEditCardNum.right + kExplainWidth * 2;
	rtEditCardNum.right = rtEditCardNum.left + 230;
	m_cStaticCardNumText.MoveWindow(&rtEditCardNum);

	CRect rtUpdateButton = rtEditCardNum;
	rtUpdateButton.left = rtUpdateButton.right + 24;
	rtUpdateButton.right = rtUpdateButton.left + 130;
	m_cUpdateButton.MoveWindow(&rtUpdateButton);

	CRect rtResult = rtStaticCardNum;
	rtResult.top = rtResult.bottom + 60;
	rtResult.bottom = rtResult.top + 36;
	rtResult.right = rtResult.left + kExplainWidth * 6;
	m_cResult.MoveWindow(&rtResult);

	CRect rtResultText = rtEditCardNum;
	rtResultText.top = rtEditCardNum.bottom + 60;
	rtResultText.bottom = rtResultText.top + kExplainHeight * 2;
	rtResultText.left = rtResult.right;
	m_cResultText.MoveWindow(&rtResultText);

	CRect rtResultText2 = rtResultText;
	rtResultText2.top = rtResultText2.bottom + 30;
	rtResultText2.bottom = rtResultText2.top + kExplainHeight * 2;
	m_cResultText2.MoveWindow(&rtResultText2); 
}

void CDLCardToolDlg::SetCtrlText()
{
	m_cExplain.SetWindowText(L"使用说明");
	m_cExplain1.SetWindowText(L"将");
	m_cExplain2.SetWindowText(L"卡放置在读卡器上。");
	m_cExplain3.SetWindowText(L"点击");
	m_cExplain4.SetWindowText(L"卡片升级按钮,");
	m_cExplain5.SetWindowText(L"查看升级结果。");
	m_cExplain6.SetWindowText(L"1)若提示“");
	m_cExplain7.SetWindowText(L"升级成功!");
	m_cExplain8.SetWindowText(L"”,则升级完成。");
	m_cExplain9.SetWindowText(L"2)若提示“");
	m_cExplain10.SetWindowText(L"升级失败!");
	m_cExplain11.SetWindowText(L"”,请查看失败原因。");

	m_cResult.SetWindowText(L"升级结果");

	m_cStaticCardNum.SetWindowText(L"卡   片:");
}

void CDLCardToolDlg::SetCtrlFont()
{
	const CString kFontName = L"微软雅黑";
	const int kFontHeightBig = 140;
	const int kFontHeightSmall = 120;

	m_cFontBig.CreatePointFont(kFontHeightBig, kFontName);
	m_cFontSmall.CreatePointFont(kFontHeightSmall, kFontName);

	m_cExplain.SetFont(&m_cFontBig, FALSE);
	m_cExplain1.SetFont(&m_cFontBig, FALSE);
	m_cExplain2.SetFont(&m_cFontBig, FALSE);
	m_cExplain3.SetFont(&m_cFontBig, FALSE);
	m_cExplain4.SetFont(&m_cFontBig, FALSE);
	m_cExplain5.SetFont(&m_cFontBig, FALSE);

	m_cExplain6.SetFont(&m_cFontSmall, FALSE);
	m_cExplain7.SetFont(&m_cFontSmall, FALSE);
	m_cExplain8.SetFont(&m_cFontSmall, FALSE);
	m_cExplain9.SetFont(&m_cFontSmall, FALSE);
	m_cExplain10.SetFont(&m_cFontSmall, FALSE);
	m_cExplain11.SetFont(&m_cFontSmall, FALSE);


	m_cStaticCardNum.SetFont(&m_cFontSmall, FALSE);
	m_cUpdateButton.SetFont(&m_cFontSmall, FALSE);
	m_cStaticCardNumText.SetFont(&m_cFontSmall, FALSE);

	m_cResult.SetFont(&m_cFontSmall, FALSE);
	m_cResultText.SetFont(&m_cFontSmall, FALSE);
	m_cResultText2.SetFont(&m_cFontSmall, FALSE);

	m_cPen.CreatePen(PS_DASH, 1, RGB(102, 102, 102));
}

void CDLCardToolDlg::SetCtrlImg()
{
	namespace fs = boost::filesystem;
	const std::wstring sError(L"files.png");
	fs::path curr_exec_path = fs::current_path();
	curr_exec_path += L"\\res\\";
	std::wstring sErrorPath = curr_exec_path.wstring() + sError;
	if (!fs::exists(sErrorPath))
		return;
	HRESULT hr = m_cImg.Load(sErrorPath.c_str());
	m_cStaticImg.ModifyStyle(0xF, SS_BITMAP | SS_CENTERIMAGE);
	m_cStaticImg.SetBitmap(HBITMAP(m_cImg));
}

void CDLCardToolDlg::UpdataBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);
	UINT state = lpDrawItemStruct->itemState;;
	CRect btn_rect, text_rc, offset_text_rect;
	btn_rect.CopyRect(&lpDrawItemStruct->rcItem);

	const int kOffset = 1;
	text_rc = btn_rect;
	text_rc.OffsetRect(-kOffset, -kOffset);
	offset_text_rect = text_rc;
	offset_text_rect.OffsetRect(kOffset, kOffset);

	// Retrieve the button's caption
	CString caption_str;
	GetWindowText(caption_str);

	//TODO: read from config file.
	const COLORREF kBtnRectEnable(RGB(17, 139, 222)), kBtnRectSelected(RGB(17, 139, 222)); 
	const COLORREF kBtnTextEnable(RGB(255, 255, 255)), kBtnTextSelected(RGB(255, 255, 255));


	if (state & ODS_SELECTED)
	{
		DrawFilledRect(dc, btn_rect, kBtnRectSelected);
		DrawButtonText(dc, offset_text_rect, caption_str, kBtnTextSelected);
	}
	else
	{
		DrawFilledRect(dc, btn_rect, kBtnRectEnable);
		DrawButtonText(dc, text_rc, caption_str, kBtnTextEnable);
	}
}

void CDLCardToolDlg::UpdataBtn::DrawFilledRect(CDC* dc, CRect rc, COLORREF color)
{
	CBrush solid_br;

	solid_br.CreateSolidBrush(color);
	dc->FillRect(rc, &solid_br);
}

void CDLCardToolDlg::UpdataBtn::DrawButtonText(CDC* dc, CRect rc, CString caption, COLORREF textcolor)
{
	const CSize text_size = dc->GetOutputTextExtent(caption);
	const int kVerticalModifiedValue = 1;
	const int kVerticalStartPos = (rc.Height() - text_size.cy) / 2 - kVerticalModifiedValue;
	const uint32_t kDrawStyles = DT_CENTER | DT_VCENTER | DT_SINGLELINE;

	dc->SetTextColor(textcolor);
	dc->SetBkMode(TRANSPARENT);

	const int kHorizontalModifiedValue = 3;
	CRect text_rect = rc;
	text_rect.DeflateRect(kHorizontalModifiedValue, 0, kHorizontalModifiedValue, 0);
	text_rect.top = kVerticalStartPos;
	text_rect.bottom = kVerticalStartPos + text_size.cy;

	dc->DrawText(caption, caption.GetLength(), text_rect, kDrawStyles);
}


void CDLCardToolDlg::SetResultTest(CString sTitle, CString sText)
{
	if (sTitle.Compare(kSuccessText) == 0 || sTitle.Compare(kFailedText) == 0)
	{
		m_bSetRed = true;
	}

	m_cResultText.SetWindowText(sTitle);
	m_cResultText2.SetWindowText(sText);
}