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

📄 netmsg.cpp

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

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

#include "MainFrm.h"
#include "NetmsgDoc.h"
#include "NetmsgView.h"
#include "Conversation.h"

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

static const CLSID clsid =
{ 0xc2bf18fc, 0xbdc4, 0x45d6, { 0x86, 0x3a, 0x9c, 0x24, 0xdb, 0x4b, 0x28, 0xfd } };

/////////////////////////////////////////////////////////////////////////////
// CNetmsgApp

BEGIN_MESSAGE_MAP(CNetmsgApp, CWinApp)
	//{{AFX_MSG_MAP(CNetmsgApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	// Standard print setup command
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetmsgApp construction

CNetmsgApp::CNetmsgApp()
{
	dwOsVer = 0;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CNetmsgApp object

CNetmsgApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CNetmsgApp initialization

BOOL CNetmsgApp::InitInstance()
{
	CheckExistingWindow();

	if (!AfxOleInit())
	{
		AfxMessageBox("OLE Init failed");
		return FALSE;
	}

	AfxEnableControlContainer();

	if (!AfxSocketInit())
	{
		AfxMessageBox("Windows sockets initialization failed.");
		return FALSE;
	}

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	SetRegistryKey(_T("Netmsg"));

	LoadStdProfileSettings();

	//have the colors available REAL eairly

	GetApp()->crDisabled = strtoul(AfxGetApp()->GetProfileString("Colors", "Disabled", "13424598"), NULL, 10);
	GetApp()->crLightItems = strtoul(AfxGetApp()->GetProfileString("Colors", "LightItems", "16244694"), NULL, 10);
	GetApp()->crLineFrames = strtoul(AfxGetApp()->GetProfileString("Colors", "LineFrames", "11880704"), NULL, 10);
	GetApp()->crNormalText = strtoul(AfxGetApp()->GetProfileString("Colors", "NormalText", "0"), NULL, 10);
	GetApp()->crNotifyText = strtoul(AfxGetApp()->GetProfileString("Colors", "NotifyText", "15715253"), NULL, 10);
	GetApp()->crBtnBack = strtoul(AfxGetApp()->GetProfileString("Colors", "BtnBack", "15793151"), NULL, 10);
	GetApp()->crBtnOver = strtoul(AfxGetApp()->GetProfileString("Colors", "BtnOver", "2927840"), NULL, 10);
	GetApp()->crBtnActive = strtoul(AfxGetApp()->GetProfileString("Colors", "BtnActive", "16711680"), NULL, 10);

	SetOsInfo();

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CNetmsgDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CNetmsgView));
	AddDocTemplate(pDocTemplate);

	m_server.ConnectTemplate(clsid, pDocTemplate, FALSE);
	COleTemplateServer::RegisterAll();
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)
	{
		return TRUE;
	}

	m_server.UpdateRegistry(OAT_DISPATCH_OBJECT);
	COleObjectFactory::UpdateRegistryAll();
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
	//look for an existing window first
	char buf[1024];

	::GetClassName(hwnd, buf, sizeof(buf));

	if (!strcmp(buf, "NetmsgFrameClass"))
	{
		SendMessage(hwnd, WM_APPACTIVATE, 0, 0);
		exit(0);
	}
	return TRUE;
}

void CNetmsgApp::CheckExistingWindow()
{
	EnumWindows(EnumWindowsProc, NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	afx_msg void OnVisithome();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_BN_CLICKED(IDC_VISITHOME, OnVisithome)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CNetmsgApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CNetmsgApp message handlers


char *FormatBytes(unsigned long bytes)
{
	static char ret[64];
	if (bytes < 1000)
	{
		sprintf(ret, "%d Bytes", bytes);
		return ret;
	}
	if (bytes < 1000000)
	{
		sprintf(ret, "%.2f KB", (float) bytes / 1000);
		return ret;
	}
	if (bytes < 1000000000) 
	{
		sprintf(ret, "%.2f MB", (float) bytes / 1000000);
		return ret;
	}
	else
	{
		sprintf(ret, "%.2f GB", (float) bytes / 1000000000);
		return ret;
	}
	return NULL;
}

void nopath(char *to, const char *from)

{
	const char *s = from;
	if (s[strlen(s) - 1] == '\\') 
{
		to[0] = 0;
		return;
	}
	if (!strchr(s, '\\'))
	{
		while (*s) 
		*to++ = *(s++);
		*to = 0;
		return;
	}
	while (*s) s++;
	while (*s != '\\') s--;
	s++;
	while (*s) 
	*to++ = *(s++);
	*to = 0;
}

int uital2(void *buf, size_t len, char *ret)
{
	unsigned int pos = len, total = 0;
	BYTE *data = (BYTE *)buf;
	*ret = 0;
	while (pos--)
	{
		char ch[4];
		sprintf(ch, "%02X", *data++);
		strcat(ret, ch);
		total++;
	}
	return total;
}

int uifal2(void *buf, size_t len, char *in)
{
	unsigned int pos = len, cpos = 0, total = 0;
	BYTE *data = (BYTE *)buf;
	if ((strlen(in) / 2) < len) return 0; 
	while (pos--)
	{
		char ch[4];
		ch[0] = *(in + cpos);
		ch[1] = *(in + cpos + 1);
		ch[2] = 0;
		cpos += 2;
		*data++ = (BYTE)strtoul(ch, NULL, 16);
		total++;
	}
	return total;
}

char *nstrdup(const char *txt)
{
	char *ret = new char [strlen(txt) + 1];
	strcpy(ret, txt);
	return ret;
}

int strrpl(char *lpszBuf, const char *lpszOld, const char *lpszNew)
{
	// can't have empty or NULL lpszOld

	int nSourceLen;
	if (!lpszOld) nSourceLen = 0;
	else nSourceLen = strlen(lpszOld);
	if (nSourceLen == 0)
		return 0;
	int nReplacementLen = strlen(lpszNew);

	// loop once to figure out the size of the result string
	int nCount = 0;
	char *lpszStart = lpszBuf;
	char *lpszEnd = lpszBuf + strlen(lpszBuf);
	char *lpszTarget;
	while (lpszStart < lpszEnd)
	{
		while ((lpszTarget = strstr(lpszStart, lpszOld)) != NULL)
		{
			nCount++;
			lpszStart = lpszTarget + nSourceLen;
		}
		lpszStart += strlen(lpszStart) + 1;
	}

	// if any changes were made, make them
	if (nCount > 0)
	{
		// if the buffer is too small, just
		//   allocate a new buffer (slow but sure)
		int nOldLength = strlen(lpszBuf); 
		int nNewLength =  nOldLength + (nReplacementLen-nSourceLen)*nCount;

		// else, we just do it in-place
		lpszStart = lpszBuf;
		lpszEnd = lpszBuf + strlen(lpszBuf); 

		// loop again to actually do the work
		while (lpszStart < lpszEnd)
		{
			while ( (lpszTarget = strstr(lpszStart, lpszOld)) != NULL)
			{
				int nBalance = nOldLength - int(lpszTarget - lpszBuf + nSourceLen);
				memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
					nBalance * sizeof(char));
				memcpy(lpszTarget, lpszNew, nReplacementLen * sizeof(char));
				lpszStart = lpszTarget + nReplacementLen;
				lpszStart[nBalance] = '\0';
				nOldLength += (nReplacementLen - nSourceLen);
			}
 			lpszStart += strlen(lpszStart) + 1;
		}
	}
	return nCount;
}

void CNetmsgApp::SetOsInfo()
{
	OSVERSIONINFO osvi;
	memset(&osvi, 0, sizeof(OSVERSIONINFO));

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	GetVersionEx(&osvi);

	switch (osvi.dwPlatformId)
	{
		case VER_PLATFORM_WIN32s:
		{
			dwOsVer |= WIN_WIN32S;
			break;
		}
		case VER_PLATFORM_WIN32_WINDOWS:
		{
			dwOsVer |= WIN_WIN32;

			if (osvi.dwMinorVersion == 0) 
			{
				dwOsVer |= WIN_WIN95;
			} 
			else if (osvi.dwMajorVersion == 4 && (osvi.dwBuildNumber & 0xFFFF) < 2250) 
			{
				dwOsVer |= WIN_WIN98;
			}
			else if (osvi.dwMajorVersion == 4 && (osvi.dwBuildNumber & 0xFFFF) >= 2250) 
			{
				dwOsVer |= WIN_WINME;
			}
			break;
		}
		case VER_PLATFORM_WIN32_NT:
		{
			dwOsVer |= WIN_WINNT;

			if (osvi.dwMajorVersion <= 4) 
			{
				dwOsVer |= WIN_WINNT4;
			}
			else if (osvi.dwMajorVersion == 5) 
			{
				if (osvi.dwMinorVersion == 0)
				{
					dwOsVer |= WIN_WIN2K;
				}
				else if (osvi.dwMinorVersion >= 1)
				{
					dwOsVer |= WIN_WINXP;
				}
			}
			break;
		}
	}
}

void CAboutDlg::OnVisithome() 
{
	ShellExecute(GetSafeHwnd(), "open", "http://homepages.ihug.co.nz/~sk8ie/matt/", NULL, NULL, SW_SHOWNORMAL);
}

⌨️ 快捷键说明

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