
{
WORD wVersionRequested
WSADATA wsaData
char name[255]
CString ip
PHOSTENT hostinfo
wVersionRequested = MAKEWORD(2, 0)
if (WSAStartup(wVersionRequested, &wsaData) == 0)
{
if(gethostname(name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list)
}
}
WSACleanup( )
}
// AfxMessageBox(name)//name里是本机名
// AfxMessageBox(ip) //ip中是本机IP
m_IPAddress = ip// m_IPAddress是IP控件对应的变量,ip是Edit控件对应的变量
// m_IP.SetAddress(255, 86, 255, 68)// 直接设置控件里显示的值
// 本段代码:已知IP Address控件里显示的值,转换为CString格式
/*
// 下面代码实现:把IP Address控件里的值转化为 CString格式
unsigned char *pIP
CString strIP
DWORD dwIP
m_IP.GetAddress(dwIP)// m_IP为IP Address控件对应的变量
pIP = (unsigned char*)&dwIP
strIP.Format("%u.%u.%u.%u",*(pIP+3), *(pIP+2), *(pIP+1), *pIP)
MessageBox(strIP)*/
/*
// 下面代码实现:把IP Address控件里的值转化为 CString格式
BYTE f0, f1, f2, f3
m_IP.GetAddress(f0, f1, f2, f3)
CString m_addr
m_addr.Format("%d%s%d%s%d%s%d", f0, ".", f1, ".", f2, ".", f3)
MessageBox(m_addr)*/
/*
// 下面代码实现:把IP Address控件里的值转化为 CString格式
BYTE IPByte[4]
m_IP.GetAddress(IPByte[0], IPByte[1], IPByte[2], IPByte[3])
CString strIP = ""
char temp1[10], temp2[10], temp3[10], temp4[10]
itoa(IPByte[0], temp1, 10)
itoa(IPByte[1], temp2, 10)
itoa(IPByte[2], temp3, 10)
itoa(IPByte[3], temp4, 10)
strIP += temp1
strIP += "."
strIP += temp2
strIP += "."
strIP += temp3
strIP += "."
strIP += temp4
MessageBox(strIP)*/
/*
// 下面代码实现:把IP Address控件里的值转化为 CString格式
CString strx
m_IP.GetWindowText(strx)
MessageBox(strx)*/
// 此段代码:用获取的IP地址值,显示到对话框里IP Address控件中
/* CString strIP
GetDlgItemText(IDC_EDIT_IPAddress, strIP)
m_IP.SetWindowText(strIP)*/
m_IP.SetWindowText(ip)// 把IP地址(CString类型)直接显示到IP Address控件中
UpdateData(FALSE)
}
IP控件也是常用的控件之一,也是最简单的一个控件,MFC对他的封装无非就是一个字符串的处理。使用起来也相当简便。
首先拖动控件到指定的地方,用ClassWizard为其关联一个变量,我们看到变量的类型任然是一个类,IPAddressCtrl的类,下面介绍几种常用的 *** 作:
1, 将IP地址输出到Ip控件显示:只需要短短两行代码,假定要显示的IP地址是192.168.1.1,则在确定的消息相应函数添加
Cpp代码
CString a="192.168.1.1"
m_ip.SetWindowText(a)
2. 将IP地址输入到程序中:
Cpp代码
BYTE a1,a2,a3,a4
m_ip.GetAddress(a1,a2,a3,a4)
CString str
str.Format("%d.%d.%d.%d",a1,a2,a3,a4)//这里的nf得到的值是IP值了.
MessageBox(str)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)