?? cmdbatview.cpp
字號:
// CmdBatView.cpp : implementation file
//
#include "stdafx.h"
#include "VCStyle.h"
#include "CmdBatView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include<Mmsystem.h>
#include "data.h"
#include "sja\\sjaexport.h"
/////////////////////////////////////////////////////////////////////////////
// CCmdBatView
IMPLEMENT_DYNCREATE(CCmdBatView, CFormView)
CCmdBatView::CCmdBatView()
: CFormView(CCmdBatView::IDD)
{
//{{AFX_DATA_INIT(CCmdBatView)
m_LoopCount = 0;
m_sCmdBat = _T("");
m_sNote = _T("");
//}}AFX_DATA_INIT
m_TimerID = 0;
m_TimerRes = 0;
}
CCmdBatView::~CCmdBatView()
{
}
void CCmdBatView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCmdBatView)
DDX_Control(pDX, IDVALID, m_btnvalid);
DDX_Control(pDX, IDINVALID, m_btninvalid);
DDX_Control(pDX, IDC_BUTTON_SAVE, m_btnsave);
DDX_Control(pDX, IDC_BUTTON_LOAD, m_btnload);
DDX_Control(pDX, IDC_BUTTON_HELP, m_btnhelp);
DDX_Text(pDX, IDC_EDIT_LOOPCOUNT, m_LoopCount);
DDX_Text(pDX, IDC_EDIT_CMDBAT, m_sCmdBat);
DDX_Text(pDX, IDC_EDIT_NOTE, m_sNote);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCmdBatView, CFormView)
//{{AFX_MSG_MAP(CCmdBatView)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
ON_BN_CLICKED(IDVALID, OnValid)
ON_BN_CLICKED(IDINVALID, OnInvalid)
ON_MESSAGE(WM_RECEIVEFRAME,OnReceiveFrame)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_LOAD, OnButtonLoad)
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCmdBatView diagnostics
#ifdef _DEBUG
void CCmdBatView::AssertValid() const
{
CFormView::AssertValid();
}
void CCmdBatView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CCmdBatDoc *CCmdBatView::GetDocument () // non-debug version is inline
{
ASSERT (m_pDocument->IsKindOf (RUNTIME_CLASS (CCmdBatDoc)));
return (CCmdBatDoc *) m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCmdBatView message handlers
HBRUSH CCmdBatView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor==CTLCOLOR_EDIT)
pDC->SetBkColor(RGB(255,250,255));
// TODO: Return a different brush if the default is not desired
return m_bkbrush;
}
extern "C"
void CALLBACK internalTimerProc(UINT id, UINT msg,
DWORD dwUser, DWORD dw1, DWORD dw2)
{
CCmdBatView* pcmdbatview = (CCmdBatView*)dwUser;
CCmdBatDoc* pDoc = pcmdbatview->GetDocument();
if(pDoc->m_LoopCount == 0) return;//有限次循環(huán)
if(pDoc->m_cmdindex == -1) return;//無效
//判斷是否到發(fā)送該命令的時間
if(pDoc->m_cmd_buf[pDoc->m_cmdindex].remain == 0){
pDoc->m_cmd_buf[pDoc->m_cmdindex].remain =
pDoc->m_cmd_buf[pDoc->m_cmdindex].ticks;
Transmit(pDoc->m_cmd_buf[pDoc->m_cmdindex].cmdframe);
pDoc->m_cmdindex++;//next command
if(pDoc->m_cmdindex >= pDoc->m_cmd_buf.GetSize()){
pDoc->m_cmdindex = 0;//reset
pDoc->m_LoopCount--;
}
}
else{//否,則等待下一個時間片
pDoc->m_cmd_buf[pDoc->m_cmdindex].remain--;
}
}
void CCmdBatView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
m_bkbrush.CreateSolidBrush(RGB(245,240,245));
// buttons
m_btnhelp.SubclassDlgItem(IDI_ICON_HELP,this);
m_btnhelp.SetIcon(IDI_ICON_HELP);
/////////////
TIMECAPS tc;
//通過函數(shù)timeGetDevCaps取出系統(tǒng)分辨率的取值范圍(對intel系統(tǒng),1~16毫秒),
//如果無錯則繼續(xù)
if (TIMERR_NOERROR == timeGetDevCaps(&tc,sizeof(TIMECAPS)))
{
//分辨率的值不能超出系統(tǒng)的取值范圍
m_TimerRes = min(max(tc.wPeriodMin,1),tc.wPeriodMax);
//調(diào)用timeBeginPeriod函數(shù)設(shè)置定時器的分辨率,類似于for循環(huán)的步長
timeBeginPeriod(m_TimerRes);
}
m_TimerID = timeSetEvent(6,m_TimerRes,internalTimerProc,
(DWORD)this,TIME_PERIODIC);
}
#include "HelpFrmDlg.h"
void CCmdBatView::OnButtonHelp()
{
CHelpFrmDlg dlg;
dlg.DoModal();
}
void CCmdBatView::OnValid()
{
UpdateData(TRUE);
m_sCmdBat += "\r\n";//必須以回車換行結(jié)束
CCmdBatDoc* pDoc = GetDocument();
pDoc->m_cmdindex = -1;//先取消當前運行命令隊列
pDoc->m_cmd_buf.RemoveAll();//清空命令隊列
//構(gòu)造新的命令隊列
BOOL validcmd = FALSE;//判斷是否輸入合法
struct Cmd cmd;
while(sscanf(m_sCmdBat,"%2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
&cmd.ticks,&cmd.cmdframe[0],&cmd.cmdframe[1],&cmd.cmdframe[2],
&cmd.cmdframe[3],&cmd.cmdframe[4],&cmd.cmdframe[5],&cmd.cmdframe[6],
&cmd.cmdframe[7],&cmd.cmdframe[8],&cmd.cmdframe[9],&cmd.cmdframe[10])
== 12){
cmd.remain = cmd.ticks;
pDoc->m_cmd_buf.Add(cmd);
validcmd = TRUE;//只要有一條就要執(zhí)行
m_sCmdBat = m_sCmdBat.Right(m_sCmdBat.GetLength()-m_sCmdBat.Find("\r\n")-2);
}
if(validcmd){
pDoc->m_LoopCount = m_LoopCount;
pDoc->m_cmdindex = 0;//生效
}
}
void CCmdBatView::OnInvalid()
{
CCmdBatDoc* pDoc = GetDocument();
pDoc->m_cmdindex = -1;//先取消當前運行命令隊列
pDoc->m_cmd_buf.RemoveAll();//清空命令隊列
}
void CCmdBatView::OnDestroy()
{
if(m_TimerID){
timeKillEvent(m_TimerID);
}
if (m_TimerRes){
timeEndPeriod(m_TimerRes);
}
CFormView::OnDestroy();
}
void CCmdBatView::OnButtonLoad()
{
CFileDialog dlg(TRUE,"cmd",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"批處理文件(*.cmd)|*.cmd||",this);
if(dlg.DoModal() == IDCANCEL) return;
CString fname = dlg.GetPathName();
CFile f;
if(f.Open(fname,CFile::modeRead) ==0){
AfxMessageBox("讀文件出錯!");
return;
}
CArchive ar(&f,CArchive::load);
ar >> m_sNote;
ar >> m_LoopCount;
ar >> m_sCmdBat;
ar.Close();
f.Close();
UpdateData(FALSE);
}
void CCmdBatView::OnButtonSave()
{
UpdateData();
CFileDialog dlg(FALSE,"cmd",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"批處理文件(*.cmd)|*.cmd||",this);
if(dlg.DoModal() == IDCANCEL) return;
CString fname = dlg.GetPathName();
CFile f;
if(f.Open(fname,CFile::modeWrite|CFile::modeCreate) ==0){
AfxMessageBox("寫文件出錯!");
return;
}
CArchive ar(&f,CArchive::store);
ar << m_sNote;
ar << m_LoopCount;
ar << m_sCmdBat;
ar.Close();
f.Close();
}
LRESULT CCmdBatView::OnReceiveFrame(WPARAM w,LPARAM l)
{
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -