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

📄 qts_qqdlg.cpp

📁 本程序是模仿QQ聊天程序
💻 CPP
字号:
// QTS_QQDlg.cpp : implementation file
//

#include "stdafx.h"
#include "QTS_QQ.h"
#include "QTS_QQDlg.h"
#include "chat.h"
#include "TransFileDlg.h" 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// 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
	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)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQTS_QQDlg dialog

CQTS_QQDlg::CQTS_QQDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQTS_QQDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQTS_QQDlg)
	m_name = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQTS_QQDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQTS_QQDlg)
	DDX_Control(pDX, IDC_SENDTEXT, m_sendctr);
	DDX_Control(pDX, IDC_CHAT, m_chat);
	DDX_Text(pDX, IDC_NAME, m_name);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQTS_QQDlg, CDialog)
	//{{AFX_MSG_MAP(CQTS_QQDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_SENDFILE, OnSendfile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQTS_QQDlg message handlers

BOOL CQTS_QQDlg::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
	Serial.Open(m_Port,1200);
	m_receive="";
	SetTimer(1000,1,NULL);
	if(m_name=="")
		m_name="无名大虾";
	m_sendctr.SetFocus();
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQTS_QQDlg::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 CQTS_QQDlg::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);
		m_sendctr.SetFocus();
	}
	else
	{
//		CDC dc;
//		dc.CreateDC(
//		dc.SetTextColor(RGB(0,0,128));
//		dc.SetBkColor(RGB(255,255,255));	
//		CRect rrect;
//		m_chat.GetClientRect(rrect);
//		dc.FillSolidRect(rrect,RGB(255,255,255));
		m_sendctr.SetFocus();
		CDialog::OnPaint();
	}
}

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

void CQTS_QQDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	CString timestr,head;
	SYSTEMTIME CurTime;
	GetLocalTime(&CurTime);
	timestr.Format("%d:%d:%d",CurTime.wHour,CurTime.wMinute,CurTime.wSecond);
	char strend;
	strend=char(255);
	UpdateData();
	if(m_sendctr.GetWindowTextLength()==0)
	{
		AfxMessageBox("对不起,您不能发送空信息!");
		return;
	}
	m_sendctr.GetWindowText(m_send,m_sendctr.GetWindowTextLength()+1);
	m_send[m_sendctr.GetWindowTextLength()+1]=char(0);
	chat+=m_name;
	chat+="说:  ";
	chat+=timestr;
	chat+="\r\n";
	chat+=m_send;
	chat+="\r\n\r\n";
	m_chat.SetWindowText(chat);
	Serial.SendData(m_name,m_name.GetLength());
	Serial.SendData("说:  ",6);
	Serial.SendData(timestr,timestr.GetLength());
	Serial.SendData("\r\n",2);
	Serial.SendData(m_send,m_sendctr.GetWindowTextLength());
	Serial.SendData("\r\n",2);
	Serial.SendData(&strend,1);
	m_sendctr.SetWindowText("");
	m_sendctr.SetFocus();
}

void CQTS_QQDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	char str;
	int min,max;
	for(int n=0;n<Serial.ReadDataWaiting();n++)
	{
		str=Serial.GetByte();
		if(str==char(0))
			break;
		if(str==char(1))
		{
			OnReceive();
			break;
		}
		if(str!=char(255))
			m_receive+=str;
		else
		{
			m_receive+="\r\n";
			chat+=m_receive;
			m_chat.SetWindowText(chat);
			m_receive="";
		}
	}
	m_chat.UpdateData(FALSE);
	m_chat.GetScrollRange(SB_VERT,&min,&max);
	m_chat.SetScrollPos(SB_VERT,max);
	m_chat.LineScroll(m_chat.GetLineCount());
	CDialog::OnTimer(nIDEvent);
}

BOOL CQTS_QQDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{ 
		case VK_ESCAPE:
			m_sendctr.SetFocus ();
			break;
		case '\r':
			if(::GetKeyState(VK_CONTROL) < 0)
			{
				OnSend();
				pMsg->wParam=NULL;
			}
			break;
		default:
			m_sendctr.SetFocus ();
			break;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CQTS_QQDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	int min,max;
	chat="";
	m_chat.SetWindowText("");
	m_chat.GetScrollRange(SB_VERT,&min,&max);
	m_chat.SetScrollPos(SB_VERT,min);
	m_chat.LineScroll(0);
	UpdateData(FALSE);
	m_sendctr.SetFocus();
}

void CQTS_QQDlg::OnSendfile() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1000);
	CTransFileDlg FileDlg;
	FileDlg.Serial=&Serial;
	FileDlg.ifsend=true;
	FileDlg.DoModal();
	SetTimer(1000,1,NULL);
}
void CQTS_QQDlg::OnReceive(void)
{
	char filename[100]={0};
	char str0=char(0);
	KillTimer(1000);
	if(AfxMessageBox("收到文件传送请求,是否接收?",MB_YESNO)==IDYES)
	{
		Serial.ReadData(filename,Serial.ReadDataWaiting());  //get filename
		CTransFileDlg FileDlg;
		FileDlg.Serial=&Serial;
		FileDlg.m_filename=filename;
		FileDlg.ifsend=false;
		FileDlg.DoModal();
	}
	else
	{
		Serial.SendData(&str0,1);
		Serial.ReadData(filename,Serial.ReadDataWaiting());  //get filename in order to clear buffer
	}
	SetTimer(1000,1,NULL);
}

⌨️ 快捷键说明

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