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

📄 mainfrm.cpp

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

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

#include "MainFrm.h"
#include "NetmsgView.h"
#include "ConversationThread.h"
#include "Conversation.h"
#include "NetSocket.h"
#include "MainOptions.h"
#include "ColorOptions.h"

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

static UINT WM_TRAY = ::RegisterWindowMessage("CreateTrayIcon");

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_COMMAND(ID_IDRRIGHTCLICK_BLOCK, OnIdrrightclickBlock)
	ON_COMMAND(ID_IDRRIGHTCLICK_SENDINSTANTMESSAGE, OnIdrrightclickSendinstantmessage)
	ON_COMMAND(ID_FILE_NEWCONTACT, OnFileNewcontact)
	ON_COMMAND(ID_IDRRIGHTCLICK_DELETE, OnIdrrightclickDelete)
	ON_WM_TIMER()
	ON_COMMAND(ID_TOOLS_OPTIONS, OnToolsOptions)
	ON_COMMAND(ID_IDRTRAYMENU_RESTORE, OnIdrtraymenuRestore)
	ON_COMMAND(ID_APP_EXIT, OnAppExit)
	ON_COMMAND(ID_IDRRIGHTCLICK_SENDFILE, OnIdrrightclickSendfile)
	ON_WM_ACTIVATE()
	ON_WM_GETMINMAXINFO()
	ON_COMMAND(ID_RIGHTCLICKME_SETAWAYSTATUS, OnRightclickmeSetawaystatus)
	ON_COMMAND(ID_RIGHTCLICKME_SETAPPEAROFFLINESTATUS, OnRightclickmeSetappearofflinestatus)
	ON_COMMAND(ID_RIGHTCLICKME_CHANGESCREENNAME, OnRightclickmeChangescreenname)
	ON_COMMAND(ID_RIGHTCLICKME_SETNORMALSTATUS, OnRightclickmeSetnormalstatus)
	ON_COMMAND(ID_IDRTRAYMENU_QUIT, OnIdrtraymenuQuit)
	ON_WM_QUERYENDSESSION()
	ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
	ON_REGISTERED_MESSAGE(WM_TRAY, OnTrayMessage)
	ON_COMMAND(ID_FILE_CLOSE_WINDOW, OnFileCloseWindow)
	ON_COMMAND_RANGE(ID_CUSTOMMENU_START, ID_CUSTOMMENU_END, OnCustomMenu)

	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_APPACTIVATE, OnActivate)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
};

static UINT toolbar[] =
{
	ID_FILE_NEWCONTACT,
	ID_IDRRIGHTCLICK_DELETE
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	RightClickMenu.LoadMenu(IDR_RIGHTCLICK);

}

CMainFrame::~CMainFrame()
{
}

ATOM RegisterNotifyWnd();

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.SetButtons(toolbar, sizeof(toolbar) / sizeof(unsigned int)))
	{
		return -1;      // fail to create
	}
	
	SIZE szImage = {27, 24}, szButton = {35, 30};


	m_wndToolBar.SetSizes(szButton, szImage);
	m_wndToolBar.GetToolBarCtrl().AddBitmap(1, IDB_CONTACT);
	m_wndToolBar.GetToolBarCtrl().AddBitmap(2, IDB_CROSS);
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		return -1;      // fail to create
	}

	char buf[2048], *param; 

	strcpy(buf, GetCommandLine());
	strtok(buf, "\"");

	param = strtok(NULL, "");
	if (param)
	{
		param++;
		if (!stricmp(param, "/M"))
		{
			SetTimer(10, 500, 0);
		}
	}

	SetTimer(1, AfxGetApp()->GetProfileInt("Settings", "Pingtime", 120000), NULL);
	SetTimer(2, AfxGetApp()->GetProfileInt("Settings", "Reconnect", 120000), NULL);
	SetTimer(0, 1, 0);

	WINDOWPLACEMENT *wpl;
	unsigned int sz = sizeof(WINDOWPLACEMENT);
	if (AfxGetApp()->GetProfileBinary("Settings", "WindowPos", (LPBYTE *)&wpl, &sz))
	{
		SetWindowPlacement(wpl);
		delete [] wpl;
	}

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{

	WNDCLASS wnd;

	wnd.lpfnWndProc = ::DefWindowProc;
	wnd.style = NULL;
	wnd.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	wnd.hCursor = NULL;
	wnd.hInstance = AfxGetInstanceHandle();
	wnd.lpszClassName = "NetmsgFrameClass";
	wnd.hbrBackground = NULL;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.lpszMenuName = NULL;

	AfxRegisterClass(&wnd);

	cs.lpszClass = "NetmsgFrameClass";
	
	GetApp()->MainFrame = this;
	
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnClose() 
{
	Minimise();	
	ShowWindow(SW_HIDE);	
}

CStatusBar *CMainFrame::GetStatusBar()
{
	return &m_wndStatusBar;
}

void CMainFrame::Created(CNetmsgView *MainView)
{
	View = MainView;
}

void CMainFrame::OnIdrrightclickBlock() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_BLOCK);	
}

void CMainFrame::OnIdrrightclickSendinstantmessage() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_SEND);	
}


void CMainFrame::OnFileNewcontact() 
{
	View->ContactList.MenuCmd(NULL, MENU_NEWCONTACT);		
}

void CMainFrame::OnIdrrightclickDelete() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_DELETE);			
}

void CMainFrame::OnRightclickmeSetawaystatus() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_AWAY);
}

void CMainFrame::OnRightclickmeSetappearofflinestatus() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_APPEAROFFLINE);
}

void CMainFrame::OnRightclickmeChangescreenname() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_CHANGENAME);			
}

void CMainFrame::OnRightclickmeSetnormalstatus() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_SETNORMAL);
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	switch (nIDEvent)
	{
	case 0:
		SetWindowText("Network Messenger");
		TrayIcon(TRUE);
		KillTimer(0);
	break;
	case 1:
		SendToAllOnline(true, true, "PING");
		TrayIcon(TRUE);
		View->ContactList.SaveContacts();
		break;
	case 2:
		if (!GetApp()->View->Notify)
		GetApp()->View->NotifyOthers();
		break;
	case 10:
		Minimise();
		ShowWindow(SW_HIDE);
		KillTimer(10);
		break;
	}

	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::OnToolsOptions() 
{
	int PingTime, Reconnect, ShowIps, UsePopup, UseEmoticons;
	CString Startup;
	char szStartup[1024];

	ShowIps = AfxGetApp()->GetProfileInt("Settings", "ShowIps", 0);
	PingTime = AfxGetApp()->GetProfileInt("Settings", "Pingtime", 120000);
	Reconnect = AfxGetApp()->GetProfileInt("Settings", "Reconnect", 120000);
	UsePopup = AfxGetApp()->GetProfileInt("Settings", "UsePopup", 1);
	UseEmoticons = AfxGetApp()->GetProfileInt("Settings", "UseEmoticons", 1);
	Startup = AfxGetApp()->GetProfileString("Settings", "SecurityWarning", 
		"(M) Netmsg 1.2.2.0$CRLF $UL$CRLF");

	strcpy(szStartup, Startup);

	CMainOptions MainOptions(&View->ContactList, &PingTime, &Reconnect, &ShowIps, &UsePopup, &UseEmoticons, szStartup);
	CColorOptions ColorOptions;

	CString ScreenName;
	ScreenName = View->ContactList.Me.GetScreenName();

	KillTimer(1);
	KillTimer(2);

	CPropertySheet Options("Options", this, 0);
	Options.AddPage(&MainOptions);
	Options.AddPage(&ColorOptions);

	CList <PLUGIN *, PLUGIN *> &Plugins = GetApp()->View->Plugins;
	POSITION pos;
	pos = Plugins.GetHeadPosition();

	while (pos)
	{
		CPropertyPage* (__stdcall *NMGetPropertyPage)(void);
		PLUGIN *pPlugin = Plugins.GetNext(pos);
		NMGetPropertyPage = (CPropertyPage* (__stdcall *)(void))
		GetProcAddress(pPlugin->hHandle, "NMGetPropertyPage");
		if (!NMGetPropertyPage) continue;
		if (!NMGetPropertyPage()) continue;
		Options.AddPage(NMGetPropertyPage());
	}

	Options.DoModal();

	AfxGetApp()->WriteProfileInt("Settings", "UsePopup", UsePopup);
	AfxGetApp()->WriteProfileInt("Settings", "Pingtime", PingTime);
	AfxGetApp()->WriteProfileInt("Settings", "Reconnect", Reconnect);
	AfxGetApp()->WriteProfileInt("Settings", "ShowIps", ShowIps);
	AfxGetApp()->WriteProfileInt("Settings", "UseEmoticons", UseEmoticons);
	AfxGetApp()->WriteProfileString("Settings", "SecurityWarning", szStartup);

	View->ContactList.ShowIps = ShowIps ? true : false;
	View->ContactList.BuildList();

	SetTimer(1, PingTime, 0);
	SetTimer(2, Reconnect, 0);
}

void CMainFrame::Minimise()
{
	if (minimised)
	{
		ShowWindow(SW_HIDE);
	}
	else
	{
		ShowWindow(SW_SHOW);
	}
}

void CMainFrame::Restore()
{
	HICON Icon;
	Icon = (HICON) AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	ShowWindow(SW_RESTORE);
	SetForegroundWindow();
	SetIcon(Icon, FALSE);
	SetIcon(Icon, TRUE);
	minimised = FALSE;
}

void CMainFrame::TrayIcon(BOOL add)
{
	HICON Icon;
	Icon = (HICON) AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	NOTIFYICONDATA nid;
	CString pcstr;
	nid.cbSize = sizeof(NOTIFYICONDATA);
	nid.hIcon =	Icon;
	nid.hWnd = AfxGetMainWnd()->GetSafeHwnd();
	nid.uCallbackMessage = WM_TRAY;
	nid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
	nid.uID = 1;
	AfxGetMainWnd()->GetWindowText(pcstr);
	_tcscpy(nid.szTip, pcstr);
	::Shell_NotifyIcon(add ? NIM_ADD : NIM_DELETE, &nid);
}

LRESULT CMainFrame::OnTrayMessage(WPARAM wParam, LPARAM lParam)
{
	UINT msg = (UINT)lParam;
	switch (msg)
	{
	case WM_LBUTTONDBLCLK:
		Restore();
		break;
	case WM_RBUTTONUP:
		CPoint pos;
		CMenu theMenu;
		CWnd* pTarget = AfxGetMainWnd();
		CMenu menu;
		theMenu.LoadMenu(IDR_TRAY_MENU);
		CMenu *subMenu = (CMenu*)theMenu.GetSubMenu(0);
		GetCursorPos(&pos);
		menu.LoadMenu(IDR_TRAY_MENU);
		::SetMenuDefaultItem(menu, 0, TRUE);
		pTarget->SetForegroundWindow(); 
		::TrackPopupMenu(subMenu->GetSafeHmenu(), 0, pos.x, pos.y, 0, 
		pTarget->GetSafeHwnd(), NULL);
		pTarget->PostMessage(WM_NULL, 0, 0);
		break;
	}
	return 1;
}

void CMainFrame::OnIdrtraymenuRestore() 
{
	Restore();	
}

void CMainFrame::Exit()
{
	char buf[64];

	AfxGetApp()->WriteProfileString("Colors", "Disabled", ultoa(GetApp()->crDisabled, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "LightItems", ultoa(GetApp()->crLightItems, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "LineFrames", ultoa(GetApp()->crLineFrames, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "NormalText", ultoa(GetApp()->crNormalText, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "NotifyText", ultoa(GetApp()->crNotifyText, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "BtnBack", ultoa(GetApp()->crBtnBack, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "BtnActive", ultoa(GetApp()->crBtnActive, buf, 10));
	AfxGetApp()->WriteProfileString("Colors", "BtnOver", ultoa(GetApp()->crBtnOver, buf, 10));


	PluginsApplicationExit();
	WINDOWPLACEMENT windowPlacement;
	GetWindowPlacement(&windowPlacement);
	if (windowPlacement.showCmd != SW_SHOWMINIMIZED)
	AfxGetApp()->WriteProfileBinary("Settings", "WindowPos", (BYTE*)&windowPlacement, sizeof(WINDOWPLACEMENT));	
	View->Destroy();
	TrayIcon(FALSE);
	CFrameWnd::OnClose();
}

void CMainFrame::OnAppExit() 
{
	Exit();	
}

void CMainFrame::OnIdrrightclickSendfile() 
{
	View->ContactList.MenuCmd(View->ContactList.GetSelectionMark(), MENU_SENDFILE);	
}

void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
	CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
	
	// TODO: Add your message handler code here
	
}

void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
	lpMMI->ptMinTrackSize.y = 160;
	lpMMI->ptMinTrackSize.x = 50;
	CFrameWnd::OnGetMinMaxInfo(lpMMI);
}

void CMainFrame::OnIdrtraymenuQuit() 
{
	Exit();
}

BOOL CMainFrame::OnQueryEndSession() 
{
	Exit();	
	return TRUE;
}

LRESULT CMainFrame::OnActivate(WPARAM wParam, LPARAM lParam)
{
	SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	ShowWindow(SW_RESTORE);
	SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	return 0L;
}

void CMainFrame::OnFileClose() 
{
}

void CMainFrame::OnFileCloseWindow() 
{
	Minimise();	
	ShowWindow(SW_HIDE);
}

void CMainFrame::OnCustomMenu(UINT nID)
{
	POSITION pos;
	pos = CustMenu.GetHeadPosition();
	while (pos)
	{
		CMENU *Menu = CustMenu.GetNext(pos);
		if (Menu->nItem == nID)
		{
			if (Menu->lpfn)
			Menu->lpfn();
		}
	}
}

unsigned int CMainFrame::AppendMainMenu(int whichMenu, int nMenu, char *szText, void (__stdcall *lpfn)(void))
{
	int ret;
	CMenu *Menu = GetMenu();
	CMenu *SubMenu;

	switch (whichMenu)
	{
	case AM_MAIN:
		SubMenu = Menu->GetSubMenu(nMenu);
		break;
	case AM_RIGHTCLICKME:
		SubMenu = RightClickMenu.GetSubMenu(1);
		break;
	case AM_RIGHTCLICK:
		SubMenu = RightClickMenu.GetSubMenu(0);
		break;
	}

	if (!SubMenu) return 0;
	SubMenu->AppendMenu(szText ? MF_STRING : MF_SEPARATOR, ID_CUSTOMMENU_START + CustMenu.GetCount(), szText);
	CMENU *NewMenu = new CMENU;
	NewMenu->lpfn = lpfn;
	NewMenu->nItem = ret = ID_CUSTOMMENU_START + CustMenu.GetCount();
	CustMenu.AddTail(NewMenu);
	return ret;
}

BOOL CMainFrame::DeleteMainMenu(int whichMenu, int nMenu, unsigned int nItem)
{
	POSITION pos;
	BOOL ret;
	pos = CustMenu.GetHeadPosition();
	while (pos)
	{
		CMENU *cMenu = CustMenu.GetNext(pos);
		if (nItem == cMenu->nItem)
		{
			CMenu *Menu = GetMenu();
			CMenu *SubMenu;

			switch (whichMenu)
			{
			case AM_MAIN:
				SubMenu = Menu->GetSubMenu(nMenu);
				break;
			case AM_RIGHTCLICKME:
				SubMenu = RightClickMenu.GetSubMenu(1);
				break;
			case AM_RIGHTCLICK:
				SubMenu = RightClickMenu.GetSubMenu(0);
				break;
			}			
			if (!SubMenu) return FALSE;
			ret = SubMenu->RemoveMenu(nItem, MF_BYCOMMAND);
			delete Menu;
			CustMenu.RemoveAt(CustMenu.Find(cMenu));
		}
	}
	return ret;
}

void RedrawEntireApplication()
{
	GetApp()->MainFrame->RedrawWindow();
	GetApp()->MainView->ColorUpdate();

	CList<CContact *, CContact *> &Contacts = GetApp()->View->List;
	POSITION pos;
	pos = Contacts.GetHeadPosition();
	while (pos)
	{
		CContact *Contact = Contacts.GetNext(pos);
		if (Contact->IsConvWindowOpen())
		{
			Contact->pThread->Conv->ColorUpdate();
		}
	}

}

⌨️ 快捷键说明

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