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

📄 gethostips.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
#include "stdafx.h"

#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Net;

__gc class MyForm : public Form
{
public:
  MyForm();

protected:
  TextBox *m_ptxtHostName;
  Button *m_pbtnGetIPAddresses;
  ListBox *m_plbxIPAddresses;

protected:
  void ButtonClick(Object *sender, System::EventArgs* e);
};

int __stdcall WinMain()
{
  // Instantiate a MyForm object.
  MyForm* pMyForm = new MyForm();

  // Create and display the form and 
  // start the message loop to control messaging.
  Application::Run(pMyForm);

  return 0;
}

MyForm ::MyForm()
{
  // Set the form's caption.
  Text = "Managed C++ Windows Forms App";

  // Set border style, turn off maximize button, and set the form size.
  FormBorderStyle=FormBorderStyle::Fixed3D;
  MaximizeBox=false;
  ClientSize = System::Drawing::Size(300,200);

  // Create the Host Name text object and set its properties.
  m_ptxtHostName = new TextBox();
  m_ptxtHostName->Text="Enter desired DNS Host Name here";
  m_ptxtHostName->Size = System::Drawing::Size(275,50);
  m_ptxtHostName->Location = Point(5,5);

  // Add the label to the form.
  Controls->Add(m_ptxtHostName);

  // Now add a button control and set its relevant properties.
  m_pbtnGetIPAddresses = new Button();
  m_pbtnGetIPAddresses->Text = "Get IP Address(es)";
  m_pbtnGetIPAddresses->Location = Point(5,60);
  m_pbtnGetIPAddresses->Size = System::Drawing::Size(120,30);

  // Add a button handler function.
  m_pbtnGetIPAddresses->add_Click(new EventHandler(this,&MyForm::ButtonClick));
  
  // Add the button to the form.
  Controls->Add(m_pbtnGetIPAddresses);

  // Create the IP Listbox.
  m_plbxIPAddresses = new ListBox();
  m_plbxIPAddresses->Size = System::Drawing::Size(150,100);
  m_plbxIPAddresses->ScrollAlwaysVisible = true;
  m_plbxIPAddresses->Location = Point(130,60);

  // Add the ListBox to the form.
  Controls->Add(m_plbxIPAddresses);
}

void MyForm::ButtonClick(Object *sender, System::EventArgs *e)
{
  ListBox::ObjectCollection* pItems = m_plbxIPAddresses->Items;
  pItems->Clear();

  IPHostEntry* IPHost = Dns::Resolve(m_ptxtHostName->Text); 
	IPAddress* address[] = IPHost->AddressList; 

  for(int i = 0; i < address.Length; i++) 
  {
		pItems->Add((address->Item[i])->ToString());
  }
}

⌨️ 快捷键说明

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