comrdlg.cpp
来自「VC++高级编程技巧与示例」· C++ 代码 · 共 158 行
CPP
158 行
// comrDlg.cpp : implementation file
//
#include "stdafx.h"
#include "comr.h"
#include "comrDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComrDlg dialog
CComrDlg::CComrDlg(CWnd* pParent /*=NULL*/)
: CDialog(CComrDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CComrDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CComrDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CComrDlg)
DDX_Control(pDX, IDC_EDIT1, m_edit1);
DDX_Control(pDX, IDC_MSCOMM1, m_com2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CComrDlg, CDialog)
//{{AFX_MSG_MAP(CComrDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_KEYDOWN()
ON_BN_CLICKED(IDC_CLEAR, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComrDlg message handlers
BOOL CComrDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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_com2.SetPortOpen(TRUE);
m_Str="";
count=0;
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CComrDlg::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 CComrDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CComrDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
BEGIN_EVENTSINK_MAP(CComrDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CComrDlg)
ON_EVENT(CComrDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CComrDlg::OnComm()
{
// TODO: Add your control notification handler code here
VARIANT m_input1;
COleSafeArray m_input2;
long length,i;
BYTE data[1024];
CString str,str1;
str1.Empty();
if(m_com2.GetCommEvent()==2)
{
m_input1=m_com2.GetInput();//读取缓冲区内的数据
m_input2=m_input1;//将VARIANT型变量转换为ColeSafeArray型变量
length=m_input2.GetOneDimSize();//确定数据长度
for(i=0;i<length;i++)
m_input2.GetElement(&i,data+i);//将数据转换为BYTE型数组
for(i=0;i<length;i++)//将数组转换为Cstring型变量
{
char a=* (char *)(data+i);
str.Format("%c",a);
str1+=str;
}
str1+="\r\n";
m_Str+=str1;
SetDlgItemText(IDC_EDIT,m_Str);
count++;
str.Format("%d",count);
SetDlgItemText(IDC_EDIT1,str);
}
}
void CComrDlg::OnClear()
{
// TODO: Add your control notification handler code here
CString str;
m_Str.Empty();
count=0;
str.Format("%d",count);
SetDlgItemText(IDC_EDIT,m_Str);
SetDlgItemText(IDC_EDIT1,str);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?