⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winpingdlg.cpp

📁 VC各工程的源码集合
💻 CPP
字号:
// WinPingDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WinPing.h"
#include "WinPingDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CAboutDlg dialog used for App About
/////////////////////////////////////////////////////////////////////////////
// WINPING - Developed by Norm Almond, Acknowledgements to Chris Maunder & Lewis Napper
//
// MAINTANENCE LOG:
//
// Version 1.0 - Public Release
//
// Version 1.1 - Fix combo assert in debug
//             - Fix repeated string entries in combo
//             - Fix Ping times now correct

/////////////////////////////////////////////////////////////////////////////

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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinPingDlg dialog

CWinPingDlg::CWinPingDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWinPingDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWinPingDlg)
	m_strRetry = _T("");
	m_num = 0;
	m_Exit = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_nDir = 0;
	m_nPos = 0;
	m_bProgress = FALSE;
	
}

void CWinPingDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWinPingDlg)
	DDX_Control(pDX, IDC_SERVERS, m_cboServers);
	DDX_Control(pDX, IDC_SPIN1, m_spinner);
	DDX_Control(pDX, IDC_LIST, m_list);
	DDX_Text(pDX, IDC_RETRIES, m_strRetry);
	DDX_Text(pDX, IDC_NUM, m_num);
	DDX_Text(pDX, IDC_EXIT, m_Exit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWinPingDlg, CDialog)
	//{{AFX_MSG_MAP(CWinPingDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_PING, OnPing)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CONTINUE, OnContinue)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_WM_DESTROY()
	ON_MESSAGE(WM_MSG_STATUS,OnStatus)
	ON_MESSAGE(WM_PING_END,OnPingEnd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWinPingDlg message handlers

BOOL CWinPingDlg::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

	SetProp(this->m_hWnd,"WinPing",(HANDLE)1);

	CString strHost;
	CString str;

	//读取INI文件,获得上次ping过的主机地址
	int nEntrys = AfxGetApp()->GetProfileInt("MRU","Entries",0);
	for (int n=0;n < nEntrys;n++)
	{
		str.Format("Host#%d",n);
		strHost = AfxGetApp()->GetProfileString("MRU",str,"");
		m_cboServers.AddString(strHost);
		m_cboServers.SetCurSel(0);
	}

	//创建图标链表
	m_img.Create(IDB_LOG,16,1,RGB(255,0,255));
	//初始化控件
	CRect rc;
	CWnd* pWnd;

	pWnd= GetDlgItem(IDC_FROM);
	pWnd->GetWindowRect(rc);
	ScreenToClient(rc);

	m_ptFrom = rc.BottomRight();
	m_ptFrom.x+=5;

	pWnd= GetDlgItem(IDC_TO);
	pWnd->GetWindowRect(rc);
	ScreenToClient(rc);

	m_ptTo = CPoint(rc.left,rc.bottom);

	//设置列表框风格
	m_list.InsertColumn(0,"Status",LVCFMT_LEFT,800);

	m_list.SetImageList(&m_img, LVSIL_SMALL);

	m_list.GetClientRect(rc);
	m_list.SetColumnWidth(0,rc.Width());

	m_rcRect.left = m_ptFrom.x;
	m_rcRect.right = m_ptTo.x;
	m_rcRect.top = m_rcRect.bottom = m_ptFrom.y;
	m_rcRect.InflateRect(0,3);

	m_spinner.SetRange(1,32767);
	m_spinner.SetPos(2);

	UpdateData(TRUE);
	// TODO: Add extra initialization here
	m_bContinue=FALSE;
	m_num=1;
	UpdateData(0);


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWinPingDlg::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 CWinPingDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	if (IsIconic())
	{

		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
	{
		//画对话框顶部的动画
		dc.MoveTo(m_ptFrom);
		dc.LineTo(m_ptTo);
		if (m_bProgress)
		{
			
			//根据位置的变化
			CRect rc(m_nPos,m_ptTo.y,m_nPos,m_ptTo.y);
			rc.InflateRect(3,3);
			CBrush br(RGB(255,0,0));
			CBrush* pBrush = dc.SelectObject(&br);
			dc.Rectangle(rc);
			dc.SelectObject(pBrush);
		}

		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWinPingDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWinPingDlg::OnPing() 
{

	//更新数据
	UpdateData(TRUE);

	//使ping按钮,两个CComboBox控件无效
	EnableControls(FALSE);

	int nRetries = m_spinner.GetPos();


	int nCnt = m_list.GetItemCount();
	if (nCnt > 0)
	{
		m_list.InsertItem(nCnt,"",4);

	}

	CString strHost;
	
	GetDlgItem(IDC_SERVERS)->GetWindowText(strHost);

	//**************
	m_Exit=0;
	UpdateData(FALSE);
	//*************
	//开始ping
	m_pingThread.StartPing(nRetries,strHost,m_hWnd);	

	m_nPos = m_ptFrom.x;
	m_nDir = 20;
	//设定定时器
	SetTimer(0x1001,100,NULL);
	m_bProgress = TRUE;

}

//响应ping的过程中ping线程发过来的状态消息
LRESULT CWinPingDlg::OnStatus(WPARAM wParam, LPARAM lParam)
{
	char* pMsg = (char*) lParam;

	int nCnt = m_list.GetItemCount();
	
	int i = m_list.InsertItem(nCnt,pMsg,wParam);
	m_list.EnsureVisible(i,FALSE);
	if(!strcmp(pMsg,"Request Timed Out"))
		MessageBeep(1);
	delete pMsg;
	

	return 0;
}

//ping结束
LRESULT CWinPingDlg::OnPingEnd(WPARAM wParam, LPARAM lParam)
{
	EnableControls(TRUE);
	m_bProgress = FALSE;

	InvalidateRect(m_rcRect,TRUE);
	KillTimer(0x1001);
	if (lParam == 1)
	{
		CString strHost;
		
		GetDlgItem(IDC_SERVERS)->GetWindowText(strHost);

		//将已经ping过的地址加到列表框中
		if (m_cboServers.FindStringExact(-1,strHost) == CB_ERR)
			m_cboServers.AddString(strHost);

		GetDlgItem(IDC_SERVERS)->SetFocus();
	}


	if(m_bContinue && m_nIP<m_nStop)
	{
		m_nIP++;
		if(m_nIP==m_nStop)
		{
			m_bContinue=FALSE;
			m_num=m_nNum;
			GetDlgItem(IDC_NUM)->EnableWindow(TRUE);
			GetDlgItem(IDC_CONTINUE)->EnableWindow(TRUE);
			UpdateData(FALSE);
			return 0;
		}
		m_num--;
		UpdateData(FALSE);
		CString str;
		str.Format("192.168.0.%d",m_nIP);
		GetDlgItem(IDC_SERVERS)->SetWindowText(str);
		OnPing();
	}

	return 0;
}

//更新ping进度
void CWinPingDlg::OnTimer(UINT nIDEvent) 
{
	m_nPos += m_nDir;
	InvalidateRect(m_rcRect,TRUE);
	if (m_nPos > m_ptTo.x)
	{
		m_nDir = -20;
		m_nPos = m_ptTo.x - 5;
	}
	
	if (m_nPos < m_ptFrom.x)
	{
		m_nDir = +20;
		m_nPos = m_ptFrom.x + 5;
	}

	CDialog::OnTimer(nIDEvent);
}

//设置控件状态
void CWinPingDlg::EnableControls(BOOL bEnable)
{
	GetDlgItem(IDC_SERVERS)->EnableWindow(bEnable);
	GetDlgItem(IDC_RETRIES)->EnableWindow(bEnable);
	GetDlgItem(ID_PING)->EnableWindow(bEnable);	
}

//关闭窗口
void CWinPingDlg::OnCancel() 
{
	CString strHost;
	CString str;

	//写入ini文件
	int nCnt = m_cboServers.GetCount();
	AfxGetApp()->WriteProfileInt("MRU","Entries",nCnt);
	
	for (int n=0;n < nCnt;n++)
	{
		m_cboServers.GetLBText(n,strHost);
		str.Format("Host#%d",n);
		AfxGetApp()->WriteProfileString("MRU",str,strHost);
	}
			
	CDialog::OnCancel();
}

void CWinPingDlg::OnContinue() 
{
	UpdateData();
	CString strHost;	
	GetDlgItem(IDC_SERVERS)->GetWindowText(strHost);
	if(strHost.IsEmpty())
	{
		MessageBox("请输入起始IP!");
		return ;
	}
	GetDlgItem(IDC_NUM)->EnableWindow(FALSE);
	GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);

	int index=strHost.ReverseFind('.');
	strHost=strHost.Right(strHost.GetLength()-index-1);
	//MessageBox(strHost);
	m_nIP=atoi(strHost);
	m_nNum=m_num;
	m_nStop=m_nIP+m_num;
	m_bContinue=TRUE;
	OnPing();
	
}

void CWinPingDlg::OnStop() 
{
	if(m_bContinue)
	{
		m_bContinue=FALSE;
		m_num=m_nNum;
		CString str;
		str.Format("192.168.0.%d",m_nIP);
		GetDlgItem(IDC_SERVERS)->SetWindowText(str);
		
		GetDlgItem(IDC_SERVERS)->EnableWindow(1);
		GetDlgItem(IDC_RETRIES)->EnableWindow(1);
		GetDlgItem(ID_PING)->EnableWindow(1);	
		GetDlgItem(IDC_NUM)->EnableWindow(1);	
		GetDlgItem(IDC_CONTINUE)->EnableWindow(1);	
		UpdateData(FALSE);
	}
	else
	{
		m_Exit=1;
		UpdateData(FALSE);
	}
	
	
}

void CWinPingDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	RemoveProp(this->m_hWnd,"WinPing");	
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -