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

📄 userverdlg.cpp

📁 聊天室的源代码程序
💻 CPP
字号:
// UServerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "UServer.h"
#include "UServerDlg.h"
#include "Transmission.h"

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

/////////////////////////////////////////////////////////////////////////////
// CColorEdit

CColorEdit::CColorEdit()
{
	m_FontColor = RGB(0, 0, 255);			//字体颜色 
	m_backgroundColor = RGB(255, 255, 255);		//背景颜色 
	m_brush.CreateSolidBrush(m_backgroundColor/*背景颜色*/);
}

CColorEdit::~CColorEdit()
{
}


BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
	//{{AFX_MSG_MAP(CColorEdit)
	ON_WM_CTLCOLOR_REFLECT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorEdit message handlers

HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	pDC->SetTextColor(m_FontColor);
	pDC->SetBkColor(m_backgroundColor);
	return (HBRUSH) m_brush.GetSafeHandle();
}

void CColorEdit::SetBkColor(COLORREF crBkgnd)
{
	m_backgroundColor = crBkgnd;
}
	
void CColorEdit::SetTextColor(COLORREF crText)
{
	m_FontColor = crText;
}


/////////////////////////////////////////////////////////////////////////////
// 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)
	virtual void OnOK();
	//}}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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUServerDlg dialog

CUServerDlg::CUServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUServerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUServerDlg)
	m_ERemoteIP = _T("");
	m_ESendMsg = _T("");
	m_bHideWnd=false;
	m_BroadcastFlag=false;
	m_MulticastFlag=false;
	m_JoinGroupFlag=false;
	m_bMsgNotice=false;
	m_PtoPFlag=false;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUServerDlg)
	DDX_Control(pDX, IDC_LIST_GroupMember, m_listGroupMember);
	DDX_Text(pDX, IDC_ERemoteIP, m_ERemoteIP);
	DDX_Text(pDX, IDC_ESendMsg, m_ESendMsg);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUServerDlg, CDialog)
	//{{AFX_MSG_MAP(CUServerDlg)
	ON_WM_VSCROLL_REFLECT()
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BSendMsg, OnBSendMsg)
	ON_BN_CLICKED(IDC_CBroadcast, OnCBroadcast)
	ON_BN_CLICKED(IDC_CJoinGroup, OnCJoinGroup)
	ON_BN_CLICKED(IDC_CPtoP, OnCPtoP)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_MsgNotice, OnMsgNotice)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUServerDlg message handlers

BOOL CUServerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	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 for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	CreateReceiveArea();

	m_pTrans=new CTransmission(this);
	m_pTrans->StartTransmission(8080);
	GetDlgItem(IDC_HostIP)->SetWindowText(m_pTrans->GetHostIP());
	SetForegroundWindow();
	CEdit *pEdit=(CEdit *)GetDlgItem(IDC_ReceiveMsg);
	pEdit->SetReadOnly();
	m_listGroupMember.AddString(m_pTrans->GetHostIP());
	m_listGroupMember.SetCurSel(m_listGroupMember.GetCount()-1);
	m_strReceiveMsg.Format("%38c%s\r\n\r\n",' ',"[欢迎使用我爱我Q 1.0版]");
	UpdateData(false);
	CString str("COME");
	m_pTrans->Broadcast(str,str.GetLength());

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CUServerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

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

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
//		CPaintDC dc(this);
//		dc.Rectangle(20,20,300,300);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CUServerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUServerDlg::OnBSendMsg() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CString error;
	CString str0="[",str="]:",strIP="225.225.225.225",strMsg(m_ESendMsg);
	if(strMsg.GetLength())
	{
		strMsg=m_pTrans->GetHostName()+str0+m_pTrans->GetSystemTime()+str+strMsg;
		if(m_BroadcastFlag)
		{
			if(!m_pTrans->Broadcast(strMsg,strMsg.GetLength()))
			{
				AfxMessageBox("Broadcast failed!!");
			}

		}

		if(m_MulticastFlag)
		{
			if(!m_pTrans->Multicast(strMsg,strMsg.GetLength(),strIP))
			{
				AfxMessageBox("Multicast failed!!");

			}

		}

		if(m_PtoPFlag)
		{
			if(!m_pTrans->PtoP(strMsg,strMsg.GetLength(),m_ERemoteIP))
			{
				AfxMessageBox("PtoP failed!!");

			}
	
		}
		if(!m_PtoPFlag&&!m_MulticastFlag&&!m_BroadcastFlag)
			AfxMessageBox("Choice a method for sending!!");
	}
	else
	{
		AfxMessageBox("Nothing is sent!!");
	}
	m_ESendMsg="";
	UpdateData(FALSE);
}

void CUServerDlg::OnCBroadcast() 
{
	// TODO: Add your control notification handler code here
	m_BroadcastFlag=!m_BroadcastFlag;
	UpdateData(FALSE);
}

void CUServerDlg::OnCJoinGroup() 
{
	// TODO: Add your control notification handler code here
	m_JoinGroupFlag=!m_JoinGroupFlag;
	CString strIP="225.225.225.225";
	if(m_JoinGroupFlag)
	{
		if(m_pTrans->JoinGroupActive(strIP))
		{
			AfxMessageBox("join group successfully!!");
			m_MulticastFlag=true;
			CString str;
			str.Format("%s[%s]: %s",m_pTrans->GetHostName(),m_pTrans->GetSystemTime(),"  Join our group!!");
			m_pTrans->Multicast(str,str.GetLength(),strIP);
		}
		else
		{
			AfxMessageBox("join group failed!!");

		}
	}
	else
	{
		CString str;
		str.Format("%s[%s]: %s",m_pTrans->GetHostName(),m_pTrans->GetSystemTime(),"Exit our group!! ");
		m_pTrans->Multicast(str,str.GetLength(),strIP);
		if(m_pTrans->ExitGroupActive(strIP))
		{
			AfxMessageBox("Exit group successfully!!");
			m_MulticastFlag=false;
		}
		else
		{
			AfxMessageBox("Exit group failed!!");
		}
	}
//	UpdateData(FALSE);
	
}

void CUServerDlg::OnCPtoP() 
{
	// TODO: Add your control notification handler code here
	m_PtoPFlag=!m_PtoPFlag;
	if(m_PtoPFlag)
	{
		GetDlgItem(IDC_ERemoteIP)->EnableWindow(true);//为可用
		m_ERemoteIP="192.168.80.232";
	}
	else
	{
		GetDlgItem(IDC_ERemoteIP)->EnableWindow(false);//默认为不可用
		m_ERemoteIP="";
	}
	UpdateData(FALSE);
	
}

void CUServerDlg::OnOK() 
{
	// TODO: Add extra validation here
	
//	CDialog::OnOK();
}
void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

BOOL CUServerDlg::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style =WS_POPUP;//使主窗口不可见
	cs.dwExStyle|=WS_EX_TOOLWINDOW;//不显示任务按钮

	return CDialog::PreCreateWindow(cs);
}


int CUServerDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	RegisterHotKey(m_hWnd,1001,MOD_ALT,'9');
	RegisterHotKey(m_hWnd,1002,MOD_ALT,'0');
//	AddTrayIcon();
	return 0;
}
/*
LRESULT CUServerDlg::OnHotkey(WPARAM wParam,LPARAM lParam)
{
	if (wParam==1001||wParam==1002)
	{
		ShowWindow(SW_SHOW);
		SetForegroundWindow();
	}
	return 0;
}


LRESULT CUServerDlg::OnRcv()
{
//	AfxMessageBox("message arrived!");
	ShowWindow(SW_SHOW);
	return 0;
}
*/

LRESULT CUServerDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch(message)
	{
		case WM_SYSCOMMAND:
			if(wParam==SC_MINIMIZE)
				{ 
					ShowWindow(SW_HIDE);
					AddTrayIcon();
					m_bHideWnd=!m_bHideWnd;
					return 0;
				} 
			break;
		case WM_HOTKEY: 
			if (wParam==1001)
			{
				if(m_bHideWnd)
				{
					ShowWindow(SW_SHOW);
					SetForegroundWindow();
					DeleteTrayIcon();
					m_bHideWnd=!m_bHideWnd;
				}
			}
			if (wParam==1002)
			{
				if(!m_bHideWnd)
				{
					ShowWindow(SW_HIDE);
					AddTrayIcon();
					m_bHideWnd=!m_bHideWnd;
				}
			}
			break;
		case WM_US_MSGARRIVE:
			if(m_bHideWnd)
			{
				ShowWindow(SW_SHOW);
				DeleteTrayIcon();
				m_bHideWnd=!m_bHideWnd;
			}
			SetForegroundWindow();
			break;
		case WM_US_NOTIFYICON: 
			if (lParam==WM_LBUTTONDBLCLK)
			{
				ShowWindow(SW_SHOW);
				SetForegroundWindow();
				DeleteTrayIcon();
			}
			break;
	}

	return CDialog::WindowProc(message, wParam, lParam);
}

void CUServerDlg::OnDestroy() 
{
	UnregisterHotKey(m_hWnd, 1001);
	UnregisterHotKey(m_hWnd, 1002);
	DeleteTrayIcon();
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}
void CUServerDlg::FontSet(CWnd *pWnd,LPCSTR strFont, int Size)
{
		CFont *font = new CFont;
		font->CreateFont(Size,0,0,0,500,0,0,0,
						ANSI_CHARSET,OUT_DEFAULT_PRECIS,
						CLIP_DEFAULT_PRECIS,
						DEFAULT_QUALITY,
						DEFAULT_PITCH|FF_DONTCARE,
						strFont); 
		pWnd->SetFont(font);

}

HBRUSH CUServerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
/*	if(nCtlColor==CTLCOLOR_DLG)
	{
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(255,0,0));
		HBRUSH m_hbrush=CreateSolidBrush(RGB(255,200,0));
		hbr=m_hbrush;
	}
*/
	if(nCtlColor==CTLCOLOR_EDIT)
	{
		if(pWnd->GetDlgCtrlID()==IDC_ESendMsg)
		{
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(0,0,255));
		HBRUSH m_hbrush=CreateSolidBrush(RGB(255,255,255));
		hbr=m_hbrush;
		}
		if(pWnd->GetDlgCtrlID()==IDC_ERemoteIP)
		{
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(255,0,0));
		HBRUSH m_hbrush=CreateSolidBrush(RGB(255,255,255));
		hbr=m_hbrush;
		}

	}
	if(nCtlColor==CTLCOLOR_STATIC)
	{
/*		if(pWnd->GetDlgCtrlID()==IDC_EDIT_ReceiveMsg)
		{
			pDC->SetBkMode(TRANSPARENT);
			pDC->SetTextColor(RGB(0,0,255));
			HBRUSH m_hbrush=CreateSolidBrush(RGB(255,255,255));
			hbr=m_hbrush;
		}
*/
		if(pWnd->GetDlgCtrlID()==IDC_HostIP)
		{
			pDC->SetBkMode(TRANSPARENT);
			pDC->SetTextColor(RGB(255,0,0));
		}
	}

	// TODO: Return a different brush if the default is not desired
		return hbr;
}

void CUServerDlg::VScroll()
{
	AfxMessageBox("fdkgjfkdg");
}

void CUServerDlg::OnMsgNotice() 
{
	// TODO: Add your control notification handler code here
	m_bMsgNotice=!m_bMsgNotice;
}

void CUServerDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	CString str("EXIT");
	m_pTrans->Broadcast(str,str.GetLength());
	CDialog::OnClose();
}

void CUServerDlg::AddTrayIcon()
{
	m_iconData.cbSize=sizeof(NOTIFYICONDATA);
	m_iconData.hWnd=m_hWnd;
	m_iconData.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;
	m_iconData.uID=IDR_MAINFRAME;
	m_iconData.uCallbackMessage=WM_US_NOTIFYICON;
	CString szToolTip=_T("我爱我Q");
	_tcscpy(m_iconData.szTip, szToolTip);
	HICON hIcon;
	hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_iconData.hIcon=hIcon;
	Shell_NotifyIcon(NIM_ADD,&m_iconData);
	if(hIcon)DestroyIcon(hIcon);
}

void CUServerDlg::DeleteTrayIcon()
{
	Shell_NotifyIcon(NIM_DELETE,&m_iconData);
}

void CUServerDlg::CreateReceiveArea()
{
	m_pEdit=new CColorEdit;
	RECT rect={10,40,415,222};
	m_pEdit->Create(ES_MULTILINE|ES_AUTOVSCROLL|WS_VSCROLL|WS_BORDER|WS_EX_DLGMODALFRAME|
		WS_EX_TRANSPARENT|WS_THICKFRAME|ES_READONLY|ES_RIGHT,rect,this,IDC_ReceiveMsg);
	m_pEdit->ShowWindow(true);

}
void CUServerDlg::SetReceiveText(CString szReceiveMsg)
{
	m_pEdit->SetWindowText(szReceiveMsg);

}

⌨️ 快捷键说明

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