📄 timeandkeydlg.cpp
字号:
// TimeAndKeyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TimeAndKey.h"
#include "TimeAndKeyDlg.h"
#include "UsbDeviceCommand.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTimeAndKeyDlg dialog
CTimeAndKeyDlg::CTimeAndKeyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTimeAndKeyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTimeAndKeyDlg)
m_strwritedata = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTimeAndKeyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTimeAndKeyDlg)
DDX_Text(pDX, IDC_EDIT_DATA, m_strwritedata);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTimeAndKeyDlg, CDialog)
//{{AFX_MSG_MAP(CTimeAndKeyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
//}}AFX_MSG_MAP
ON_WM_DEVICECHANGE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTimeAndKeyDlg message handlers
BOOL CTimeAndKeyDlg::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
m_bitmap[0].LoadBitmap(IDB_BITMAP1);
m_bitmap[1].LoadBitmap(IDB_BITMAP2);
m_nboard=0;
m_readparam.testDlg=this;
m_readparam.bInThread=false;
m_writeparam.testDlg=this;
m_controlparam.testDlg=this;
m_devhandle=NULL;
m_readparam.hThread=NULL;
CheckBoard();
return TRUE; // return TRUE unless you set the focus to a control
}
void CTimeAndKeyDlg::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 CTimeAndKeyDlg::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 CTimeAndKeyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTimeAndKeyDlg::OnButtonModify()
{
// TODO: Add your control notification handler code here
char tmp[20];
UpdateData(true);
if(m_strwritedata.GetLength()>8)
{
MessageBox("所写数值溢出,最多只能写入8位。","错误",MB_OK|MB_ICONSTOP);
return;
}
if(m_strwritedata.GetLength()==0)
{
MessageBox("请填入数值。","错误",MB_OK|MB_ICONSTOP);
return;
}
tmp[8-m_strwritedata.GetLength()]=0;
strcat(&tmp[8-m_strwritedata.GetLength()],(LPCTSTR)m_strwritedata);
memset(tmp,'0',8-m_strwritedata.GetLength());
UpdateData(false);
if(strtodata((unsigned char*)tmp,m_writeparam.buffer,8)!=0)
{
MessageBox("所写数值格式不对,有可能不是16进制数。","错误",MB_OK|MB_ICONSTOP);
return;
}
AfxBeginThread(BulkWritePipe,&m_writeparam);
}
void CTimeAndKeyDlg::EnableButton(bool modify)
{
GetDlgItem(IDC_BUTTON_MODIFY)->EnableWindow(modify);
}
void CTimeAndKeyDlg::UpdateReadData()
{
unsigned char key=m_readparam.buffer[0];
unsigned char hour,minute,second;
second=m_readparam.buffer[1]-((m_readparam.buffer[1]&0xf0)>>4)*6;
minute=m_readparam.buffer[2]-((m_readparam.buffer[2]&0xf0)>>4)*6;
hour=m_readparam.buffer[3]-((m_readparam.buffer[3]&0xf0)>>4)*6;
for(int i=0;i<8;i++)
{
if(i==key)
((CStatic*)GetDlgItem(IDC_STATIC_1+key))->SetBitmap((HBITMAP)m_bitmap[1]);
else
((CStatic*)GetDlgItem(IDC_STATIC_1+i))->SetBitmap((HBITMAP)m_bitmap[0]);
}
CString strtmp;
strtmp.Format("%d : %d : %d",hour,minute,second);
if(hour<10)
strtmp.Insert(0,"0");
if(minute<10)
strtmp.Insert(5,"0");
if(second<10)
strtmp.Insert(10,"0");
((CStatic*)GetDlgItem(IDC_STATIC_TIME))->SetWindowText(strtmp);
}
int CTimeAndKeyDlg::CheckBoard()
{
m_controlparam.testDlg=this;
ControlIn((void*)&m_controlparam);
return 0;
}
//-----------------------------------------------------
//参数:
//str:要转换的字符串
//data:储存转换过来的数据串
//len:数据长度
//函数功能:字符串转换为数据串
//-----------------------------------------------------
int CTimeAndKeyDlg::strtodata(unsigned char *str, unsigned char *data,int len)
{
unsigned char cTmp=0;
int i=0;
for(int j=0;j<len;j++)
{
if(chartoint(str[i++],&cTmp))
return 1;
data[j]=cTmp;
}
return 0;
}
//-----------------------------------------------------
//参数:
//chr:要转换的字符
//cint:储存转换过来的数据
//函数功能:字符转换为数据
//-----------------------------------------------------
int CTimeAndKeyDlg::chartoint(unsigned char chr, unsigned char *cint)
{
unsigned char cTmp;
cTmp=chr-48;
if(cTmp>=0&&cTmp<=9)
{
*cint=cTmp;
return 0;
}
cTmp=chr-65;
if(cTmp>=0&&cTmp<=5)
{
*cint=(cTmp+10);
return 0;
}
cTmp=chr-97;
if(cTmp>=0&&cTmp<=5)
{
*cint=(cTmp+10);
return 0;
}
return 1;
}
BOOL CTimeAndKeyDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
Sleep(200);
CheckBoard();
return 0;
}
void CTimeAndKeyDlg::OnCancel()
{
// TODO: Add extra cleanup here
m_nboard=0;
WaitForSingleObject(m_readparam.hThread,500);
CDialog::OnCancel();
}
void CTimeAndKeyDlg::OnOK()
{
// TODO: Add extra validation here
m_nboard=0;
WaitForSingleObject(m_readparam.hThread,500);
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -