ValidIPTestDlg.cpp
上传用户:qzzxgm
上传日期:2009-12-14
资源大小:1882k
文件大小:6k
- // ValidIPTestDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ValidIPTest.h"
- #include "ValidIPTestDlg.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()
- /////////////////////////////////////////////////////////////////////////////
- // CValidIPTestDlg dialog
- CValidIPTestDlg::CValidIPTestDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CValidIPTestDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CValidIPTestDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CValidIPTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CValidIPTestDlg)
- DDX_Control(pDX, IDC_NETWORKID, m_ctrlNetworkID);
- DDX_Control(pDX, IDC_MASK, m_ctrlMask);
- DDX_Control(pDX, IDC_IP, m_ctrlIP);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CValidIPTestDlg, CDialog)
- //{{AFX_MSG_MAP(CValidIPTestDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_TEST, OnTest)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CValidIPTestDlg message handlers
- BOOL CValidIPTestDlg::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 CValidIPTestDlg::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 CValidIPTestDlg::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 CValidIPTestDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CValidIPTestDlg::OnTest()
- {
- DWORD dwIP,dwMask,dwNetworkID;
- //取得用户输入的IP地址
- m_ctrlIP.GetAddress(dwIP);
- //取得掩码数据
- m_ctrlMask.GetAddress(dwMask);
- //取得网络ID值
- m_ctrlNetworkID.GetAddress(dwNetworkID);
- //如果IP和掩码数据按位做与操作,结果不等于网络ID
- //则说明该IP地址是无效的
- /*
- if ((dwIP & dwMask) != dwNetworkID)
- {
- AfxMessageBox("这是一个无效的IP地址,因为该IP地址和掩码按位与后不等于网络ID");
- return;
- }
- */
- //将掩码数据取反,根据得到结果中1的个数和位置
- //就知道IP地址中主机ID有几位,在哪些位置上
- DWORD dw = ~dwMask;
- //如果主机位全是1,则这个IP地址是无效的。
- if ((dw & dwIP) == dw)
- {
- AfxMessageBox("这是一个无效的IP地址,因为主机位全是1");
- return;
- }
- //如果主机位全是0,则这个IP地址是无效的
- if ((dw & dwIP) == 0)
- {
- AfxMessageBox("这是一个无效的IP地址,因为主机位全是0");
- return;
- }
-
- //另外,每个网段的开始和结束的那段IP也是无效的
- DWORD dwStart1,dwEnd1,dwStart2,dwEnd2;
- //计算本网段开始一段的无效IP地址范围
- dwStart1 = dwNetworkID + 0;
- dwEnd1 = dwNetworkID + dw;
- //计算本网段结束一段的无效IP地址
- DWORD dwBase;
- if (dw < 0x100)
- {
- dwBase = (0x100 -1) & ~dw;
- }
- else if (dw >= 0x100 && dw < 0x10000)
- {
- dwBase = (0x10000 -1) & ~dw;
- }
- else if (dw >= 0x10000 && dw < 0x1000000)
- {
- dwBase = (0x1000000 -1) & ~dw;
- }
- else
- {
- //因为A、B、C、D、E五类网址的掩码最高位都是255
- //因此dw不可能比0x1000000大的
- AfxMessageBox("指定的掩码数据是错误的");
- return;
- }
- dwStart2 = dwNetworkID + dwBase;
- dwEnd2 = dwNetworkID + dwBase + dw;
- //判断给定的IP是否在这两个范围之内,如果在则是无效的
- if ((dwIP >= dwStart1 && dwIP <= dwEnd1)||
- (dwIP >= dwStart2 && dwIP <= dwEnd2))
- {
- AfxMessageBox("给定的IP地址是无效的,每个网段的最开始的那段和结束的那段IP都不可以使用");
- return;
- }
- AfxMessageBox("这是一个合法的IP地址");
-
- }