⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scommtestdlg.cpp

📁 一个串口通信的例子
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}

void CSCommTestDlg::OnStaticTop() //要想CStatic控件响应消息事件,必须把它的属性设为Notify
{
	// TODO: Add your control notification handler code here 
    m_ctrlStaticTop.ModifyStyle(0,SS_BITMAP|SS_CENTERIMAGE);
	bTopCounter=!bTopCounter;
	if(!bTopCounter)
	{
	AfxGetMainWnd()->SetWindowPos(&wndNoTopMost, 0, 0, 0, 0,SWP_NOMOVE | SWP_NOSIZE);
	m_bmpBitmap.LoadBitmap(IDB_BITMAP);
	m_ToolTips.AddTool((CStatic*)GetDlgItem(IDC_STATIC_TOP),"NoTop");
	}
	else
	{
	SetWindowPos(&wndTopMost, 0, 0, 0, 0,SWP_NOMOVE | SWP_NOSIZE);
	m_bmpBitmap.LoadBitmap(IDB_BITMAPTOP);
	m_ToolTips.AddTool((CStatic*)GetDlgItem(IDC_STATIC_TOP),"Top");
	}
	m_ctrlStaticTop.SetBitmap(m_bmpBitmap);
	m_bmpBitmap.DeleteObject( );
}

void CSCommTestDlg::OnLoadfile() 
{
	// TODO: Add your control notification handler code here

//	static char LoadFileFilter[]="Text Files(*.txt)|*.txt|All Files (*.*)|*.*||";
//	CFileDialog LoadFileNameDlg(TRUE,".txt",NULL,OFN_ENABLEHOOK|OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,LoadFileFilter);
	//以上2句作用同下:
	static char LoadFileFilter[]="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0";
	CFileDialog LoadFileNameDlg(TRUE,NULL,NULL,OFN_ENABLEHOOK|OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
	LoadFileNameDlg.m_ofn.lpstrFilter=LoadFileFilter;
//	LoadFileNameDlg.m_ofn.Flags=OFN_ENABLEHOOK|OFN_ALLOWMULTISELECT|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;//另一种显示方式
	LoadFileNameDlg.m_ofn.lpstrInitialDir="D:\\My Documents\\";
	LoadFileNameDlg.m_ofn.lpstrTitle="载入文件";
//判断系统的版本
	DWORD dwVersion,dwWindowsMajorVersion,dwWindowsMinorVersion;
	dwVersion=GetVersion();
	dwWindowsMajorVersion=(DWORD)(LOBYTE(LOWORD(dwVersion)));
	dwWindowsMinorVersion=(DWORD)(HIBYTE(LOWORD(dwVersion)));
	if(dwVersion<0X80000000)//若是Windows NT/2000
	{
		LoadFileNameDlg.m_ofn.lStructSize=88;//显示新的文件对话框
	}
	else   //若是Windows 95/98
	{
        LoadFileNameDlg.m_ofn.lStructSize=76;//显示老的文件对话框
	}
	if(LoadFileNameDlg.DoModal()==IDOK)
	{
		m_strCatalog=LoadFileNameDlg.GetPathName();
		GetDlgItem(IDC_EDIT_CATALOG)->SetWindowText(m_strCatalog);
//		CFile LoadFile;
//		if(LoadFile.Open(m_strCatalog/*若用字符串,则例如:"D:\\My Documents\\杨玲.txt"*/,CFile::modeRead))
/*		{
			int bytecount=0;
			bytecount=(int)(LoadFile.GetLength());//返回字节数(不包括'\0'在内)
			char *p=new char[bytecount+1];//增加1个字节用于存放结束符'\0'
			memset(p, 0, bytecount+1);//初始化字符数组
			int i=LoadFile.Read(p,bytecount);//读取文件中实际数量的字符(不包括'\0')
			m_strTXData.Empty();
			m_strTXData=p;
			delete []p;
			LoadFile.Close();
			UpdateData(FALSE);
			GetDlgItem(IDC_BUTTON_SENDFILE)->EnableWindow(TRUE);
		}*/
		GetDlgItem(IDC_EDIT_TXDATA)->SetWindowText("文件被载入!");
		GetDlgItem(IDC_BUTTON_SENDFILE)->EnableWindow(TRUE);
		return;
	}
	return;	
}



void CSCommTestDlg::OnSendfile() 
{
	// TODO: Add your control notification handler code here
	    m_iSentFilesCount++;//发送文件计数器更新
		CFile SendFile;
		if(SendFile.Open(m_strCatalog/*若用字符串,则例如:"D:\\My Documents\\杨玲.txt"*/,CFile::modeRead))
		{
			int bytecount=0;
			bytecount=(int)(SendFile.GetLength());//返回字节数(不包括'\0'在内)
			if(bytecount>1000)
			{
				int lastsendcount=bytecount%1000;//最后一次应发送的字节数
				int sendcount=(bytecount-lastsendcount)/1000;//发送1000个字符的次数
				char *p=new char[bytecount+1];
		    	memset(p, 0, bytecount+1);//初始化字符数组
		    	int i=SendFile.Read(p,bytecount);//读取文件中实际数量的字符(不包括'\0')
			    CString strtemp=p;
				delete []p;
				CString everysendstr="";
				int strtempcount=bytecount;
				for(int j=1;j<=sendcount;j++)
				{
					everysendstr=strtemp.Left(1000);
					strtemp=strtemp.Right(strtempcount-1000);
					strtempcount=strtempcount-1000;
					SendFileData(everysendstr);
				}
				SendFileData(strtemp);
				GetDlgItem(IDC_BUTTON_MANUALSEND)->EnableWindow(TRUE);
               	GetDlgItem(IDC_CHECK_AUTOSEND)->EnableWindow(TRUE);
				GetDlgItem(IDC_BUTTON_SENDFILE)->EnableWindow(FALSE);
			}
			else
			{
				char *p=new char[bytecount+1];
		    	memset(p, 0, bytecount+1);//初始化字符数组
		    	int i=SendFile.Read(p,bytecount);//读取文件中实际数量的字符(不包括'\0')
			    CString strtemp=p;
				delete []p;
				SendFileData(strtemp);
				GetDlgItem(IDC_BUTTON_MANUALSEND)->EnableWindow(TRUE);
               	GetDlgItem(IDC_CHECK_AUTOSEND)->EnableWindow(TRUE);
				GetDlgItem(IDC_BUTTON_SENDFILE)->EnableWindow(FALSE);
			}
		
			SendFile.Close();
			TXStatus+=bytecount;
			CString s;
	        s.Format("%d",TXStatus);
	        m_strTXCount.Empty();
	        m_strTXCount="TX:"+s;
   	        UpdateData(FALSE);
		}
		CString strDisplay;
		strDisplay.Format("%d",m_iSentFilesCount);
		strDisplay+="个文件已发送完毕!";
		GetDlgItem(IDC_STATIC_SENDSTATUS)->SetWindowText(strDisplay);
		return;
}

void CSCommTestDlg::SendFileData(CString str)
{
	if(!(m_ctrlComm.GetPortOpen()))
	{
	    MessageBox("串口还未打开!","SComDebugAssaint",MB_ICONHAND|MB_OK);
        return;
	}
	GetDlgItem(IDC_BUTTON_MANUALSEND)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_AUTOSEND)->EnableWindow(FALSE);
	GetDlgItem(IDC_STATIC_SENDSTATUS)->SetWindowText("正在发送文件,请等待!");
	if(m_ctrlHexSend.GetCheck())
	{
	    CByteArray hexdata;
	    int len=String2Hex(str,hexdata);//输入字符串everysendstr,输出16进制数hexdata
	    m_ctrlComm.SetOutput(COleVariant(hexdata));//发送16进制数
	}
	else
		m_ctrlComm.SetOutput(COleVariant(str));//发送数据

}

void CSCommTestDlg::OnDelRxTxcount() 
{
	// TODO: Add your control notification handler code here
	m_strRXCount="RX:0";
	m_strTXCount="TX:0";
	TXStatus=0;
	RXStatus=0;
	GetDlgItem(IDC_STATIC_TXSTATUS)->SetWindowText(m_strTXCount);
	GetDlgItem(IDC_STATIC_RXSTATUS)->SetWindowText(m_strRXCount);
	SetDlgItemText(IDC_EDIT_CATALOG,"还没有选择要发送的文件!");
	SetDlgItemText(IDC_STATIC_SENDSTATUS,"没有文件被发送!");
// 或UpdateData(FALSE);
}

void CSCommTestDlg::OnChangeSavePath() 
{
	// TODO: Add your control notification handler code here
	static char SaveFileFilter[]="Text Files(*.txt)\0*.txt\0";
	CFileDialog SaveFileDlg(FALSE);
	SaveFileDlg.m_ofn.lpstrTitle="保存文件";
	SaveFileDlg.m_ofn.lpstrFilter=SaveFileFilter;
	DWORD dwVersion;
	dwVersion=::GetVersion();
	if(dwVersion<0X80000000)
	{
		SaveFileDlg.m_ofn.lStructSize=88;
	}
	else
	{
		SaveFileDlg.m_ofn.lStructSize=76;
	}
	if(SaveFileDlg.DoModal()==IDOK)
	{
		UpdateData(TRUE);
		CFile SaveFile;
		CString SaveFilePath=SaveFileDlg.GetPathName();
		if(SaveFileDlg.GetFileExt()!="txt")
		{
			SaveFilePath=SaveFileDlg.GetPathName()+".txt";
		}
	    if(SaveFile.Open(SaveFilePath,CFile::modeCreate|CFile::modeWrite))
		{
//		    MessageBox("测试");
			CString SaveDate;
			CTime time=CTime::GetCurrentTime();
			SaveDate.Format("%d年%d月%d日%d时%d分%d秒",time.GetYear(),time.GetMonth(),time.GetDay()
				,time.GetHour(),time.GetMinute(),time.GetSecond());
			SaveDate="     "+SaveDate+"\r\n"+m_strRXData;// \r为换行
//			MessageBox(SaveDate);
		    SaveFile.Write(SaveDate,SaveDate.GetLength());
		}
	    SaveFile.Close();
		m_strSavePath=SaveFileDlg.GetPathName();
	}
	UpdateData(FALSE);
	return;
}





void CSCommTestDlg::OnSize(UINT nType, int cx, int cy) 
{

	CDialog::OnSize(nType, cx, cy);
	// update size-grip
	InvalidateRect(&m_rcGripRect);
	UpdateGripPos();
	InvalidateRect(&m_rcGripRect);
	// TODO: Add your message handler code here
	if(cx==0 && cy==0) return;//最小化时
	if(IsWindow(m_ctrlRxDataIsFull.m_hWnd))//判断对话框是否已经存在(生成)
	{     
		CRect RXrect;
		m_ctrlRxDataIsFull.GetWindowRect(RXrect);//得到屏幕坐标
		ScreenToClient(RXrect);//将屏幕坐标转换成客户坐标
		RXrect.right = cx-(m_lOriginalX1-RXrect.right);
		RXrect.bottom = cy-(m_lOriginalY1-RXrect.bottom);
		m_ctrlRxDataIsFull.MoveWindow(RXrect,TRUE);//控件的移动是相对对话框的,所以rt中必须用客户坐标

		LeftNoChang(IDC_EDIT_TXDATA,cx,cy);
		LeftRightNoChang(IDC_BUTTON_DELTXDATA,cx,cy);
		LeftRightNoChang(IDC_STATIC_TX,cx,cy);
		LeftRightNoChang(IDC_CHECK_HEXSEND,cx,cy);
		LeftRightNoChang(IDC_BUTTON_MANUALSEND,cx,cy);
		LeftRightNoChang(IDC_STATIC_CYCLE,cx,cy);
		LeftRightNoChang(IDC_CHECK_AUTOSEND,cx,cy);
		LeftRightNoChang(IDC_EDIT_AUTOSENDTIME,cx,cy);
		LeftRightNoChang(IDC_STATIC_HAOMIAO,cx,cy);
		LeftRightNoChang(IDC_STATIC_COMSTATUS,cx,cy);
		LeftRightNoChang(IDC_BUTTON_LOADFILE,cx,cy);
     
		LeftRightNoChang(IDC_BUTTON_DELCOUNT,cx,cy);
		LeftRightNoChang(IDC_BUTTON_SENDFILE,cx,cy);
		LeftRightNoChang(IDC_STATIC_SENDSTATUS,cx,cy);
		LeftRightNoChang(IDC_EDIT_CATALOG,cx,cy);
		LeftRightNoChang(IDC_STATIC_TOP,cx,cy);
		LeftRightNoChang(IDC_STATIC_RXSTATUS,cx,cy);
		LeftRightNoChang(IDC_STATIC_TXSTATUS,cx,cy);
        AllRectParaChange(IDCANCEL,cx,cy);
        AllRectParaChange(IDC_STATIC_TIME,cx,cy);
		AllRectParaChange(IDC_STATIC_TIMETXET,cx,cy);
		AllRectParaChange(IDC_STATIC_TIMER,cx,cy);
		Invalidate();
		m_lOriginalX1 = cx;
     	m_lOriginalY1 = cy;
		rect.bottom=rect.top+cy;
		rect.right=rect.left+cx;
		return;
	}
	m_lOriginalX = cx;
	m_lOriginalY = cy;
	m_lOriginalX1 = cx;
    m_lOriginalY1 = cy;

}

void CSCommTestDlg::AllRectParaChange(int id ,int cx, int cy)
{
	    CRect rect;
		GetDlgItem(id)->GetWindowRect(rect);
		ScreenToClient(rect);
		long height=rect.bottom-rect.top;
		long width=rect.right-rect.left;
		rect.right = cx-(m_lOriginalX1-rect.right);
		rect.left =rect.right-width;
		rect.bottom = cy-(m_lOriginalY1-rect.bottom);
		rect.top=rect.bottom-height;
		GetDlgItem(id)->MoveWindow(rect,TRUE);
		return;
}

void CSCommTestDlg::LeftNoChang(int id, int cx, int cy)
{
	    CRect rect;
		GetDlgItem(id)->GetWindowRect(rect);
		ScreenToClient(rect);
		rect.right = cx-(m_lOriginalX1-rect.right);
		long height=rect.bottom-rect.top;
		rect.bottom = cy-(m_lOriginalY1-rect.bottom);
		rect.top=rect.bottom-height;
		GetDlgItem(id)->MoveWindow(rect,TRUE);
}

void CSCommTestDlg::LeftRightNoChang(int id, int cx, int cy)
{
	    CRect rect;
		GetDlgItem(id)->GetWindowRect(rect);
		ScreenToClient(rect);
		long height=rect.bottom-rect.top;
		rect.bottom = cy-(m_lOriginalY1-rect.bottom);
		rect.top=rect.bottom-height;
		GetDlgItem(id)->MoveWindow(rect,TRUE);
}





void CSCommTestDlg::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CDialog::CalcWindowRect(lpClientRect, nAdjustType);
}

int CSCommTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	return 0;
}

void CSCommTestDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	    this->GetWindowRect(rect);
	CDialog::OnMouseMove(nFlags, point);
}

void CSCommTestDlg::UpdateGripPos()
{
	GetClientRect(&m_rcGripRect);

	m_rcGripRect.left = m_rcGripRect.right - m_szGripSize.cx;
	m_rcGripRect.top = m_rcGripRect.bottom - m_szGripSize.cy;

}
UINT CSCommTestDlg::OnNcHitTest(CPoint point)
{
	CPoint pt = point;//point为光标位置,按屏幕坐标表示
	ScreenToClient(&pt);

	// if in size grip and in client area
	if(m_rcGripRect.PtInRect(pt) &&
		pt.x >= 0 && pt.y >= 0)
		return HTBOTTOMRIGHT;//返回鼠标枚举值,鼠标在窗口边框的右下角
//处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。
	UINT nHitTest=CDialog::OnNcHitTest (point);//获得鼠标点击位置
    return (nHitTest==HTCLIENT)? HTCAPTION : nHitTest;
//默认的return CDialog::OnNcHitTest(point);

}

void CSCommTestDlg::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
	if(!m_bInitDone)
		return;
	lpMMI->ptMinTrackSize = m_ptMinTrackSize;

}

void CSCommTestDlg::SaveWindowRect()
{
	CString data;
	WINDOWPLACEMENT wp;

	ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
	wp.length = sizeof(WINDOWPLACEMENT);
	GetWindowPlacement(&wp);
	
	RECT& rc = wp.rcNormalPosition;	// alias(别名)

	data.Format(PROFILE_FMT, rc.left, rc.top,
		rc.right, rc.bottom, wp.showCmd, wp.flags);

	AfxGetApp()->WriteProfileString(m_sSection, m_sEntry, data);

}

void CSCommTestDlg::LoadWindowRect()
{
	CString data;
	WINDOWPLACEMENT wp;

	data = AfxGetApp()->GetProfileString(m_sSection, m_sEntry);
	
	if (data.IsEmpty())	// never saved before
		return;
	
	ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
	wp.length = sizeof(WINDOWPLACEMENT);
	GetWindowPlacement(&wp);

	RECT& rc = wp.rcNormalPosition;	// alias

	if (_stscanf(data, PROFILE_FMT, &rc.left, &rc.top,
		&rc.right, &rc.bottom, &wp.showCmd, &wp.flags) == 6)
	{
		SetWindowPlacement(&wp);
	}

}



void CSCommTestDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	SaveWindowRect();
}

BOOL CSCommTestDlg::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class

	return CDialog::PreCreateWindow(cs);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -