📄 rs232dlg.cpp
字号:
// RS232Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "RS232.h"
#include "RS232Dlg.h"
#include "FileOpenSave.h"
#include "FontSetWindows.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
HANDLE hCom;
CString FileName,Buf;
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()
/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg dialog
CRS232Dlg::CRS232Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CRS232Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRS232Dlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRS232Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRS232Dlg)
DDX_Control(pDX, IDC_OPENCOM, m_capilot);
DDX_Control(pDX, IDC_VALUE, m_Value);
DDX_Control(pDX, IDC_COMNUM, m_ComNum);
DDX_Control(pDX, IDC_EDITDATA, m_disp);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRS232Dlg, CDialog)
//{{AFX_MSG_MAP(CRS232Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EDIT, OnEdit)
ON_COMMAND(ID_MENUIEXIT, OnMenuiexit)
ON_BN_CLICKED(IDC_SERVAL, OnServal)
ON_COMMAND(ID_MENUABOUT, OnMenuabout)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_COMMAND(ID_MENUOPEN, OnMenuopen)
ON_BN_CLICKED(IDC_OPENCOM, OnOpencom)
ON_WM_CLOSE()
ON_COMMAND(ID_MENUASSAVE, OnMenuassave)
ON_COMMAND(ID_MENUSAVE, OnMenusave)
ON_COMMAND(ID_MENUFONTSET, OnMenufontset)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRS232Dlg message handlers
BOOL CRS232Dlg::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
m_ComNum.AddString("COM1");
m_ComNum.AddString("COM2");
m_ComNum.AddString("COM3");
m_ComNum.AddString("COM4");
m_Value.AddString(" 9600");
m_Value.AddString("19200");
m_Value.AddString("38400");
m_Value.AddString("57600");
m_ComNum.SetCurSel(0);
m_Value.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CRS232Dlg::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 CRS232Dlg::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();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CRS232Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRS232Dlg::OnEdit()
{
OnClose();
}
void CRS232Dlg::OnMenuiexit()
{
OnClose();
}
void CRS232Dlg::OnServal()
{
CString s1;
unsigned int length;
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITDATA);//GET TEXT
pEdit->GetWindowText(s1);
length=pEdit->GetWindowTextLength();
DWORD dwBytesWrite=length;
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
bWriteStat=WriteFile(hCom,s1,dwBytesWrite,&dwBytesWrite,NULL);
if(!bWriteStat)
{
AfxMessageBox("写串口失败!");
}
}
void CRS232Dlg::OnMenuabout()
{
CAboutDlg dlg;
dlg.DoModal();
}
void CRS232Dlg::OnClear()
{
m_disp.SetText("");
}
void CRS232Dlg::OnMenuopen()
{
CFileOpenSave OFile(TRUE,"*.TXT","*.TXT");
if(OFile.DoModal()==IDOK)
{
CFile file;
UINT FileSize;
FileName=OFile.GetPathName();
VERIFY(file.Open(FileName,CFile::modeRead));
FileSize=file.GetLength();
file.Read(Buf.GetBuffer(FileSize),FileSize);
m_disp.SetText(Buf);
file.Close();
}
}
BOOL CRS232Dlg::COMOPEN()
{
ComboNum=m_ComNum.GetCurSel();
if(ComboNum==0)COMNUM="COM1";
if(ComboNum==1)COMNUM="COM2";
if(ComboNum==2)COMNUM="COM3";
if(ComboNum==3)COMNUM="COM4";
ComboNum=m_Value.GetCurSel();
if(ComboNum==0)COMVALUE=9600;
if(ComboNum==1)COMVALUE=19200;
if(ComboNum==2)COMVALUE=38400;
if(ComboNum==3)COMVALUE=57600;
hCom=CreateFile(COMNUM,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hCom==(HANDLE)-1)
{
MessageBox("打开串口失败!","错误!",MB_ICONERROR);
return FALSE;
}
SetupComm(hCom,100,100);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hCom,&TimeOuts);
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=COMVALUE;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=TWOSTOPBITS;
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
}
BOOL CRS232Dlg::COMCLOSE()
{
CloseHandle(hCom);
return TRUE;
}
void CRS232Dlg::OnOpencom()
{
if(ButtonState==FALSE)
{
if(COMOPEN()==TRUE)
m_capilot.SetWindowText("关闭串口(&L)");
}
else
{
COMCLOSE();
m_capilot.SetWindowText("打开串口(&O)");
}
if(ButtonState==FALSE)ButtonState=TRUE;
else ButtonState=FALSE;
}
void CRS232Dlg::OnClose()
{
if(MessageBox("真的要退出吗","提示",MB_ICONINFORMATION|MB_OKCANCEL|MB_DEFBUTTON2)!=IDCANCEL)
{
COMCLOSE();
CDialog::OnOK();
}
}
void CRS232Dlg::OnMenuassave()
{
CFileOpenSave OFile(FALSE,"*.TXT","*.TXT",2);
if(OFile.DoModal()==IDOK)
{
UINT FileSize;
CFile file;
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITDATA);
pEdit->GetWindowText(Buf);
FileName=OFile.GetPathName();
VERIFY(file.Open(FileName,CFile::modeCreate|CFile::modeWrite));
FileSize=Buf.GetLength();
file.Write(Buf.GetBuffer(FileSize),FileSize);
file.Close();
}
}
void CRS232Dlg::OnMenusave()
{
UINT FileSize;
if(FileName=="")OnMenuassave();
else
{
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDITDATA);
pEdit->GetWindowText(Buf);
CFile file(FileName,CFile::modeCreate|CFile::modeReadWrite);
FileSize=Buf.GetLength();
file.Write(Buf.GetBuffer(FileSize),FileSize);
file.Close();
}
}
void CRS232Dlg::OnMenufontset()
{
CFontSetWindows FontSet;
if(FontSet.DoModal()==IDOK)
{
LOGFONT lf;
COLORREF FontColor;
CFont fontText;
FontSet.GetCurrentFont(&lf);
fontText.CreateFont(lf.lfHeight,lf.lfWidth,lf.lfEscapement,lf.lfOrientation,lf.lfWeight,lf.lfItalic,lf.lfUnderline,lf.lfStrikeOut,lf.lfCharSet,lf.lfOutPrecision,lf.lfOutPrecision,lf.lfQuality,lf.lfPitchAndFamily,lf.lfFaceName);
m_disp.SetFont(&fontText);
FontColor=FontSet.GetColor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -