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

📄 examplepwdlg.cpp

📁 Many programs today suffer from the fact that their users have to connect using IP addresses. This i
💻 CPP
字号:
#include "stdafx.h"
#include "ExamplePW.h"
#include "ExamplePWDlg.h"
#include "Defines.h"
#include "AboutDlg.h"
#include "AwareNet.h"
#include "Divers.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CExamplePWDlg::CExamplePWDlg(CWnd* pParent, CAwareNet *ANet)
	: CDialog(CExamplePWDlg::IDD, pParent)
{
	/* The main dialog constructor.  Just setting the private
	   data members, loading the icon */

	AwareNet = ANet;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CExamplePWDlg::~CExamplePWDlg()
{
}

void CExamplePWDlg::DoDataExchange(CDataExchange* pDX)
{
	/* Work */
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CExamplePWDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()

	/*********************** AWARENET ***********************/
	/* Catch UpdateContacts messages from AwareNet          */
	ON_REGISTERED_MESSAGE(AwareNet_UpdateContactsMsg, OnUpdateFriendsList)

	/* The Add and Delete buttons, respectively */
	ON_COMMAND(IDC_ADDFRIEND, AddFriend)
	ON_COMMAND(IDC_DELETEFRIEND, DeleteFriend)
END_MESSAGE_MAP()

BOOL CExamplePWDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	/* Add the "About..." item to the system menu */
	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 we loaded in the constructor */
	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	/* Set up the list control; first load the online and offline
	   images */
	CImageList *ImageList = new CImageList();
	CBitmap *OnlineBmp, *OfflineBmp;
	CListCtrl *ListCtrl = (CListCtrl *)GetDlgItem(IDC_FRIENDLIST);

	OnlineBmp = new CBitmap();
	OfflineBmp = new CBitmap();
	OnlineBmp->LoadBitmap(IDB_ONLINE);
	OfflineBmp->LoadBitmap(IDB_OFFLINE);

	/* Create an image list out of them */
	ImageList->Create(24, 24, ILC_COLOR8, 2, 2);
	ImageList->Add(OnlineBmp, RGB(0, 0, 0));
	ImageList->Add(OfflineBmp, RGB(0, 0, 0));

	/* Set the friend list's image list as the one we just
	   created */
	ListCtrl->SetImageList(ImageList, LVSIL_SMALL);

	/*********************** AWARENET ***********************/
	/* Initialize AwareNet.  This must be done before 
	   anything else can be done.  We pass in our window
	   handle, and our TOS identifier                       */

	if(!AwareNet->Initialize(this->m_hWnd, EXAMPLEPW_SERVICE))
	{
		/* If we couldn't initialize AwareNet, we might as well
		   quit; there's nothing we can do.  This will only occur
		   when the user refuses to register himself; initialization
		   will work if we're offline */

		MessageBox("Could not initialize AwareNet", "Fatal error",
			MB_ICONSTOP | MB_OK);
		CDialog::OnCancel();
		return FALSE;
	}

	/* Now that we've initialized AwareNet, fill the friends
	   list */
	RefreshFriendsList();

	/* Sometimes the Z-order may change before the window displays, in
	   which case we need to reassert our dominance */
	SetForegroundWindow();

	return TRUE;
}

void CExamplePWDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	/* Just handling the "About..." item on the system menu */
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CExamplePWDlg::OnPaint() 
{
	/* Handling the iconic painting */
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		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;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CExamplePWDlg::OnQueryDragIcon()
{
	/* Work */
	return (HCURSOR)m_hIcon;
}

/*********************** AWARENET ***********************/
/* This function is where we go when we catch the
   UpdateContacts message; you can see the message
   map entry above.  Of course, you can name this
   function whatever you want.                          */
afx_msg LRESULT CExamplePWDlg::OnUpdateFriendsList(WPARAM w,
												   LPARAM l)
{
	/* The friends' list just changed.  This may or may not
	   be noticeable to the user; maybe someone went online
	   or offline, maybe a friend was added, or maybe just
	   an acquaintance changed status.  We don't know, so we
	   refill the friends list */
	RefreshFriendsList();
	return (LRESULT)1;
}

void CExamplePWDlg::AddFriend()
{
/*********************** AWARENET ***********************/
/* Add a friend.  We don't need to do anything except
   call the Add member function.  A common dialog box
   will be displayed allowing the user to add a friend.  */

	AwareNet->Add();

	return;
}

void CExamplePWDlg::DeleteFriend()
{
	int TargetID, Selected;

	/* Extract the ID number of the selected friend */
	CListCtrl *ListCtrl = (CListCtrl *)GetDlgItem(IDC_FRIENDLIST);;
	Selected = ListCtrl->GetNextItem(-1, LVNI_ALL | LVNI_SELECTED);

	if(Selected >= 0)
		TargetID = (int)ListCtrl->GetItemData(Selected);
	else
		return;

/*********************** AWARENET ***********************/
/* Delete the friend with ID TargetID.                  */

	AwareNet->Delete(TargetID);

	return;
}

void CExamplePWDlg::RefreshFriendsList()
{
	CListCtrl *ListCtrl;
	int Frnd = -1, Me;
	PersonalInfo PInfo;
	int i, newindex;

/*********************** AWARENET ***********************/
/* Loops over the friend list to add each friend to the
   friend list control.                                 */

	ListCtrl = (CListCtrl *)GetDlgItem(IDC_FRIENDLIST);

	if(!AwareNet || !ListCtrl)
		return;

	/* My ID number is saved in the integer Me */
	Me = AwareNet->GetMyID();

	/* Clear out the old information */
	ListCtrl->DeleteAllItems();

	/* Loop over the friends list */
	for(i = 0; (Frnd = AwareNet->GetFriend(i)) >= 0; i++)
	{
		/* If the one we're looking at is us, we just continue;
		   you don't want to see yourself on your friends list even
		   though, internally, your information is stored in the
		   friends list */

		if(Frnd == Me)
			continue;

		/* For the friend who is not me, I extract the profile
		   information */
		AwareNet->GetProfile(Frnd, &PInfo);

		/* If he's online, I show him with a green dot.  If not,
		   with a red dot.  Offline friends are further down
		   in the list to make reading easier */

		if(AwareNet->IsOnline(Frnd, EXAMPLEPW_SERVICE, true))
			newindex = ListCtrl->InsertItem(0, PInfo.Name, 0);
		else
			newindex = ListCtrl->InsertItem(i, PInfo.Name, 1);

		/* Store his ID number with his entry in the list control
		   so we can extract it later in case we want to delete him. */
		ListCtrl->SetItemData(newindex, (DWORD)Frnd);
	}

	/* If we actually added someone, select the first one */
	if(i > 1)
		SelectListItem(ListCtrl, 0);

	return;
}

⌨️ 快捷键说明

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