初探 MFC

这几天要写智能卡大作业,老师要求必须有图形界面。 作业本身没什么难的,调用提供的API函数完成相应功能。倒是图形界面让我觉得比较有趣,以前一直用java写图形界面。但是java程序运行起来比较麻烦,首先得有jre,其次jar也不一定能双击运行,当时就萌生了写个MFC程序的念头,囿于时间有限一直没学习。这次倒是给了我一个大好机会。

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
#pragma once
#include <iostream>
#include <Windows.h>

class CardManager {

public:
	enum DATA_T {
		PUBLISHER,
		CARD_ID,
		USER_NAME,
	};

private:
	const short PUBLISHER_ADDR = 0x30;
	const short PUBLISHER_LEN = 0x10;
	const short CARD_ID_ADDR =
		PUBLISHER_ADDR + PUBLISHER_LEN;
	const short CARD_ID_LEN = 0x10;
	const short USER_NAME_ADDR =
		CARD_ID_ADDR + CARD_ID_LEN;
	const short USER_NAME_LEN = 0x10;
	const short BALANCE_ADDR =
		USER_NAME_ADDR + USER_NAME_LEN;
	const short BALANCE_LEN = 0x04; //sizeof(float)
	const short BALANCE_BUF_ADDR =
		BALANCE_ADDR + BALANCE_LEN;
	const short BALANCE_BUF_LEN = BALANCE_LEN;

	HANDLE m_icdev;
	bool m_passwdChecked;
	void(*noPasswdFunc)(void);
	void(*notInitFunc)(void);

	inline bool passWordConfirm()
	{
		if (noPasswdFunc && !m_passwdChecked) {
			noPasswdFunc();
		}
		return m_passwdChecked;
	}

	inline bool initConfirm()
	{
		if (notInitFunc && !m_icdev) {
			notInitFunc();
		}
		return m_icdev != 0;
	}
	bool writeBalance(float num);
	bool writeInfo(DATA_T info_type,
				   const std::string& data);
	float getBuffer();
	bool writeBuffer(float balance);

	static unsigned char *float2bytes(float num);
	static float bytes2float(unsigned char* bytes);
public:
	CardManager();
	virtual ~CardManager();

	inline bool isInited()
	{
		return m_icdev != 0;
	}

	inline void setNoPasswdCallBack(void(*callBack)(void))
	{
		noPasswdFunc = callBack;
	}

	inline void setNotInitCallBack(void(*callback)(void))
	{
		notInitFunc = callback;
	}

	/*
	return 0 for ok
	1 for init device error
	2 for no card
	*/
	int init(int port,  long baud);

	bool disConnect();
	int getInfo(DATA_T data_type,  std::string& out);
	bool checkPassWd(const unsigned char* key);

	/*
	return 0 for ok
	*/
	int release(float initialBalance);

	/*
	return 0 for ok
	*/
	int consume(float costs);

	float getBalance();
	int charge(float amount);
};
分享到: 更多

Comments

Copyright © 2017 - LanderlYoung - Powered by Octopress

Recent Posts

Categories

Tags

>