📄 senddataedit.cpp
字号:
// SendDataEdit.cpp : implementation file
//
#include "stdafx.h"
#include "maintain.h"
#include "SendDataEdit.h"
#include "manager.h"
#include "maintainview.h"
#include "mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static int j=0;
static int nRequestFDK = 1,nRequestCount = 0,nRequest0420 = 1;
/////////////////////////////////////////////////////////////////////////////
// CSendDataEdit
CSendDataEdit::CSendDataEdit()
{
bParityMenu = false;
bRequestFDK = false;
bRequest0420 = false;
}
CSendDataEdit::~CSendDataEdit()
{
}
BEGIN_MESSAGE_MAP(CSendDataEdit, CEdit)
//{{AFX_MSG_MAP(CSendDataEdit)
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_SELALL, OnSelall)
ON_COMMAND(ID_COPY, OnCopy)
ON_COMMAND(ID_DELETE, OnDelete)
ON_COMMAND(ID_PASTE, OnPaste)
ON_COMMAND(ID_CREATEPARITY, OnCreateparity)
ON_COMMAND(ID_CDTPARITY, OnCDTparity)
ON_COMMAND(ID_CHECKSUMPARITY, OnChecksumparity)
ON_WM_CHAR()
ON_WM_CREATE()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSendDataEdit message handlers
void CSendDataEdit::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ClientToScreen(&point);
CMenu menu;
BYTE temp[512];
if(menu.LoadMenu(IDR_MENU_SENDDATA))
{
CMenu *submenu = menu.GetSubMenu(0);
if(submenu)
{
int Begin,End;
GetSel(Begin,End);
GetData(temp,nLen);
if((nLen!=5)||(Begin==End))
submenu->EnableMenuItem(ID_CDTPARITY,MF_GRAYED);
if((End-Begin)<=3)
submenu->EnableMenuItem(ID_CREATEPARITY,MF_GRAYED);
else
submenu->EnableMenuItem(ID_CREATEPARITY,MF_ENABLED);
submenu->EnableMenuItem(ID_CHECKSUMPARITY,(End>Begin)?MF_ENABLED:MF_GRAYED);
submenu->TrackPopupMenu(TPM_LEFTALIGN,point.x,point.y,this);
}
}
CEdit::OnRButtonDown(nFlags, point);
}
void CSendDataEdit::OnSelall()
{
// TODO: Add your command handler code here
SetSel(0,-1);
}
void CSendDataEdit::OnCopy()
{
// TODO: Add your command handler code here
Copy();
}
void CSendDataEdit::OnDelete()
{
// TODO: Add your command handler code here
Cut();
}
void CSendDataEdit::OnPaste()
{
// TODO: Add your command handler code here
Paste();
}
void CSendDataEdit::GetData(BYTE *m_data, int& Length)
{
Length = 0;
int Begin,End;
CString Text;
GetWindowText(Text);
CString temp;
GetSel(Begin,End);
//如果没有选中数据就发送所有,否则发送选中
if((End-Begin)>0)
temp = Text.Mid(Begin,End-Begin);
else
temp = Text;
for(int i =0;i<temp.GetLength();i++)
{
if(temp.GetAt(i) == 0X20)
continue;
if(temp.GetAt(i) == 0X0D)
continue;
if(temp.GetAt(i) == 0X0A)
continue;
BYTE c = atoh(temp.GetAt(i));
if((i+1)<temp.GetLength())
{
char d = temp.GetAt(i+1);
if((int)d != 0X20)
{
BYTE c1 = atoh(temp.GetAt(i+1));
m_data[Length] = c*16+c1;
Length ++;
}
}
}
}
BYTE CSendDataEdit::atoh(char cc1)
{
int i;
if((cc1>='0')&&(cc1<='9'))
i=cc1-0x30;
else
{
if((cc1>='a')&&(cc1<='f'))
i=10+(cc1-'a');
if((cc1>='A')&&(cc1<='F'))
i=10+(cc1-'A');
}
return (BYTE)i;
}
void CSendDataEdit::OnCreateparity()
{
// TODO: Add your command handler code here
int Begin,End;
GetSel(Begin,End);
BYTE temp[512];
//int nLen = 0;
GetData(temp,nLen);
WORD Parity = CManager::Crc16Parity(&temp[0],nLen);
bParity[0] = LOBYTE(Parity);
bParity[1] = HIBYTE(Parity);
CString temp1,temp2;
if(bParity[0] < 0x10)
temp1.Format("0%x",bParity[0]);
else
temp1.Format("%x",bParity[0]);
if(bParity[1] < 0x10)
temp2.Format("0%x",bParity[1]);
else
temp2.Format("%x",bParity[1]);
temp1 += ' ';
temp1 += temp2;
//temp1.Format("%x %x",bParity[0],bParity[1]);
CString Text,sel;
GetWindowText(Text);
sel = Text.Mid(Begin,End-Begin);
int i=sel.GetLength();
if (i<=1)
return;
if(sel.GetAt(i-1)==0x20)
{
temp1+=" ";
sel+=temp1;
}
else
{
sel+=" ";
sel+=temp1;
}
ReplaceSel(sel,true);
}
void CSendDataEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CString str;
GetWindowText(str);
int nLen = str.GetLength();
if(::GetKeyState(VK_CONTROL)&0x80) //键盘控制
;
else
{
if((nChar==VK_BACK)\
||(nChar == VK_SPACE)\
||(nChar == VK_END)\
||(nChar == VK_DELETE)\
||(nChar == VK_LEFT)\
||(nChar == VK_RIGHT)\
||(nChar == VK_HOME))
{
if(nChar == VK_SPACE)
{
if(nLen<=1)
return;//第1,2个字符不能是空格
}
}
else if(((nChar>='0')&&(nChar<='9'))||((nChar>='a')&&(nChar<='f'))||((nChar>='A')&&(nChar<='F')))
;/*{
GetWindowText(str);
int strLen = str.GetLength();
if(strLen>=2)
{
if(32!=str.GetAt(strLen-1))
if(32!=str.GetAt(strLen-2))
{
str+=" ";
SetWindowText(str);
SendMessage(WM_KEYDOWN,VK_END);
}
}
}*/
else
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
void CSendDataEdit::AdjustSel()
{
CString Text,sel;
GetWindowText(Text);
int Begin,End;
GetSel(Begin,End);
if((End-Begin)<=3)
{
bParityMenu = false;
return;
}
BYTE bTemp;
if(Begin>0)
{
bTemp = Text.GetAt(Begin-1);
if(bTemp == 0x20)
Begin+=1;
else
Begin-=1;
}
bTemp = Text.GetAt(End);
if(bTemp!=0x20)
End-=2;
else
End-=1;
GetSel(Begin,End);
if((End-Begin)<=3)
{
bParityMenu = false;
return;
}
SetSel(Begin,End);
}
int CSendDataEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CFont *font = CFont::FromHandle((HFONT)::GetStockObject(ANSI_FIXED_FONT));
SetFont(font);
return 0;
}
void CSendDataEdit::SetFrameText(BYTE *buf, int nLen)
{
CString strTemp,str;
for(int i=0;i<nLen;i++)
{
strTemp.Format("%.2X ",buf[i]);
str+=strTemp;
}
GetWindowText(strTemp);
if(strTemp.GetLength() == 0)
SetWindowText(str);
else
{
if(bRequestFDK||bRequest0420)
{
strTemp+="\r\n";
strTemp+=str;
SetWindowText(strTemp);
int nLineCount = GetLineCount();
if(nLineCount > 50)
SetWindowText("");
LineScroll(GetLineCount(),1);
}
else
SetWindowText(str);
}
}
void CSendDataEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
BOOL CSendDataEdit::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN)
{
}
return CEdit::PreTranslateMessage(pMsg);
}
void CSendDataEdit::OnCDTparity()
{
int Begin,End;
GetSel(Begin,End);
BYTE temp[512];
GetData(temp,nLen);
if(nLen != 5)
{
MessageBox("CDT校验字节长度应为5!","提示",MB_ICONWARNING);
return;
}
BYTE Parity = CManager::CdtParity(&temp[0]);
CString temp1;
if(Parity < 0x10)
temp1.Format("0%x",Parity);
else
temp1.Format("%x",Parity);
CString Text,sel;
GetWindowText(Text);
sel = Text.Mid(Begin,End-Begin);
int i=sel.GetLength();
if (i<=1)
return;
if(sel.GetAt(i-1)==0x20)
{
temp1+=" ";
sel+=temp1;
}
else
{
sel+=" ";
sel+=temp1;
}
ReplaceSel(sel,true);
}
void CSendDataEdit::OnChecksumparity()
{
int Begin,End;
GetSel(Begin,End);
BYTE temp[512];
//int nLen = 0;
GetData(temp,nLen);
BYTE Parity = CManager::Check_sum(&temp[0],nLen);
bParity[0] = Parity;
CString temp1,temp2;
if(bParity[0] < 0x10)
temp1.Format("0%x",bParity[0]);
else
temp1.Format("%x",bParity[0]);
temp1 += ' ';
CString Text,sel;
GetWindowText(Text);
sel = Text.Mid(Begin,End-Begin);
int i=sel.GetLength();
if (i<=1)
return;
if(sel.GetAt(i-1)==0x20)
{
temp1+=" ";
sel+=temp1;
}
else
{
sel+=" ";
sel+=temp1;
}
ReplaceSel(sel,true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -