📄 testipdlg.cpp
字号:
// TestIPDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestIP.h"
#include "TestIPDlg.h"
#include <winsock2.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)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CTestIPDlg dialog
CTestIPDlg::CTestIPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestIPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestIPDlg)
m_nIPNum = 0;
m_sIPString = _T("");
m_nShortHost = 0;
m_nShortNetwork = 0;
m_nIntHost = 0;
m_nIntNetwork = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestIPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestIPDlg)
DDX_Text(pDX, IDC_EDT_NUM, m_nIPNum);
DDX_Text(pDX, IDC_EDT_IPSTR, m_sIPString);
DDX_Text(pDX, IDC_EDT_SHORT_HOST, m_nShortHost);
DDX_Text(pDX, IDC_EDT_SHORT_NETWORK, m_nShortNetwork);
DDX_Text(pDX, IDC_EDT_INT_HOST, m_nIntHost);
DDX_Text(pDX, IDC_EDT_INT_NETWORK, m_nIntNetwork);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestIPDlg, CDialog)
//{{AFX_MSG_MAP(CTestIPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_H2N, OnBtnH2n)
ON_BN_CLICKED(IDC_BTN_N2H, OnBtnN2h)
ON_BN_CLICKED(IDC_BTN_INT_H2N, OnBtnIntH2n)
ON_BN_CLICKED(IDC_BTN_INT_N2H, OnBtnIntN2h)
ON_BN_CLICKED(IDC_BTN_IP_INT2STR, OnBtnIpInt2str)
ON_BN_CLICKED(IDC_BTN_IP_INT2STR2, OnBtnIpInt2str2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestIPDlg message handlers
BOOL CTestIPDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestIPDlg::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 CTestIPDlg::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 CTestIPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void IPulong2string(unsigned long ulIP, char outBuf[16])
{
//memset(outbuf, 16);
unsigned short add1, add2, add3, add4;
add1 = (unsigned short)( ulIP & 255);
add2 = (unsigned short)((ulIP>>8) & 255);
add3 = (unsigned short)((ulIP>>16) & 255);
add4 = (unsigned short)((ulIP>>24) & 255);
sprintf(outBuf, "%d.%d.%d.%d", add4, add3, add2, add1);
}
bool CheckIPStr(const char *pIP)
{
// IP地址长度最大为15,最小为7
if (pIP == NULL || strlen(pIP) > 15 || strlen(pIP) < 7)
{
return false;
}
char a;
int dot = 0; // Dot count
int a3, a2, a1, a0, i = 0;
char str[16];
strcpy(str, pIP);
a3 = a2 = a1 = a0 = -1;
while (a = str[i++])
{
if ((a == ' ') || (a == '.') || ((a >= '0') && (a <= '9')))
{
if (a == '.')
++dot;
}
else
{
return false;
}
}
if (dot != 3)
{
return false;
}
else
{
sscanf(str, "%d.%d.%d.%d", &a3, &a2, &a1, &a0);
if ((a0 > 255) || (a1 > 255) || (a2 > 255) || (a3 > 255))
{
return false;
}
if ((a0 < 0) || (a1 < 0) || (a2 < 0) || (a3 < 0))
{
return false;
}
}
return true;
}
unsigned long IPstring2ulong(const char *pIP)
{
if (!CheckIPStr(pIP))
{
return 0;
}
int a3, a2, a1, a0;
unsigned long dwReturn = 0;
sscanf(pIP, "%d.%d.%d.%d", &a3, &a2, &a1, &a0);
dwReturn += a3 << 24;
dwReturn += a2 << 16;
dwReturn += a1 << 8;
dwReturn += a0;
return dwReturn;
}
void CTestIPDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CTestIPDlg::OnBtnH2n()
{
UpdateData(TRUE);
if (m_nShortHost != 0)
{
unsigned short nHost = m_nShortHost;
unsigned short nNetwork = htons(nHost);
m_nShortNetwork = nNetwork;
UpdateData(FALSE);
}
}
void CTestIPDlg::OnBtnN2h()
{
UpdateData(TRUE);
if (m_nShortNetwork != 0)
{
unsigned short nNetwork = m_nShortNetwork;
unsigned short nHost = ntohs(nNetwork);
m_nShortHost = nHost;
UpdateData(FALSE);
}
}
void CTestIPDlg::OnBtnIntH2n()
{
UpdateData(TRUE);
if (m_nIntHost != 0)
{
m_nIntNetwork = htonl(m_nIntHost);
UpdateData(FALSE);
}
}
void CTestIPDlg::OnBtnIntN2h()
{
UpdateData(TRUE);
if (m_nIntNetwork != 0)
{
m_nIntHost = ntohl(m_nIntNetwork);
UpdateData(FALSE);
}
}
void CTestIPDlg::OnBtnIpInt2str()
{
UpdateData(TRUE);
if (m_nIPNum != 0)
{
char sBuf[16] = {0};
IPulong2string(m_nIPNum, sBuf);
m_sIPString = sBuf;
UpdateData(FALSE);
}
}
void CTestIPDlg::OnBtnIpInt2str2()
{
UpdateData(TRUE);
m_sIPString.TrimLeft();
m_sIPString.TrimRight();
if (m_sIPString.GetLength() > 0)
{
m_nIPNum = IPstring2ulong(m_sIPString);
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -