📄 serialdlg.cpp
字号:
// SerialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Serial.h"
#include "SerialDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CSerialDlg dialog
CSerialDlg::CSerialDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSerialDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSerialDlg)
m_uBits = 0;
m_uPath = 0;
m_uValue = 0;
m_strValueHex = _T("");
m_strStatusHex = _T("");
m_strOut = _T("");
m_uPul = 1000;
m_strPeriod = _T("");
m_nPLength = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//m_cbits = 0;
m_ucValue = 0;
m_uTimer = 0;
m_uMesneData = 0;
m_uOut = 0;
m_strOut = _T("");
m_bIsRunning = FALSE;
m_bIsPause = FALSE;
m_bPCorrect = FALSE;
m_bVCorrect = FALSE;
m_bBCorrect = FALSE;
}
void CSerialDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSerialDlg)
DDX_Text(pDX, IDC_EDITBITS, m_uBits);
DDX_Text(pDX, IDC_EDITPATH, m_uPath);
DDX_Text(pDX, IDC_EDITVALUE, m_uValue);
DDX_Text(pDX, IDC_EDIT_VALUEHEX, m_strValueHex);
DDX_Text(pDX, IDC_EDIT_STATUSHEX, m_strStatusHex);
DDX_Text(pDX, IDC_EDIT_OUT, m_strOut);
DDX_Text(pDX, IDC_EDIT_PUL, m_uPul);
DDX_Text(pDX, IDC_EDIT_PERIOD, m_strPeriod);
DDX_Text(pDX, IDC_EDIT_PLENGTH, m_nPLength);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
//{{AFX_MSG_MAP(CSerialDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_EXEC, OnExec)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_BN_CLICKED(IDC_PAUSE, OnPause)
ON_EN_CHANGE(IDC_EDITBITS, OnChangeEditbits)
ON_EN_CHANGE(IDC_EDITVALUE, OnChangeEditvalue)
ON_EN_CHANGE(IDC_EDITPATH, OnChangeEditpath)
ON_EN_SETFOCUS(IDC_EDITBITS, OnSetfocusEditbits)
ON_EN_SETFOCUS(IDC_EDITVALUE, OnSetfocusEditvalue)
ON_EN_SETFOCUS(IDC_EDITPATH, OnSetfocusEditpath)
ON_EN_SETFOCUS(IDC_EDIT_PUL, OnSetfocusEditPul)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSerialDlg message handlers
BOOL CSerialDlg::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
((CWnd *)GetDlgItem(IDC_EXEC))->EnableWindow(FALSE);
((CWnd *)GetDlgItem(IDC_PAUSE))->EnableWindow(FALSE);
((CWnd *)GetDlgItem(IDC_STOP))->EnableWindow(FALSE);
((CWnd *)GetDlgItem(IDC_STATIC_STATUS_RECT))->GetWindowRect(&m_statusRct);
ScreenToClient(&m_statusRct);
((CWnd *)GetDlgItem(IDC_STATIC_VALUE_RECT))->GetWindowRect(&m_valueRct);
ScreenToClient(&m_valueRct);
CRect rct;
((CWnd *)GetDlgItem(IDC_STATICBIN))->GetWindowRect(&rct);
m_MRGValueOrgPt.x = rct.left + rct.Width() + 10;
m_MRGValueOrgPt.y = rct.top;
ScreenToClient(&m_MRGValueOrgPt);
((CWnd *)GetDlgItem(IDC_STATICSTATUS))->GetWindowRect(&rct);
m_MRGStatusOrgPt.x = rct.left + 20;
m_MRGStatusOrgPt.y = rct.bottom + 2;
ScreenToClient(&m_MRGStatusOrgPt);
((CWnd *)GetDlgItem(IDC_STATICOUT))->GetWindowRect(&rct);
m_MRGOutOrgPt.x = rct.right + 2;
m_MRGOutOrgPt.y = rct.bottom + 16;
ScreenToClient(&m_MRGOutOrgPt);
m_gValue.EnableClick(TRUE);
m_gStatus.EnableClick(FALSE);
m_gOut.EnableClick(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSerialDlg::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 CSerialDlg::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();
}
UpdateData(FALSE);
m_strValueHex.Format("0x%.3X",m_uValue);
m_strStatusHex.Format("0x%.2X",m_uMesneData);
CClientDC dc(this);
CRect rct;
if(m_uBits != 0)
{
// draw the "value" table
m_gValue.DrawGrid(dc,m_MRGValueOrgPt,14,24,1,m_uBits);
m_gValue.SetMarginText(dc);
m_gValue.FillGridColor(dc,RGB(0,255,0),RGB(200,200,200),-1,-1,NULL);
m_gValue.SetGridsText(dc,1,m_uBits,RGB(255,0,0),RGB(0,0,255));
}
if(m_bIsRunning || m_bIsPause)
{
//draw the "status" table
m_gStatus.DrawGrid(dc,m_MRGStatusOrgPt,14,30,1,m_uBits);
m_gStatus.SetMarginText(dc);
//m_gOut.SetSelectBoxByUInt(0,temp);
SetGridsByUInt(&m_gStatus,0,0,m_uMesneData);
m_gStatus.FillGridColor(dc,RGB(0,255,0),RGB(200,200,200),-1,-1,NULL);
m_gStatus.GetSelectRect(0,m_uPath,&rct);
m_gStatus.FillGridColor(dc,RGB(255,0,0),RGB(150,150,255),0,m_uPath,&rct);
m_gStatus.SetGridsText(dc,1,m_uBits,RGB(255,0,0),RGB(0,0,255));
m_gStatus.SetGridText(dc,0,m_uPath,RGB(0,255,0),RGB(0,0,255));
if(m_uMesneData == m_uValue)
{
m_gStatus.FillGridColor(dc,RGB(255,0,0),RGB(150,150,255),-1,-1,NULL);
m_gStatus.SetGridsText(dc,1,m_uBits,RGB(0,255,0),RGB(0,0,255));
}
// draw the "output" table
m_gOut.DrawGrid(dc,m_MRGOutOrgPt,0,30,1,1);
SetGridsByUInt(&m_gOut,0,0,m_uMesneData);
m_gOut.FillGridColor(dc,RGB(0,255,0),RGB(200,200,200),-1,-1,NULL);
m_gOut.SetGridsText(dc,1,1,RGB(255,0,0),RGB(0,0,255));
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSerialDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSerialDlg::OnExec()
{
// TODO: Add your control notification handler code here
CString str(_T(""));
double max_value = 0;
if(m_bIsPause && !m_bIsRunning)
{
m_uTimer = SetTimer(1,m_uPul,NULL);
((CWnd *)GetDlgItem(IDC_EXEC))->EnableWindow(FALSE);
((CWnd *)GetDlgItem(IDC_PAUSE))->EnableWindow(TRUE);
((CWnd *)GetDlgItem(IDC_STOP))->EnableWindow(TRUE);
return ;
}
m_bIsRunning = TRUE;
m_bIsPause = FALSE;
UpdateData();
str.LoadString(IDS_STRING_BITSINFO);
SetDlgItemText(IDC_STATICINFO,str);
if(m_uBits < 3 || m_uBits > 9)
{
m_bBCorrect = FALSE;
((CWnd *)GetDlgItem(IDC_EXEC))->EnableWindow(FALSE);
str.Format(_T(" 不合法的位数输入!请输入一个在“3”到“9”之间的数值,"));
SetDlgItemText(IDC_STATICINFO,str);
m_uBits = 0;
UpdateData(FALSE);
//((CWnd *)GetDlgItem(IDC_EDITBITS))->SetFocus();
InvalidateRect(&m_valueRct);
return ;
}
str.LoadString(IDS_STRING_VALUEINFO);
SetDlgItemText(IDC_STATICINFO,str);
max_value = ldexp(1.0,m_uBits) - 1;
//CClientDC dc((CWnd *)GetDlgItem(IDC_STATICINFO));
if(m_uValue <= 0 || (double)m_uValue > max_value)
{
m_bVCorrect = FALSE;
((CWnd *)GetDlgItem(IDC_EXEC))->EnableWindow(FALSE);
str.Format(_T(" 不合法的初值输入!请输入一个在“1”到“%d”之间正确的数值,")\
_T("或者用下面的方法输入一个二进制式的值!"),(int)max_value);
SetDlgItemText(IDC_STATICINFO,str);
m_uValue = 0;
UpdateData(FALSE);
return ;
}
str.LoadString(IDS_STRING_PATHINFO);
SetDlgItemText(IDC_STATICINFO,str);
if(m_uPath < 1 || m_uPath >= m_uBits)
{
m_bPCorrect = FALSE;
((CWnd *)GetDlgItem(IDC_EXEC))->EnableWindow(FALSE);
str.Format(_T(" 不合法的反馈路径输入!请输入一个在“1”到“%d”之间的反馈路径"),\
(int)m_uBits-1);
SetDlgItemText(IDC_STATICINFO,str);
m_uPath = 0;
UpdateData(FALSE);
return ;
}
str.LoadString(IDS_STRING_RUNNING);
SetDlgItemText(IDC_STATICINFO,str);
//m_cbits = m_uBits;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -