在实现这程序的过程中,关于字符串的使用。
1、CString类型加法。指令操作是由各个功能区组成的。
Instruction = pParentDlg->m_StationID + pParentDlg->m_PCID + Instruction + pParentDlg->m_WaitingTime + SoftElementType+ SoftElementNum + m_SoftElementAmount;
2、字符串前面补“0”,使得长度一致
for (int i = 0; i<(4 - LengthOfSoftElementNum); i++)
{
SoftElementNum = _T("0") + SoftElementNum;
}
3、字符转HEX(2位)
for (int i = 0; i
转HEX时,有大小写差异,%x 转换后 是小写, %X转换后是大写
4、特殊字符用转义符
if (pParentDlg->m_Ctrl_Protocol.GetCurSel() == 0)
{
m_InstructionPackage = _T("\x5") + m_Command;
}
else if (pParentDlg->m_Ctrl_Protocol.GetCurSel() == 1)
{
m_InstructionPackage = _T("\x5") + m_Command + _T("\r") + _T("\n");
}
CWnd* pcCComboBox;
pcCComboBox = GetDlgItem(IDC_COMBO_SOFTELEMENTTYPE1);
SoftElementType1 = GetSoftElementType(pcCComboBox);
CString PC_FXPLCDlg_Test::GetSoftElementType(CWnd* cComboBox)
//定义一个指向主窗口类的指针,通过该指针来获取其他变量
//两次GetParent(),第一次得到Tab,第二次得到主对话框
CPC_FXPLCDlg *pParentDlg = (CPC_FXPLCDlg *)GetParent()->GetParent();
Instruction = pParentDlg->m_StationID + pParentDlg->m_PCID + Instruction + pParentDlg->m_WaitingTime + ElementsAmount + SoftElementType1 + SoftElementID1 + SoftElementData1;
1、需要建立border为none、style为child的窗口及关联类;
2、在主窗口类中添加子窗口类变量,在主窗口的初始化函数中,添加如下代码
//Tab区初始化
CRect tabRect; // 标签控件客户区的位置和大小
m_Ctrl_Tab.InsertItem(0, _T("成批软元件读写")); // 插入第一个标签“成批软元件读写”
m_Ctrl_Tab.InsertItem(1, _T("指定软元件读写")); // 插入第二个标签“指定软元件读写”
m_Ctrl_Tab.InsertItem(2, _T("其他指令"));
m_rwDlg.Create(IDD_PC_FXPLCDLG_RW, &m_Ctrl_Tab); // 创建第一个标签页
m_testDlg.Create(IDD_PC_PLCTEST_DIALOG, &m_Ctrl_Tab); // 创建第二个标签页
m_othersDlg.Create(IDD_PC_PLC_DIALOG_OTHERS, &m_Ctrl_Tab);
m_Ctrl_Tab.GetClientRect(&tabRect); // 获取标签控件客户区Rect
// 调整tabRect,使其覆盖范围适合放置标签页
tabRect.left += 1;
tabRect.right -= 1;
tabRect.top += 20;
tabRect.bottom -= 1;
// 根据调整好的tabRect放置m_rwDlg子对话框,并设置为显示
m_rwDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
// 根据调整好的tabRect放置m_testDlg子对话框,并设置为隐藏
m_testDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
// 根据调整好的tabRect放置m_othersDlg子对话框,并设置为隐藏
m_othersDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
3、定义tab切换的函数
void CPC_FXPLCDlg::OnTcnSelchangeTabFunctions(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
CRect tabRect; // 标签控件客户区的Rect
// 获取标签控件客户区Rect,并对其调整,以适合放置标签页
m_Ctrl_Tab.GetClientRect(&tabRect);
tabRect.left += 1;
tabRect.right -= 1;
tabRect.top += 20;
tabRect.bottom -= 1;
switch (m_Ctrl_Tab.GetCurSel())
{
// 如果标签控件当前选择标签为“成批软元件读写”,则隐藏m_testDlg、m_othersDlg对话框, 显示m_rwDlg对话框
case 0:
m_rwDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
m_testDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_othersDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
break;
// 如果标签控件当前选择标签为“指定软元件读写”,则隐藏m_rwDlg、m_othersDlg对话框,显示m_testDlg对话框
case 1:
m_rwDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_testDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
m_othersDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
break;
// 如果标签控件当前选择标签为“其他指令”,则隐藏m_rwDlg、m_testDlg对话框,显示m_othersDlg对话框
case 2:
m_rwDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_testDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_othersDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
break;
default:
break;
}
}
UINT Sum_Instruction = 0;
for (int i = 0; i <= Instruction.GetLength();i++)
{
char c = Instruction[i];
Sum_Instruction = Sum_Instruction + c;
}
m_SumOfInstruction.Format(_T("%X"), Sum_Instruction);
void CPC_FXPLCDlg_RW::OnEnKillfocusEditSoftelementamount()
{
// TODO: 在此添加控件通知处理程序代码
CString SoftElementAmount_New;
m_Ctrl_SoftElementAmount.GetWindowTextW(SoftElementAmount_New);
BOOL Flag_SoftElementAmount = TRUE;
int i = 0;
char c = SoftElementAmount_New[i];
for (;SoftElementAmount_New[i] != '\0';i++)
{
if (Flag_SoftElementAmount)
{
if (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'))))
{
AfxMessageBox(_T("输入的软元数量非法"));
Flag_SoftElementAmount = FALSE;
UpdateData(FALSE);
return;
}
}
}
if (Flag_SoftElementAmount)
{
UpdateData(TRUE);
UpdateData(FALSE);
}
}
程序代码:
http://download.csdn.net/detail/pengjc2001/9752456