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

📄 ip查询dlg.cpp

📁 IP查询源代码..原理简单..适合初学者学习
💻 CPP
字号:
// IP查询Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "IP查询.h"
#include "IP查询Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CIPDlg dialog

CIPDlg::CIPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIPDlg)
		// 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 CIPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX,IDC_IPLIST,m_ListCtrl);
	//{{AFX_DATA_MAP(CIPDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIPDlg, CDialog)
	//{{AFX_MSG_MAP(CIPDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDEXIT, OnExit)
	ON_BN_CLICKED(IDQUERY, OnQuery)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIPDlg message handlers

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

//  定义List控件风格
	DWORD ListStyle=m_ListCtrl.GetExtendedStyle();
	m_ListCtrl.SetExtendedStyle(ListStyle|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_FLATSB);
	m_ListCtrl.SetBkColor(RGB(255,255,255));    //设置List控件的背景色
	m_ListCtrl.SetTextBkColor(RGB(255,255,255)); 
	m_ListCtrl.SetTextColor(RGB(255,0,0));     //设置List控件中的文本颜色
	m_ImageList.Create(18,17,ILC_COLOR24|ILC_MASK,8,1);
//	将位图与List控件相关联	
	CBitmap cBmp;
	cBmp.LoadBitmap(IDB_BITMAP1);
	m_ImageList.Add(&cBmp,RGB(255,0,255));
	cBmp.DeleteObject();
    m_ListCtrl.SetImageList(&m_ImageList,LVSIL_SMALL);
	CString  String;
	String.LoadString(IDS_COMPUTERNAME);
	m_ListCtrl.InsertColumn(0,String,LVCFMT_LEFT,160,-1);
	String.LoadString(IDS_IPADDRESS);
	m_ListCtrl.InsertColumn(1,String,LVCFMT_LEFT,250,-1);
//  设置状态栏
	 m_wndStatusBar.Create(WS_CHILD|WS_VISIBLE|CCS_BOTTOM,CRect(0,0,0,0),this,102);
	int strPartDim[3]={175,-1};
	m_wndStatusBar.SetParts(2,strPartDim);
	String.LoadString(IDS_INTRODUCE);
	m_wndStatusBar.SetText(String,0,0);
	String.LoadString(IDS_COPYRIGHT);
	m_wndStatusBar.SetText(String,1,0);






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

void CIPDlg::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 CIPDlg::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 CIPDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CIPDlg::OnExit() 
{
	CDialog::OnCancel();  //退出
	
}




/***********************************查询模块****************************************/
/*                                                                                 */
/*                                 核心代码                                        */     
/*                                                                                 */
/*   功能说明:查询本机的名称和IP地址.                                             */ 
/*   日期:2005-10-24                                                              */ 
                                                                                 
void CIPDlg::OnQuery() 
{ 

/*查询时声音提示*/
  
  int CIRCLE;
  unsigned FREQUENCY[]={1000,800};
  unsigned DELAY[]={375,325,500,500,500,1000};
  for(CIRCLE=0;CIRCLE<10;CIRCLE++)
  Beep(FREQUENCY[CIRCLE],DELAY[CIRCLE]);
  
/*获取计算机名称*/
  char szHostName[128];      //将本机的名称存入一维数组,数组名称为szHostName
  struct hostent * pHost;	//定义结构体 hostent
  int i; 	                //定义变量i
  
    LVITEM lvi;
	lvi.mask=LVIF_IMAGE|LVIF_TEXT;
	
	lvi.iItem=0;
	lvi.iSubItem=0;
	lvi.iImage=0;

 if(gethostname(szHostName,128)==0)    //如果本机的名称查到,则将其名称送入List控件
{
     m_ListCtrl.InsertItem(lvi.iSubItem, szHostName,lvi.iImage);
	 
     pHost = gethostbyname(szHostName); 
 for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 	
	 {
		 	 LPCSTR IPAddress=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
	         m_ListCtrl.SetItemText(0,1,IPAddress);
     }
}
 else
 {  
	CString String;
	String.LoadString(IDS_COMPUTERNAME_ERROR);
    m_ListCtrl.InsertItem(lvi.iSubItem,String,lvi.iImage);
	String.LoadString(IDS_IPADDRESS_ERROR);
	m_ListCtrl.SetItemText(0,1,String);
 }
}



⌨️ 快捷键说明

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