CEdit ChatDlg - yamaumima/hello.github.io GitHub Wiki
// ChDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Ch.h"
#include "ChDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChDlg dialog
CChDlg::CChDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChDlg)
DDX_Control(pDX, IDC_HIST, m_hist);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChDlg, CDialog)
//{{AFX_MSG_MAP(CChDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChDlg message handlers
BOOL CChDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//设置控件内缺省的文字格式
CHARFORMAT cf = {sizeof(cf)};
cf.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE;
cf.crTextColor = RGB(0,255,0);
cf.yHeight = 256;
strcpy(cf.szFaceName,"黑体");
m_hist.SetDefaultCharFormat(cf);
m_hist.SetWindowText("【系统】欢迎进入MFC聊天室\r\n");
//为将要输入的文字设置格式
int nLen = m_hist.GetWindowTextLength();
m_hist.SetSel(nLen,nLen);
cf.dwMask = CFM_COLOR|CFM_FACE|CFM_BOLD|CFM_UNDERLINE|CFM_SIZE;
cf.crTextColor = RGB(255,0,0);
strcpy(cf.szFaceName,"宋体");
cf.dwEffects =CFE_BOLD|CFE_UNDERLINE;
m_hist.SetWordCharFormat(cf);
//追加文字
m_hist.ReplaceSel("【提示】警惕聊托,请勿透漏联系方式\r\n");
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CChDlg::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
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CChDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChDlg::OnOK()
{
CHARFORMAT cf = {sizeof(cf)};
cf.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD|CFM_UNDERLINE;
cf.crTextColor = RGB(255,0,255);
cf.yHeight = 200;
cf.dwEffects = CFE_BOLD|CFE_UNDERLINE;
strcpy(cf.szFaceName,"楷体_GB2312");
int nLen = m_hist.GetWindowTextLength();
m_hist.SetSel(nLen,nLen);
COleDateTime time = COleDateTime::GetCurrentTime();
m_hist.ReplaceSel("你对 xxx 说:(" +time.Format("%H:%M:%S") + ")\r\n");
m_hist.SetSel(nLen,-1);
m_hist.SetSelectionCharFormat(cf);
nLen = m_hist.GetWindowTextLength();
m_hist.SetSel(nLen,-1);
CString str;
GetDlgItemText(IDC_INPUT,str);
m_hist.ReplaceSel(str+"\r\n");
cf.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_UNDERLINE|CFM_BOLD;
cf.crTextColor = RGB(0,0,255);
strcpy(cf.szFaceName,"楷体_GB2312");
cf.yHeight = 250;
cf.dwEffects = 0;
m_hist.SetSel(nLen,-1);
m_hist.SetSelectionCharFormat(cf);
m_hist.SetSel(-1,-1);
// CDialog::OnOK();
}