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

📄 netmsgview.cpp

📁 vc++6.0开发网络典型应用实例导航 1. 本光盘提供了本书中所有的实例源程序文件。 2. 附录文件夹下是Winsock 函数参考以及错误码列表
💻 CPP
字号:
// NetmsgView.cpp : implementation of the CNetmsgView class
//

#include "stdafx.h"
#include "Netmsg.h"

#include "NetmsgDoc.h"
#include "NetmsgView.h"
#include "ListenSocket.h"
#include "NetSocket.h"
#include "NotifyThread.h"
#include "MainFrm.h"
#include "CornerStatic.h"

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

#define OK_BUTTON 0x8010
#define MESSAGE_INPUT 0x8011

/////////////////////////////////////////////////////////////////////////////
// CNetmsgView

IMPLEMENT_DYNCREATE(CNetmsgView, CView)

BEGIN_MESSAGE_MAP(CNetmsgView, CView)
	//{{AFX_MSG_MAP(CNetmsgView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(OK_BUTTON, OnMessageOK)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetmsgView construction/destruction

CNetmsgView::CNetmsgView()
{
	// TODO: add construction code here
	HasMessage = false;
	FontNormal.CreateFont(14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial");
	BrushNormal.CreateSolidBrush(GetColor(COL_LINEFRAMES));
	BrushLight.CreateSolidBrush(GetColor(COL_LIGHTITEMS));
}

CNetmsgView::~CNetmsgView()
{
}

BOOL CNetmsgView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CNetmsgView drawing

void CNetmsgView::OnDraw(CDC* pDC)
{
	CNetmsgDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CNetmsgView printing

BOOL CNetmsgView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CNetmsgView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CNetmsgView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CNetmsgView diagnostics

#ifdef _DEBUG
void CNetmsgView::AssertValid() const
{
	CView::AssertValid();
}

void CNetmsgView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CNetmsgDoc* CNetmsgView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNetmsgDoc)));
	return (CNetmsgDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CNetmsgView message handlers

int CNetmsgView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	GetApp()->MainView = this;

	MibII m;

	int rv = m.Init();
	if (rv < 0)
		return -1;

	NICCount = m.GetNICCount(true, true);
	if (NICCount > 0)
	{
		pNICInfo = new tSTRUCTNICINFO [NICCount];
		m.GetNICInfo(pNICInfo);
	}
	CRect rect;
	rect.SetRectEmpty();
	OK.Create("OK", WS_CHILD | BS_OWNERDRAW, rect, this, OK_BUTTON);
	MessageInput.Create(WS_CHILD | WS_VISIBLE, rect, this, MESSAGE_INPUT);
	OK.SetFont(&FontNormal);
	MessageInput.SetFont(&FontNormal);
	MessageInput.SetLimitText(64);

	MessageText.Create("", WS_CHILD, rect, this);
	MessageText.SetFont(&FontNormal);
	MessageText.SetColors(GetColor(COL_NORMALTEXT), GetColor(COL_LIGHTITEMS));

	ContactList.CreateEx(WS_EX_ACCEPTFILES, "SysListView32", "", LVS_LIST |
	LVS_SINGLESEL | LVS_SHOWSELALWAYS | WS_CHILD | WS_TABSTOP, rect, this, CONTACT_LIST);
	ContactList.ShowWindow(SW_SHOW);
	CRect srect;
	srect.SetRectEmpty();
	CMainFrame *Frame = (CMainFrame *)GetParentFrame();
	Frame->Created(this);

	ContactList.Me.SetHost(IP(pNICInfo->IP[0], pNICInfo->IP[1], pNICInfo->IP[2], pNICInfo->IP[3]));
	//some point of reference

	ContactList.mainWnd = GetParentFrame();

	ContactList.BuildList();

	return 0;
}

void CNetmsgView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	if (cx > 0 && cy > 0)
	{
		ContactList.MoveWindow(8, HasMessage ? 52 : 16, cx - 16, HasMessage ? cy - 68 : cy - 32);	
		if (HasMessage)
		{
			MessageInput.MoveWindow(7, 12, cx - 42, 14);
			OK.MoveWindow(cx - 30, 11, 24, 16);
			MessageText.MoveWindow(8, 30, cx - 16, 14);
		}
	}
}


void CNetmsgView::Destroy()
{
	delete [] pNICInfo;
	ContactList.Destroy();
}

bool CNetmsgView::IsMyIp(IP Ip)
{
	int i;
	for (i = 1; i <= NICCount; i++)
	{
		tSTRUCTNICINFO *Info = &pNICInfo[i - 1];
		if (Ip == IP(Info->IP[0], Info->IP[1], Info->IP[2], Info->IP[3]))
			return true;
	}
	return false;
}

void CNetmsgView::PrepareMessage(bool bMessage, int nWhich)
{
	RECT rect;
	GetClientRect(&rect);
	nMessage = nWhich;
	CEdit *Reason = (CEdit *)GetDlgItem(MESSAGE_INPUT);
	if (bMessage)
	{
		HasMessage = true;
		Reason->SetWindowText("");
		OK.ShowWindow(SW_SHOW);
		MessageInput.MoveWindow(7, 12, rect.right - 42, 14);
		ContactList.MoveWindow(8, 52, rect.right - 16, rect.bottom - 68);	
		OK.MoveWindow(rect.right - 30, 11, 24, 16);
		MessageText.MoveWindow(8, 30, rect.right - 16, 14);
		MessageInput.ShowWindow(SW_SHOW);
		MessageText.ShowWindow(SW_SHOW);
		switch (nMessage)
		{
		case MESSAGE_AWAY:
			MessageText.SetWindowText("Type in your away reason");
			break;
		case MESSAGE_CHANGENAME:
			MessageText.SetWindowText("Enter a new screen name");
			MessageInput.SetWindowText(ContactList.Me.GetScreenName());
			break;
		}
		SendMessage(WM_PAINT, 0, 0);
		RedrawWindow();
		SetTimer(65535, 1, 0);
	}
	else
	{
		HasMessage = false;
		OK.ShowWindow(SW_HIDE);
		MessageInput.ShowWindow(SW_HIDE);
		MessageText.ShowWindow(SW_HIDE);
		SendMessage(WM_PAINT, 0, 0);
		ContactList.MoveWindow(8, 16, rect.right - 16, rect.bottom - 32);	
		RedrawWindow();
	}
}

void CNetmsgView::OnMessageOK()
{
	CEdit *Reason = (CEdit *)GetDlgItem(MESSAGE_INPUT);
	char buf[64];
	Reason->GetWindowText(buf, 64);
	switch (nMessage)
	{
		case MESSAGE_AWAY:
			ContactList.Me.SetAway(true, buf);
			break;
		case MESSAGE_CHANGENAME:
			if (!strcmp(buf, "")) 
			{
				MessageText.SetWindowText("Invalid screen name");
				return;
			}
			ContactList.ChangeMyName(buf);
			break;
	}
	PrepareMessage(false, nMessage);
}

HBRUSH CNetmsgView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	if (pWnd == &OK) return BrushLight;
	else if (pWnd == &MessageText)
	{
		pDC->SetBkColor(GetColor(COL_LIGHTITEMS));
		return BrushLight;
	}
	if (pWnd == &MessageInput)
	{
		return NULL;
	}
	return NULL;
}

void CNetmsgView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	RECT rect;
	GetClientRect(&rect);
	dc.FillSolidRect(0, 10, 4, rect.bottom - 20, GetColor(COL_LINEFRAMES));	
	dc.FillSolidRect(9, 0, rect.right - 19, 4, GetColor(COL_LINEFRAMES));	
	dc.FillSolidRect(rect.right - 4, 10, 4, rect.bottom - 20, GetColor(COL_LINEFRAMES));	
	dc.FillSolidRect(10, rect.bottom - 4, rect.right - 20, 4, GetColor(COL_LINEFRAMES));	

	if (HasMessage)
	{
		dc.FillSolidRect(10, 4, rect.right - 20, 6, GetColor(COL_LIGHTITEMS));
		dc.FillSolidRect(4, 10, rect.right - 8, 36, GetColor(COL_LIGHTITEMS));
		dc.FillSolidRect(6, 11, rect.right - 40, 16,  GetColor(COL_LINEFRAMES));
	}

	DrawCorner(&dc, 0, 0, GetColor(COL_LINEFRAMES), 1, HasMessage, GetColor(COL_LIGHTITEMS));
	DrawCorner(&dc, rect.right - 10, 0, GetColor(COL_LINEFRAMES), 2, HasMessage, GetColor(COL_LIGHTITEMS));
	DrawCorner(&dc, 0, rect.bottom - 10, GetColor(COL_LINEFRAMES), 3);
	DrawCorner(&dc, rect.right - 10, rect.bottom - 10, GetColor(COL_LINEFRAMES), 4);

	// Do not call CView::OnPaint() for painting messages
}

void CNetmsgView::OnTimer(UINT nIDEvent) 
{
	if (nIDEvent == 65535)
	{
		KillTimer(65535);
		MessageInput.SetFocus();
		int len = MessageInput.GetWindowTextLength();
		MessageInput.SetSel(0, len);
	}
	else 
		PluginsTimer(nIDEvent);

	CView::OnTimer(nIDEvent);
}

void CNetmsgView::ColorUpdate()
{
	BrushNormal.DeleteObject();
	BrushLight.DeleteObject();
	BrushNormal.CreateSolidBrush(GetColor(COL_LINEFRAMES));
	BrushLight.CreateSolidBrush(GetColor(COL_LIGHTITEMS));
	MessageText.SetColors(GetColor(COL_NORMALTEXT), GetColor(COL_LIGHTITEMS));
}

⌨️ 快捷键说明

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