📄 portiodlg.cpp
字号:
// PortIODlg.cpp : implementation file
//
#include "stdafx.h"
#include "PortIO.h"
#include "PortIODlg.h"
#include "dlportio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BYTEIO 0
#define WORDIO 1
#define DWORDIO 2
#define HEXNUM 0
#define DECNUM 1
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CPortIODlg dialog
CPortIODlg::CPortIODlg(CWnd* pParent /*=NULL*/)
: CDialog(CPortIODlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPortIODlg)
m_ioSize = -1;
m_numberFormat = -1;
m_addr = _T("768");
m_value = _T("0");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_dwAddr = 768; //Values used in read\write functions
m_dwValue = 0;
m_bDec = TRUE; //Initially form is set to Decimal
}
void CPortIODlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPortIODlg)
DDX_Radio(pDX, IDC_RADIO_BYTE, m_ioSize);
DDX_Radio(pDX, IDC_RADIO_HEX, m_numberFormat);
DDX_Text(pDX, IDC_EDIT_ADDR, m_addr);
DDX_Text(pDX, IDC_EDIT_VALUE, m_value);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPortIODlg, CDialog)
//{{AFX_MSG_MAP(CPortIODlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_WRITE, OnWrite)
ON_BN_CLICKED(IDC_RADIO_DEC, OnRadioDec)
ON_BN_CLICKED(IDC_RADIO_HEX, OnRadioHex)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPortIODlg message handlers
BOOL CPortIODlg::OnInitDialog()
{
m_ioSize = BYTEIO;
m_numberFormat = DECNUM;
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);
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
return TRUE; // return TRUE unless you set the focus to a control
}
void CPortIODlg::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 CPortIODlg::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 CPortIODlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CPortIODlg::OnButtonAbout()
{
// TODO: Add your control notification handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CPortIODlg::OnRead() //读
{
BOOL bOK = UpdateInternalVariables(); //Get Data
TRACE("Address to read - m_addr %d\n", m_dwAddr);
if (bOK)
{
switch (m_ioSize) {
case BYTEIO:
m_dwValue = DlPortReadPortUchar(m_dwAddr);
break;
case WORDIO:
m_dwValue = DlPortReadPortUshort(m_dwAddr);
break;
case DWORDIO:
m_dwValue = DlPortReadPortUlong(m_dwAddr);
break;
}
TRACE("Value read - m_dwAddr %d\n", m_dwAddr);
}
FormatUpdateDiaglog(); //Write Data
}
void CPortIODlg::OnWrite() //写
{
BOOL bOK = UpdateInternalVariables(); //Get Data
TRACE("m_dwValue %d m_dwAddr %d\n", m_dwValue, m_dwAddr);
if (bOK)
{
switch (m_ioSize) {
case BYTEIO:
DlPortWritePortUchar(m_dwAddr, (UCHAR)m_dwValue);
break;
case WORDIO:
DlPortWritePortUshort(m_dwAddr, (USHORT)m_dwValue);
break;
case DWORDIO:
DlPortWritePortUlong(m_dwAddr, m_dwValue);
break;
}
}
}
void CPortIODlg::OnRadioDec()
{
m_bDec = TRUE;
UpdateInternalVariables();
FormatUpdateDiaglog();
}
void CPortIODlg::OnRadioHex()
{
m_bDec = FALSE;
UpdateInternalVariables();
FormatUpdateDiaglog();
}
BOOL CPortIODlg::UpdateInternalVariables()
{
//Function is used to get data from Dialogs and translate it into decimal data
//First read string variables from dialog
UpdateData(TRUE);
//First handle the reading and formating of the address field
if(m_addr != "") //make sure a address value was entered
{
CString Hex = m_addr.Left( 2 );
if(Hex.Compare("0x" ) == 0) //Was a hex number entered
{
sscanf(m_addr,"0x%x", &m_dwAddr);
}
else //a decimal number
{
sscanf(m_addr,"%d", &m_dwAddr);
}
//Perform range checking on address
if (((m_dwAddr >= 256) && (m_dwAddr <=1023)) == FALSE) //Out of range
{
AfxMessageBox("Address entered out of range! Address must be >= 256 and <= 1023", MB_ICONSTOP);
return FALSE;
}
}
else //if no data is entered inform user and exit
{
AfxMessageBox("Address was not entered", MB_ICONSTOP);
return FALSE;
}
//Second handle the reading and formating of the Value field
if(m_value != "") //if address was entered update variable
{
CString Hex = m_value.Left( 2 );
if(Hex.Compare("0x" ) == 0) //Was a hex number entered
{
sscanf(m_value,"0x%x", &m_dwValue);
}
else //a decimal number
{
sscanf(m_value,"%d", &m_dwValue);
}
}
return TRUE;
}
void CPortIODlg::FormatUpdateDiaglog()
{
//Write Data Back to Screen
if (m_bDec)
{
m_value.Format("%d", m_dwValue);
m_addr.Format("%d", m_dwAddr);
}
else
{ //format Data into a Hexadecimal #
m_value.Format("0x%0000x", m_dwValue);
m_addr.Format("0x%0000x", m_dwAddr);
}
UpdateData(FALSE);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -