⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tokendlg.cpp

📁 自己写得串口多线程控制程序
💻 CPP
字号:
// TokenDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Token.h"
#include "TokenDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//#define RELEASE_TOKEN
#include "afxmt.h"

#include "TokenShadow.h"
CTokenShadow* m_TokenShadow;

HWND hBackPanelWnd;
HWND hDealWnd;
CString ComName="COM1";

int nCardPoped=0;
int nCardDealed=0;

CEvent  m_TokenShadowEvent(FALSE,TRUE,"m_TokenShadowEvent",NULL);

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTokenDlg dialog

CTokenDlg::CTokenDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTokenDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTokenDlg)
	m_Num = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_Num="";
	HighByte=0;
	LowByte=0;
	TmpComName="";

}

void CTokenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTokenDlg)
	DDX_Control(pDX, IDC_COMNUM, m_Com);
	DDX_Control(pDX, IDC_STATIC_POPTOKEN, m_PopTokenCounterShow);
	DDX_Control(pDX, IDC_NUM, m_NumCtl);
	DDX_Text(pDX, IDC_NUM, m_Num);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTokenDlg, CDialog)
	//{{AFX_MSG_MAP(CTokenDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_POPTOKEN, OnPoptoken)
	ON_BN_CLICKED(IDC_DROPTOKEN, OnDroptoken)
	ON_WM_CLOSE()
	ON_MESSAGE(WM_USERPOPTOKENCONTINUEFROMSHADOW,OnOnePop)
	ON_MESSAGE(WM_USERPOPTOKENOVERFROMSHADOW,OnDealOver)
	ON_EN_CHANGE(IDC_NUM, OnChangeNum)
	ON_MESSAGE(WM_COMCHANGED,OnComChanged)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_MESSAGE(THREADINIT, OnThreadInit)
	ON_LBN_SELCHANGE(IDC_COMNUM, OnSelchangeComnum)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTokenDlg message handlers

BOOL CTokenDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	hBackPanelWnd=this->m_hWnd;
	hDealWnd=this->m_hWnd;

	m_NumCtl.LimitText(4);
	//设置静态文本区文本样式
	HDC hDC=::GetDC(m_PopTokenCounterShow.m_hWnd);
	long Height=MulDiv (12, -::GetDeviceCaps(hDC, LOGPIXELSY), 72);
	::ReleaseDC(m_PopTokenCounterShow.m_hWnd, hDC);

	sLogFont.lfHeight=Height;
	sLogFont.lfWidth=0;
	sLogFont.lfEscapement=0;
	sLogFont.lfOrientation=0;
	sLogFont.lfWeight=FW_HEAVY;
	sLogFont.lfItalic=0;
	sLogFont.lfUnderline=0;
	sLogFont.lfStrikeOut=0;
	sLogFont.lfCharSet=DEFAULT_CHARSET; //ANSI_CHARSET;
	sLogFont.lfOutPrecision=OUT_DEFAULT_PRECIS;
	sLogFont.lfClipPrecision=CLIP_DEFAULT_PRECIS;
	sLogFont.lfQuality=PROOF_QUALITY;
	sLogFont.lfPitchAndFamily=VARIABLE_PITCH | FF_ROMAN;

	m_PopTokenCounterShow.SetWindowText("hello!");

	m_Com.AddString("COM1");
	m_Com.AddString("COM2");
	m_Com.AddString("COM3");
	m_Com.AddString("COM4");
	m_Com.AddString("COM5");
	m_Com.AddString("COM6");
	
	m_TokenShadow=(CTokenShadow*)AfxBeginThread(RUNTIME_CLASS(CTokenShadow),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	m_TokenShadow->m_bAutoDelete=FALSE;
	m_TokenShadow->ResumeThread();	

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTokenDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTokenDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTokenDlg::OnPoptoken() 
{
	// TODO: Add your control notification handler code here
	::nCardPoped=0;
	::nCardDealed=0;
	if(m_TokenShadow!=NULL&&m_TokenShadow->pTokenHandle!=NULL)
	{
		if(!m_TokenShadow->pTokenHandle->ToTokenPopCard(LowByte,HighByte))
		{
			TRACE("\n发送出票指令失败!");
		}
		else
		{
			TRACE("\n发送出票指令成功!");
#ifdef RELEASE_TOKEN
			m_TokenShadow->bTokenWorking=TRUE;
#endif
		}
	}
	else
	{
		m_PopTokenCounterShow.SetWindowText("Open Serial Error!");
	}
}

void CTokenDlg::OnDroptoken() 
{
	// TODO: Add your control notification handler code here
	::nCardPoped=0;
	::nCardDealed=0;
	if(m_TokenShadow!=NULL&&m_TokenShadow->pTokenHandle!=NULL)
	{
		if(!m_TokenShadow->pTokenHandle->ToTokenCardStatus(TRUE))
		{
			TRACE("\n发送倒票命令失败!");
		}
		else
		{
			TRACE("\n发送倒票命令成功!");
#ifdef RELEASE_TOKEN
			m_TokenShadow->bTokenWorking=TRUE;
#endif
		}
	}
	else
	{
		m_PopTokenCounterShow.SetWindowText("Open Serial Error!");
	}
}

	void CTokenDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if(m_TokenShadow!=NULL)
	{
		m_TokenShadow->PostThreadMessage(THREADOVER,(WPARAM)2,(LPARAM)3);
		WaitForSingleObject(m_TokenShadow->m_hThread,INFINITE);
		delete  m_TokenShadow;
		m_TokenShadow=NULL;	
	}
	CDialog::OnClose();
}

void CTokenDlg::OnOnePop(WPARAM wparam, LPARAM lparam)
{
	CString str;
	str.Format("now the number of token poped is %2d",::nCardPoped);
	m_PopTokenCounterShow.SetWindowText(str);
}

void CTokenDlg::OnDealOver(WPARAM wparam, LPARAM lparam)
{
#ifdef RELEASE_TOKEN
	m_TokenShadow->bTokenWorking=FALSE;
#endif
	BOOL bSuccess=(BOOL)wparam;
	CString str;
	if(bSuccess)
	{
		//str.Format("good!\ncounter:%d\nnumber:%d",::nCardPoped,::nCardDealed);
		str.Format("Good!  %d",::nCardPoped);
		MessageBox(str,"result",MB_OK);
	}
	else
	{
		str.Format("Good!  %d",::nCardPoped);
		//str.Format("bad!\ncounter:%d\nnumber:%d",::nCardPoped,::nCardDealed);
		MessageBox(str,"result",MB_OK);
	}
}

void CTokenDlg::OnChangeNum() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData();
	WORD num=atoi(m_Num);
	TRACE("\nnum:%x",num);
	LowByte=num&0xff;
	num=num>>8;
	HighByte=num&0xff;
	TRACE("\nLowByte:%x,HighByte:%x",LowByte,HighByte);
}

void CTokenDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	SendMessage(WM_CLOSE,(WPARAM)0,(LPARAM)0);
	CDialog::OnCancel();
}

void CTokenDlg::OnComChanged(WPARAM wparam,LPARAM lparam)
{
	if(TmpComName==ComName)
		return;

	m_Com.EnableWindow(FALSE);

	ComName=TmpComName;

	if(m_TokenShadow!=NULL)
	{
		m_TokenShadow->PostThreadMessage(THREADOVER,(WPARAM)2,(LPARAM)3);
		WaitForSingleObject(m_TokenShadow->m_hThread,INFINITE);
		delete  m_TokenShadow;
		m_TokenShadow=NULL;	
	}

	m_TokenShadow=(CTokenShadow*)AfxBeginThread(RUNTIME_CLASS(CTokenShadow),THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	m_TokenShadow->m_bAutoDelete=FALSE;
	m_TokenShadow->ResumeThread();	
}


void CTokenDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	m_PopTokenCounterShow.SetWindowText("");
	::nCardPoped=0;
}

void CTokenDlg::OnThreadInit(WPARAM wparam,LPARAM lparam)
{
	int ThreadNum=(int)wparam;
	switch(ThreadNum)
	{
	case 0:
		if(m_TokenShadow->pTokenHandle==NULL)
		{
			m_TokenShadow->PostThreadMessage(THREADOVER,(WPARAM)2,(LPARAM)3);
			WaitForSingleObject(m_TokenShadow->m_hThread,INFINITE);
			delete  m_TokenShadow;
			m_TokenShadow=NULL;	
		}
		TRACE("\nm_TokenShadowEvent.SetEvent");
		break;
	default:
		break;
	}

	m_Com.EnableWindow(TRUE);
	m_Com.SetFocus();

	return;
}



void CTokenDlg::OnSelchangeComnum() 
{
	int Index=m_Com.GetCurSel();
	switch(Index)
	{
	case 0:
		TmpComName="COM1";
		break;
	case 1:
		TmpComName="COM2";
		break;
	case 2:
		TmpComName="COM3";
		break;
	case 3:
		TmpComName="COM4";
		break;
	case 4:
		TmpComName="COM5";
		break;
	case 5:
		TmpComName="COM6";
		break;
	default:
		break;
	}
	this->SendMessage(WM_COMCHANGED,(WPARAM)0,(LPARAM)0);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -