📄 newcontact.cpp
字号:
// NewContact.cpp : implementation file
//
#include "stdafx.h"
#include "Netmsg.h"
#include "NewContact.h"
#include "Ipclass.h"
#include "ContactView.h"
//#include <winsock2.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//The dialog for new contacts
CNewContact::CNewContact(CWnd* pParent, char *HostName)
: CDialog(CNewContact::IDD, pParent)
{
hostname = HostName;
//{{AFX_DATA_INIT(CNewContact)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
CNewContact::~CNewContact()
{
delete ImageList;
}
void CNewContact::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNewContact)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNewContact, CDialog)
//{{AFX_MSG_MAP(CNewContact)
ON_BN_CLICKED(IDC_RESOLVE, OnResolve)
ON_NOTIFY(NM_CLICK, IDC_COMPUTERS, OnClickComputers)
ON_NOTIFY(LVN_KEYDOWN, IDC_COMPUTERS, OnKeydownComputers)
ON_NOTIFY(NM_DBLCLK, IDC_COMPUTERS, OnDblclkComputers)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNewContact message handlers
void CNewContact::OnOK()
{
GetDlgItem(IDC_IPADDRESS)->GetWindowText(hostname, 64);
if (IP(hostname) == IP(0, 0, 0, 0))
{
MessageBox("The address you have chosen is invalid", "Invalid Address", MB_OK|MB_ICONSTOP);
return;
}
CDialog::OnOK();
}
void CNewContact::OnResolve()
{
struct hostent *host;
struct in_addr *ptr;
char buf[512];
CListCtrl *List = (CListCtrl *)GetDlgItem(IDC_COMPUTERS);
CIPAddressCtrl *IPAddr = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS);
List->SetSelectionMark(-1);
GetDlgItem(IDC_HOST)->GetWindowText(buf, sizeof(buf));
host = gethostbyname(buf);
if (!host)
{
MessageBox("Unable to resolve address", "DNS Error", MB_ICONSTOP|MB_OK);
IPAddr->SetWindowText("0.0.0.0");
return;
}
ptr = (struct in_addr *) host->h_addr_list[0];
sprintf(buf, "%d.%d.%d.%d", ptr->S_un.S_un_b.s_b1,
ptr->S_un.S_un_b.s_b2, ptr->S_un.S_un_b.s_b3, ptr->S_un.S_un_b.s_b4);
IPAddr->SetWindowText(buf);
}
BOOL CNewContact::OnInitDialog()
{
CDialog::OnInitDialog();
CenterWindow(GetDesktopWindow());
CList<CContact *, CContact *> *Contacts = &GetApp()->View->List;
CListCtrl *List = (CListCtrl *)GetDlgItem(IDC_COMPUTERS);
ImageList = new CImageList;
CBitmap *Computer = new CBitmap;
CBitmap *WithContact = new CBitmap;
Computer->LoadBitmap(IDB_COMPUTER);
WithContact->LoadBitmap(IDB_WITHCONTACT);
ImageList->Create(16, 16, ILC_COLOR16, 2, 2);
ImageList->Add(Computer, RGB(0, 0, 0,));
ImageList->Add(WithContact, RGB(0, 0, 0));
delete Computer;
delete WithContact;
List->SetImageList(ImageList, LVSIL_SMALL);
List->InsertColumn(0, "Computer Name", LVCFMT_LEFT, 100);
List->InsertColumn(1, "IP Address", LVCFMT_LEFT, 100);
List->InsertColumn(2, "Comment", LVCFMT_LEFT, 150);
List->InsertColumn(3, "Screen Name", LVCFMT_LEFT, 80);
CString strTemp;
int item = 0;
struct hostent *host;
struct in_addr *ptr; // To retrieve the IP Address
CIPAddressCtrl *IPAddr = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS);
DWORD dwScope = RESOURCE_CONTEXT;
NETRESOURCE *NetResource = NULL;
HANDLE hEnum;
WNetOpenEnum(dwScope, NULL, NULL, NULL, &hEnum);
WSADATA wsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
if (hEnum)
{
DWORD Count = 0xFFFFFFFF;
DWORD BufferSize = 2048;
LPVOID Buffer = new char[2048];
WNetEnumResource(hEnum, &Count, Buffer, &BufferSize);
NetResource = (NETRESOURCE*)Buffer;
char szHostName[200];
for (unsigned int i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++)
{
if (NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY)
{
if (NetResource->lpRemoteName)
{
CString strFullName = NetResource->lpRemoteName;
char buf[512];
if (0 == strFullName.Left(2).Compare("\\\\"))
strFullName = strFullName.Right(strFullName.GetLength()-2);
gethostname(szHostName, strlen( szHostName ));
host = gethostbyname(strFullName);
if (!host) continue;
ptr = (struct in_addr *) host->h_addr_list[0];
sprintf(buf, "%d.%d.%d.%d", ptr->S_un.S_un_b.s_b1,
ptr->S_un.S_un_b.s_b2, ptr->S_un.S_un_b.s_b3, ptr->S_un.S_un_b.s_b4);
CContact *Contact = GetApp()->View->GetContact(IP(buf));
List->InsertItem(item++, strFullName, Contact ? 1 : 0);
List->SetItemText(item - 1, 1, buf);
List->SetItemText(item - 1, 2, NetResource->lpComment);
if (Contact)
{
List->SetItemText(item - 1, 3, Contact->GetScreenName());
}
else
{
List->SetItemText(item - 1, 3, "(Not Added)");
}
}
}
}
delete Buffer;
WNetCloseEnum(hEnum);
}
WSACleanup();
return TRUE;
}
void CNewContact::OnClickComputers(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = 0;
}
void CNewContact::OnKeydownComputers(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CNewContact::OnDblclkComputers(NMHDR* pNMHDR, LRESULT* pResult)
{
CListCtrl *List = (CListCtrl *)GetDlgItem(IDC_COMPUTERS);
CEdit *Host = (CEdit *)GetDlgItem(IDC_HOST);
CIPAddressCtrl *IPAddr = (CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS);
int mark = List->GetSelectionMark();
char buf[512];
List->GetItemText(mark, 1, buf, sizeof(buf));
IPAddr->SetWindowText(buf);
List->GetItemText(mark, 0, buf, sizeof(buf));
Host->SetWindowText(buf);
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -