我把它的vc++范例粘贴出来.大家看看阿
// testDlg.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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)
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg:
oDataExchange(CDataExchange* pDX)
{
CDialog:
oDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_ReadCharCount=0;
}
void CTestDlg:
oDataExchange(CDataExchange* pDX)
{
CDialog:
oDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
ON_BN_CLICKED(IDC_CONFIGPORT1, OnConfigPort1)
ON_BN_CLICKED(IDC_CONFIGPORT2, OnConfigPort2)
ON_BN_CLICKED(IDC_CONFIGPORT3, OnConfigPort3)
ON_BN_CLICKED(IDC_CONFIGPORT4, OnConfigPort4)
ON_BN_CLICKED(IDC_EMPTYLIST1, OnEmptylist1)
ON_BN_CLICKED(IDC_EMPTYLIST2, OnEmptylist2)
ON_BN_CLICKED(IDC_EMPTYLIST3, OnEmptylist3)
ON_BN_CLICKED(IDC_EMPTYLIST4, OnEmptylist4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::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_ListBox[0].SubclassDlgItem(IDC_LIST1, this);
m_ListBox[1].SubclassDlgItem(IDC_LIST2, this);
m_ListBox[2].SubclassDlgItem(IDC_LIST3, this);
m_ListBox[3].SubclassDlgItem(IDC_LIST4, this);
HWND hWnd=GetSafeHwnd();
for(int i=0;i<4;i++)
{
if(!InitComPort(hWnd, i+1,WM_COMM_RXCHAR ,9600,'N',8,1,EV_RXCHAR,20))
{
if(i==0)
{
GetDlgItem(IDC_CONFIGPORT1)->EnableWindow(FALSE);
GetDlgItem(IDC_EMPTYLIST1)->EnableWindow(FALSE);
}
else if(i==1)
{
GetDlgItem(IDC_CONFIGPORT2)->EnableWindow(FALSE);
GetDlgItem(IDC_EMPTYLIST2)->EnableWindow(FALSE);
}
else if(i==2)
{
GetDlgItem(IDC_CONFIGPORT3)->EnableWindow(FALSE);
GetDlgItem(IDC_EMPTYLIST3)->EnableWindow(FALSE);
}
else if(i==3)
{
GetDlgItem(IDC_CONFIGPORT4)->EnableWindow(FALSE);
GetDlgItem(IDC_EMPTYLIST4)->EnableWindow(FALSE);
}
}
}
return TRUE; // return TRUE unless you set the focus to a control
}
CTestDlg::~CTestDlg ()
{
ExitCommPort();
}
void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
LONG CTestDlg::OnCommunication(WPARAM ch, LPARAM port)
{
if (port <= 0 || port > 4)
return -1;
CString string;
if((ch<16)&&(ch>=0))
string.Format ("0%X ",ch);
else
string.Format ("%X ",ch);
m_ReadString+=string;
m_ReadCharCount++;
if(m_ReadCharCount==7)
ReadNumber.ch [3]=ch;
if(m_ReadCharCount==8)
ReadNumber.ch [2]=ch;
if(m_ReadCharCount==9)
ReadNumber.ch [1]=ch;
if(m_ReadCharCount==10)
ReadNumber.ch [0]=ch;
if(m_ReadCharCount==11)
{
if(m_ListBox[port-1].GetCount ()==8)
m_ListBox[port-1].ResetContent ();
m_ListBox[port-1].AddString(m_ReadString);
m_ReadCharCount=0;
char buffer[20];
sprintf(buffer,"%010d",ReadNumber.i);
buffer[strlen(buffer)]='/0';
m_ListBox[port-1].AddString(buffer);
m_ListBox[port-1].SetSel(m_ListBox[port-1].GetCount()-1, TRUE);
m_ReadString.Empty ();
}
return 0;
}
void CTestDlg::OnConfigPort1()
{
CConfigDlog* dlg = new CConfigDlog(this, GetDCB(1));
HWND hWnd=GetSafeHwnd();
if (dlg->DoModal() == IDOK)
{
DWORD dwCommEvents = 0;
dwCommEvents |= EV_RXCHAR;
InitComPort(hWnd, 1,WM_COMM_RXCHAR,
atoi(dlg->m_strBaudRate),
dlg->m_strParity[0],
atoi(dlg->m_strDataBits),
atoi(dlg->m_strStopBits),
dwCommEvents,
20);
}
delete dlg;
}
void CTestDlg::OnConfigPort2()
{
// TODO: Add your control notification handler code here
CConfigDlog* dlg = new CConfigDlog(this, GetDCB(2));
HWND hWnd=GetSafeHwnd();
if (dlg->DoModal() == IDOK)
{
DWORD dwCommEvents = 0;
dwCommEvents |= EV_RXCHAR;
InitComPort(hWnd, 2,WM_COMM_RXCHAR,
atoi(dlg->m_strBaudRate),
dlg->m_strParity[0],
atoi(dlg->m_strDataBits),
atoi(dlg->m_strStopBits),
dwCommEvents,
20);
}
delete dlg;
}
void CTestDlg::OnConfigPort3()
{
// TODO: Add your control notification handler code here
CConfigDlog* dlg = new CConfigDlog(this, GetDCB(3));
HWND hWnd=GetSafeHwnd();
if (dlg->DoModal() == IDOK)
{
DWORD dwCommEvents = 0;
dwCommEvents |= EV_RXCHAR;
InitComPort(hWnd, 3,WM_COMM_RXCHAR,
atoi(dlg->m_strBaudRate),
dlg->m_strParity[0],
atoi(dlg->m_strDataBits),
atoi(dlg->m_strStopBits),
dwCommEvents,
20);
}
delete dlg;
}
void CTestDlg::OnConfigPort4()
{
// TODO: Add your control notification handler code here
CConfigDlog* dlg = new CConfigDlog(this, GetDCB(4));
HWND hWnd=GetSafeHwnd();
if (dlg->DoModal() == IDOK)
{
DWORD dwCommEvents = 0;
dwCommEvents |= EV_RXCHAR;
InitComPort(hWnd, 4,WM_COMM_RXCHAR,
atoi(dlg->m_strBaudRate),
dlg->m_strParity[0],
atoi(dlg->m_strDataBits),
atoi(dlg->m_strStopBits),
dwCommEvents,
20);
}
delete dlg;
}
void CTestDlg::OnEmptylist1()
{
// TODO: Add your control notification handler code here
m_ListBox[0].ResetContent();
}
void CTestDlg::OnEmptylist2()
{
// TODO: Add your control notification handler code here
m_ListBox[1].ResetContent();
}
void CTestDlg::OnEmptylist3()
{
// TODO: Add your control notification handler code here
m_ListBox[2].ResetContent();
}
void CTestDlg::OnEmptylist4()
{
// TODO: Add your control notification handler code here
m_ListBox[3].ResetContent();
}
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}