?? ewdpingdlg.cpp
字號:
// EWDPingDlg.cpp : implementation file
//
//////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "EWDPing.h"
#include "EWDPingDlg.h"
#include "PingOptionsDlg.h"
//////////////////////////////////////////////////////////////////////////////
#include "RegKey.h"
#include "SysTCPIP.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()
/////////////////////////////////////////////////////////////////////////////
// CEWDPingDlg dialog
CEWDPingDlg::CEWDPingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEWDPingDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEWDPingDlg)
m_Host = _T("");
m_LocalHost = _T("");
m_NameServer = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEWDPingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEWDPingDlg)
DDX_Control(pDX, IDC_TraceList, m_TraceList);
DDX_Text(pDX, IDC_DEST, m_Host);
DDX_Text(pDX, IDC_LocalHost, m_LocalHost);
DDX_Text(pDX, IDC_NameServer, m_NameServer);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEWDPingDlg, CDialog)
//{{AFX_MSG_MAP(CEWDPingDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDQUIT, OnQuit)
ON_BN_CLICKED(IDC_PingButton, OnPingButton)
ON_BN_CLICKED(IDC_TraceButton, OnTraceButton)
ON_BN_CLICKED(IDC_OptionsButton, OnOptionsButton)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_ClearButton, OnClearButton)
ON_MESSAGE(WSA_PING_ASYNC, OnPINGAsynch)
ON_BN_CLICKED(IDC_Stop, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEWDPingDlg message handlers
//////////////////////////////////////////////////////////////////////////
// //
// MESSAGE INTERRUPT PROCESSORS //
// //
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////
// //
// Initialize main dialog //
// //
//////////////////////////////////////////
//對話框初始化
BOOL CEWDPingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
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);
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
PingSocket.icmpSocket = INVALID_SOCKET;
PingSent = FALSE;
FetchWinsockSettings(); // 獲得機器名、域名的屬性.
if (!InitSockets()) // 如果初始化失敗則退出
CDialog::OnCancel();
InitImageList(); // 設定列表框圖標
pIcmpBuffer = (LPSTR)icmpBuffer;// 設定緩沖區
return TRUE;
}
//////////////////////////////////////////////////
// //
// Control menu and max/min processor //
// //
//////////////////////////////////////////////////
void CEWDPingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
//////////////////////////////////////////
// //
// Repaint window //
// //
//////////////////////////////////////////
void CEWDPingDlg::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();
}
}
//////////////////////////////////////////
// //
// Called when icon is moving //
// //
//////////////////////////////////////////
HCURSOR CEWDPingDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//////////////////////////////////////////////////////
// //
// PING Asynchronous Message Processor //
// //
// Responds to received ICMP messages //
// Updates the Trace window with the round //
// trip time and sequence, //
// Stops the timer, //
// If not TRACING, exits. //
// //
// If rcvSockAddr = TraceTarget, displays //
// host reached and exits. //
// If icmpMaxHops exceeded, displays host not //
// reached and exits. (icmpMaxHops is set //
// in EWDPing Options Dialog). //
// Displays next animated icon picture, //
// increments icmpHops and requests next //
// PING //
// //
//////////////////////////////////////////////////////
LRESULT CEWDPingDlg::OnPINGAsynch(WPARAM wParam, LPARAM lParam)
{
int WSAEvent = WSAGETSELECTEVENT(lParam);
int WSAError = WSAGETSELECTERROR(lParam);
if (WSAError)
{
CString ErrorMessage;
ErrorMessage.Format("WSAAsynch Event # %u, Error: %u",
WSAEvent,
WSAError);
MessageBox (ErrorMessage,
"CEWDPingDlg::OnPINGAsynch",
MB_OK|MB_SYSTEMMODAL);
}
StopTimer();
switch (WSAEvent)
{
case FD_READ:
if (PingSocket.Receive(pIcmpBuffer, sizeof icmpBuffer) == SOCKET_ERROR)
{
if (!PingSent)
return 0;
PingSocket.DisplayError ("FD_READ",
"CEWDPingDlg::OnWSAAsynch");
}
else
{
DWORD PingRoundTrip = PingSocket.icmpPingReceivedAt - PingSocket.icmpPingSentAt;
if (PingSocket.icmpRoundTripTime > PingRoundTrip)
PingSocket.icmpRoundTripTime = PingRoundTrip;
if (PingSent)
{
UpdateTrace();
if (!icmpTracing)
{
m_TraceList.InsertItem (m_TraceList.GetItemCount(),
"",
Icon_BlueArrow);
SetTraceFocus (m_TraceList.GetItemCount()-1,
0);
DisplayTrace ("","","Host Reached");
}
}
}
PingSent = FALSE;
if (icmpTracing)
{
PingSocket.icmpHops++;
if ( (PingSocket.icmpHops >= PingSocket.icmpMaxHops) ||
(TraceTarget.s_addr == PingSocket.rcvSockAddr.sin_addr.s_addr ))
{
EndTrace();
m_TraceList.InsertItem (m_TraceList.GetItemCount(),
"",
Icon_BlueArrow);
SetTraceFocus (m_TraceList.GetItemCount()-1,
0);
DisplayTrace ("","","HOST Reached");
}
else
{
ChangeIconState();
SendPing();
}
}
break;
case FD_WRITE:
break;
default:
break;
}
return 0;
}
//////////////////////////////////////////////////////
// //
// End Trace Operation //
// //
// Set ICON to default, and force screen to //
// be updated. Reset icmpTracing so next //
// operation will be PING. //
// //
//////////////////////////////////////////////////////
void CEWDPingDlg::EndTrace(void)
{
CStatic * pDlgWnd;
m_hIcon = AfxGetApp()->LoadIcon(IDI_CommLink1);
pDlgWnd=(CStatic*)GetDlgItem(IDC_CommLink);
pDlgWnd->SetIcon(m_hIcon);
if(IsIconic())
{
InvalidateRect(NULL,TRUE);
}
icmpTracing = FALSE;
}
//////////////////////////////////////////////////////////////////////////
// //
// TIMER SECTION //
// //
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// //
// Process Timer Interrupt //
// //
// If time-out period has not elapsed, increment //
// icmpIntervalCount, change the animated icon's state //
// and exit. //
// //
// Otherwise, stop the timer, reset icon to default, //
// set receive address to null, and update the trace //
// display to show that the timer has expired. //
// //
//////////////////////////////////////////////////////////////
//計時器事件
void CEWDPingDlg::OnTimer(UINT id)
{
CString Message;
CStatic * pDlgWnd;
//如果沒有超時則不斷改變圖標狀態,并將計時變量增加
if (icmpIntervalCount < icmpMaxIntervals)
{
ChangeIconState ();
icmpIntervalCount++;
return;
}
StopTimer();
PingSent = FALSE;
m_hIcon = AfxGetApp()->LoadIcon(IDI_CommLink1);
pDlgWnd=(CStatic*)GetDlgItem(IDC_CommLink);
pDlgWnd->SetIcon(m_hIcon);
if(IsIconic())
{
InvalidateRect(NULL,TRUE);
}
PingSocket.rcvSockAddr.sin_addr.s_addr = NULL;
PingSocket.icmpRoundTripTime = PingSocket.icmpPingTimer * icmpMaxIntervals;
UpdateTrace ();
if (icmpTracing)
SendPing();
}
//////////////////////////////////////////
// //
// Start the Timer //
// //
// Start the timer and set TimerActive //
// //
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -