📄 regprnmaindlg.cpp
字号:
// RegPrnMainDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BprConfig.h"
#include "RegPrnDlg.h"
#include "RegPrnMainDlg.h"
#include "SearchProgDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// definition of my global constants & variables
BT_PRN_TABLE cur_prn_table[MAX_PRINTER];
BT_PRN_TABLE new_prn_table[MAX_PRINTER];
UINT cur_prn_count;
UINT new_prn_count;
BOOLEAN printing;
unsigned char cur_printer_id;
unsigned char flash_update;
static CSearchProgDlg SearchProgDlg;
static BOOLEAN init_prn_info;
static void PORT_EventHandler(unsigned char event, PORT_EVENT_PARA *event_para);
static void update_printer_buttons(void);
static BOOLEAN check_tx_data(void);
static int convert_to_hex(unsigned char *buf, UINT data_size);
static int __atoi(unsigned char *str, int base);
/////////////////////////////////////////////////////////////////////////////
// CRegPrnMainDlg dialog
CRegPrnMainDlg::CRegPrnMainDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRegPrnMainDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRegPrnMainDlg)
//}}AFX_DATA_INIT
}
void CRegPrnMainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRegPrnMainDlg)
DDX_Control(pDX, IDC_RADIO_TEXT, m_radio_text);
DDX_Control(pDX, IDC_RADIO_HEX, m_radio_hex);
DDX_Control(pDX, IDC_TX_DATA_TEXT, m_tx_data_text);
DDX_Control(pDX, IDC_RX_DATA, m_rx_data);
DDX_Control(pDX, IDC_TX_DATA_HEX, m_tx_data_hex);
DDX_Control(pDX, ID_SEARCH, m_search);
DDX_Control(pDX, IDPRN7, m_idprn7);
DDX_Control(pDX, IDPRN6, m_idprn6);
DDX_Control(pDX, IDPRN5, m_idprn5);
DDX_Control(pDX, IDPRN4, m_idprn4);
DDX_Control(pDX, IDPRN3, m_idprn3);
DDX_Control(pDX, IDPRN2, m_idprn2);
DDX_Control(pDX, IDPRN1, m_idprn1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRegPrnMainDlg, CDialog)
//{{AFX_MSG_MAP(CRegPrnMainDlg)
ON_BN_CLICKED(ID_READ_PRNINFO, OnReadPrninfo)
ON_BN_CLICKED(ID_SEARCH, OnSearch)
ON_BN_CLICKED(IDPRN1, OnPrn1)
ON_BN_CLICKED(IDPRN2, OnPrn2)
ON_BN_CLICKED(IDPRN3, OnPrn3)
ON_BN_CLICKED(IDPRN4, OnPrn4)
ON_BN_CLICKED(IDPRN5, OnPrn5)
ON_BN_CLICKED(IDPRN6, OnPrn6)
ON_BN_CLICKED(IDPRN7, OnPrn7)
ON_BN_CLICKED(IDC_RADIO_HEX, OnRadioHex)
ON_BN_CLICKED(IDC_RADIO_TEXT, OnRadioText)
ON_BN_CLICKED(ID_CLEAR, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRegPrnMainDlg message handlers
BOOL CRegPrnMainDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
// initialize variables
memset((void *)cur_prn_table, 0, sizeof(cur_prn_table));
cur_prn_count = 0;
printing = FALSE;
// register PORT event handler
PORT_RegisterEventHandler((long)PORT_EventHandler, 0);
// read initial printer table
init_prn_info = TRUE;
PORT_ReadPrnInfo();
// initialize dialog
m_radio_text.SetCheck(1);
m_tx_data_text.EnableWindow(TRUE);
m_radio_hex.SetCheck(0);
m_tx_data_hex.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CRegPrnMainDlg::OnReadPrninfo()
{
// TODO: Add your control notification handler code here
init_prn_info = FALSE;
PORT_ReadPrnInfo();
}
void CRegPrnMainDlg::OnSearch()
{
// TODO: Add your control notification handler code here
PORT_Clear();
PORT_DiscoveryRequest(MAX_PRINTER);
int nResponse = SearchProgDlg.DoModal();
if (nResponse == IDOK) {
if (new_prn_count > 0)
PostMessage(WM_REGPRN, 1, 0);
else
MessageBox("No Bluetooth printers have been found.",
"Searching for Nearby Bluetooth Printers...",
MB_ICONINFORMATION | MB_OK);
}
else if (nResponse == IDCANCEL) {
MessageBox("No search responses from the Adapter.",
"Searching for Nearby Bluetooth Printers...",
MB_ICONEXCLAMATION | MB_OK);
}
}
void CRegPrnMainDlg::OnPrn1()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 1;
PORT_ConnectRequest(1, NULL);
}
}
void CRegPrnMainDlg::OnPrn2()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 2;
PORT_ConnectRequest(2, NULL);
}
}
void CRegPrnMainDlg::OnPrn3()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 3;
PORT_ConnectRequest(3, NULL);
}
}
void CRegPrnMainDlg::OnPrn4()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 4;
PORT_ConnectRequest(4, NULL);
}
}
void CRegPrnMainDlg::OnPrn5()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 5;
PORT_ConnectRequest(5, NULL);
}
}
void CRegPrnMainDlg::OnPrn6()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 6;
PORT_ConnectRequest(6, NULL);
}
}
void CRegPrnMainDlg::OnPrn7()
{
// TODO: Add your control notification handler code here
if (!printing && check_tx_data()) {
printing = TRUE;
cur_printer_id = 7;
PORT_ConnectRequest(7, NULL);
}
}
void CRegPrnMainDlg::OnRadioText()
{
// TODO: Add your control notification handler code here
m_radio_hex.SetCheck(0);
m_tx_data_text.EnableWindow(TRUE);
m_tx_data_hex.EnableWindow(FALSE);
}
void CRegPrnMainDlg::OnRadioHex()
{
// TODO: Add your control notification handler code here
m_radio_text.SetCheck(0);
m_tx_data_text.EnableWindow(FALSE);
m_tx_data_hex.EnableWindow(TRUE);
}
void CRegPrnMainDlg::OnClear()
{
// TODO: Add your control notification handler code here
theApp.pRegPrnMainDlg->m_rx_data.SetWindowText("");
}
static void
PORT_EventHandler(unsigned char event, PORT_EVENT_PARA *event_para)
{
static unsigned char buf[4096];
UINT i, data_size;
switch (event) {
case CONNECT_RESULT:
if (event_para->ConnectResult.PrinterID == cur_printer_id &&
event_para->ConnectResult.Result) {
// print now
if (theApp.pRegPrnMainDlg->m_radio_text.GetCheck()) {
data_size = theApp.pRegPrnMainDlg->m_tx_data_text.GetWindowText((char *)buf, 4096);
}
else {
data_size = theApp.pRegPrnMainDlg->m_tx_data_hex.GetWindowText((char *)buf, 4096);
data_size = (UINT)convert_to_hex(buf, data_size);
}
if (data_size > 0) PORT_Write(buf, data_size);
// disconnect
PORT_DisconnectRequest(cur_printer_id, NULL);
}
else {
printing = FALSE;
}
break;
case DISCONNECT_RESULT:
if (event_para->DisconnectResult.PrinterID == cur_printer_id &&
event_para->DisconnectResult.Result) {
printing = FALSE;
}
break;
case DISCOVERY_RESULT:
memset((void *)new_prn_table, 0, sizeof(new_prn_table));
new_prn_count = event_para->DiscoveryResult.NumDiscovery;
for (i = 0; i < new_prn_count; i++) {
new_prn_table[i].ID = i + 1;
memcpy(new_prn_table[i].BD_Addr,
event_para->DiscoveryResult.BD_Addr[i], 6);
memcpy(new_prn_table[i].DeviceName,
event_para->DiscoveryResult.DeviceName[i], 16);
memcpy(new_prn_table[i].DeviceLocation,
event_para->DiscoveryResult.DeviceLocation[i], 13);
}
SearchProgDlg.PostMessage(WM_CLOSE, 0, 0);
break;
case REPORT_STATUS:
break;
case READ_PRNINFO_RESULT:
memset((void *)new_prn_table, 0, sizeof(new_prn_table));
new_prn_count = event_para->ReadPrnInfoResult.NumPrinters;
for (i = 0; i < new_prn_count; i++) {
new_prn_table[i].ID = event_para->ReadPrnInfoResult.PrinterTable[i].ID;
memcpy(new_prn_table[i].BD_Addr,
event_para->ReadPrnInfoResult.PrinterTable[i].BD_Addr, 6);
memcpy(new_prn_table[i].DeviceName,
event_para->ReadPrnInfoResult.PrinterTable[i].DeviceName, 16);
memcpy(new_prn_table[i].DeviceLocation,
event_para->ReadPrnInfoResult.PrinterTable[i].DeviceLocation, 13);
}
if (init_prn_info)
update_printer_buttons();
else
::PostMessage(theApp.pRegPrnMainDlg->m_hWnd, WM_REGPRN, 0, 0);
break;
case WRITE_PRNINFO_RESULT:
if (event_para->WriteConfigResult.Result) {
memcpy((void *)cur_prn_table, (void *)new_prn_table, sizeof(cur_prn_table));
cur_prn_count = new_prn_count;
SetEvent(hEvent);
}
break;
case APP_DATA:
buf[0] = 0;
for (i = 0; i < event_para->AppData.DataSize && i < 1024; i++) {
sprintf((char *)buf+strlen((char *)buf), "%02X ",
event_para->AppData.Buffer[i]);
}
theApp.pRegPrnMainDlg->m_rx_data.SetWindowText((char *)buf);
break;
default:
// just ignore other events
break;
}
}
LRESULT CRegPrnMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
CRegPrnDlg dlg;
// TODO: Add your specialized code here and/or call the base class
switch (message) {
case WM_REGPRN:
flash_update = wParam;
int nResponse = dlg.DoModal();
if (nResponse == IDOK) {
if (new_prn_count == cur_prn_count &&
!memcmp((void *)new_prn_table, (void *)cur_prn_table, sizeof(new_prn_table))) {
// nothing has been changed; just break
break;
}
PORT_WritePrnInfo(new_prn_count, new_prn_table, flash_update);
if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) {
MessageBox("Can't update the printer table in the Adapter.",
"Error", MB_ICONERROR | MB_OK);
break;
}
update_printer_buttons();
}
break;
}
return CDialog::WindowProc(message, wParam, lParam);
}
static void
update_printer_buttons(void)
{
int i, flag[MAX_PRINTER] = {-1, -1, -1, -1, -1, -1, -1};
char prn_button[64];
for (i = 0; i < MAX_PRINTER; i++) {
if (new_prn_table[i].ID > 0) {
flag[new_prn_table[i].ID - 1] = i;
}
}
for (i = 0; i < MAX_PRINTER; i++) {
if (flag[i] >= 0) {
sprintf(prn_button, " Printer %d (%s)", i+1,
new_prn_table[flag[i]].DeviceLocation);
theApp.pRegPrnMainDlg->GetDlgItem(IDPRN1 + i)->SetWindowText(prn_button);
theApp.pRegPrnMainDlg->GetDlgItem(IDPRN1 + i)->EnableWindow(TRUE);
}
else {
sprintf(prn_button, " Printer %d", i+1);
theApp.pRegPrnMainDlg->GetDlgItem(IDPRN1 + i)->SetWindowText(prn_button);
theApp.pRegPrnMainDlg->GetDlgItem(IDPRN1 + i)->EnableWindow(FALSE);
}
}
}
static BOOLEAN
check_tx_data(void)
{
unsigned char buf[512];
UINT data_size;
if (theApp.pRegPrnMainDlg->m_radio_hex.GetCheck()) {
data_size = theApp.pRegPrnMainDlg->m_tx_data_hex.GetWindowText((char *)buf, 512);
if (convert_to_hex(buf, data_size) < 0) {
MessageBox(NULL, "Invalid hexadecimal numbers.", "Error",
MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
}
return TRUE;
}
static int
convert_to_hex(unsigned char *buf, UINT data_size)
{
UINT i = 0, j;
int k = 0, result;
unsigned char tmp[256];
while (i < data_size) {
while ((buf[i] == ' ' || buf[i] == '\t') && i < data_size) i++;
for (j = 0; buf[i] != ' ' && buf[i] != '\t' && i < data_size; i++, j++) {
tmp[j] = buf[i];
}
if (j > 0) {
tmp[j] = '\0';
result = __atoi(tmp, 16);
if (result < 0) return(-1);
buf[k++] = (unsigned char)result;
}
}
return(k);
}
static int
__atoi(unsigned char *str, int base)
{
int result = 0, i = 0, curr_value;
if (str == NULL) return(-1);
/* check the base */
if (str[i] == '0' && (str[i+1] == 'x' || str[i+1] == 'X')) {
base = 16;
i += 2;
}
while (str[i] != '\0') {
if (str[i] >= '0' && str[i] <= '9') {
curr_value = str[i] - '0';
}
else if (base == 16 && str[i] >= 'a' && str[i] <= 'f') {
curr_value = str[i] - 'a' + 10;
}
else if (base == 16 && str[i] >= 'A' && str[i] <= 'F') {
curr_value = str[i] - 'A' + 10;
}
else {
/* invalid char */
return(-1);
}
result = base * result + curr_value;
if (result > 0xFF) return(-1);
i++;
}
return(result);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -