sendviewre.cpp
来自「一个简易的聊天室程序」· C++ 代码 · 共 236 行
CPP
236 行
// ChatViewRE.cpp : implementation file
//
#include "stdafx.h"
#include "SendViewRE.h"
#include "Resource.h"
#include "ChatClientDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <afxrich.h>
#define JOINING_CHAT 1
#define LEAVING_CHAT 2
#define SENDING_CHATTERS_LIST 3
#define SENDING_NICKNAME 4
#define NORMAL_MESSAGE 5
#define DUPLICATE_NICKNAME 6
/////////////////////////////////////////////////////////////////////////////
// CChatViewRE
IMPLEMENT_DYNCREATE(CSendViewRE, CRichEditView)
CSendViewRE::CSendViewRE()
{
}
CSendViewRE::~CSendViewRE()
{
}
BEGIN_MESSAGE_MAP(CSendViewRE, CRichEditView)
//{{AFX_MSG_MAP(CChatViewRE)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChatViewRE drawing
void CSendViewRE::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
/////////////////////////////////////////////////////////////////////////////
// CChatViewRE diagnostics
#ifdef _DEBUG
void CSendViewRE::AssertValid() const
{
CRichEditView::AssertValid();
}
void CSendViewRE::Dump(CDumpContext& dc) const
{
CRichEditView::Dump(dc);
}
CChatClientDoc* CSendViewRE::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CChatClientDoc)));
return (CChatClientDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChatViewRE message handlers
void CSendViewRE::OnInitialUpdate()
{
CRichEditView::OnInitialUpdate();
// CHARFORMAT cf;
m_cf.cbSize = sizeof(CHARFORMAT);
m_cf.dwMask =
CFM_BOLD|
// CFM_ITALIC|
// CFM_UNDERLINE|
// CFM_STRIKEOUT|
CFM_SIZE|
CFM_COLOR;
// CFM_OFFSET|
// CFM_PROTECTED;
m_cf.dwEffects = CFE_AUTOCOLOR | FW_NORMAL;
m_cf.yHeight = 200; //10pt
// m_cf.wWeight = FW_NORMAL;
m_cf.yOffset = 0;
m_cf.crTextColor = RGB(0, 0, 0);
m_cf.bCharSet = 0;
m_cf.bPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
strcpy(m_cf.szFaceName, "Arial");
m_cf.dwMask |= CFM_FACE;
GetRichEditCtrl().SetDefaultCharFormat(m_cf);
}
void CSendViewRE::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if ((nChar != VK_RETURN) || (nRepCnt!=1))
{
CRichEditView::OnChar(nChar, nRepCnt, nFlags);
return;
}
else
{
CChatClientDoc* pDoc = (CChatClientDoc*)GetDocument();
ASSERT_VALID(pDoc);
CString strText;
GetRichEditCtrl().GetWindowText(strText);
pDoc->SendMsg(strText, NORMAL_MESSAGE, true, m_cf);
strText=_T("");
GetRichEditCtrl().SetWindowText(strText);
GetRichEditCtrl().SetSel(9,9);
}
}
void CSendViewRE::SetupFont()
{
LOGFONT lf;
CHOOSEFONT csf = {0};
LONG yPerInch;
HDC hdc;
m_cf.cbSize = sizeof(CHARFORMAT);
hdc = (HDC)GetDC();
yPerInch = GetDeviceCaps(hdc, LOGPIXELSY);
GetRichEditCtrl().SendMessage(EM_GETCHARFORMAT, (WPARAM) true,
(LPARAM) &m_cf);
csf.lStructSize = sizeof(csf);
csf.hwndOwner = this->GetSafeHwnd();
csf.hDC = 0;
csf.lpLogFont = &lf;
csf.Flags =
CF_EFFECTS |
CF_SCREENFONTS |
CF_INITTOLOGFONTSTRUCT |
CF_LIMITSIZE;
csf.nSizeMin = 1;
csf.nSizeMax = yHeightCharPtsMost;
csf.rgbColors = m_cf.crTextColor;
csf.lpszStyle = NULL;
csf.nFontType = REGULAR_FONTTYPE | SCREEN_FONTTYPE;
lf.lfHeight = -(INT) ((m_cf.yHeight * yPerInch) / 1440);
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = (m_cf.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
lf.lfItalic = (m_cf.dwEffects & CFE_ITALIC) ? true : false;
lf.lfUnderline = (m_cf.dwEffects & CFE_UNDERLINE) ? true : false;
lf.lfStrikeOut = (m_cf.dwEffects & CFE_STRIKEOUT) ? true : false;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DRAFT_QUALITY;
lf.lfCharSet = m_cf.bCharSet;
lf.lfPitchAndFamily = m_cf.bPitchAndFamily;
_tcscpy(lf.lfFaceName, m_cf.szFaceName);
if(!ChooseFont(&csf))
return;
m_cf.cbSize = sizeof(CHARFORMAT);
// don't change read-only bit
m_cf.dwMask = CFM_SIZE | CFM_EFFECTS | CFM_COLOR | CFM_FACE | CFM_CHARSET;
// Allow only upto 16 size font (360 == 16)
m_cf.yHeight = (LONG) (csf.iPointSize * 2 > 360 ? 360 : csf.iPointSize * 2);
m_cf.dwEffects = CFM_EFFECTS;
if(lf.lfWeight < FW_BOLD)
m_cf.dwEffects &= ~CFE_BOLD;
if(!lf.lfItalic)
m_cf.dwEffects &= ~CFE_ITALIC;
if(!lf.lfUnderline)
m_cf.dwEffects &= ~CFE_UNDERLINE;
if(!lf.lfStrikeOut)
m_cf.dwEffects &= ~CFE_STRIKEOUT;
m_cf.crTextColor = csf.rgbColors;
m_cf.bCharSet = lf.lfCharSet;
m_cf.bPitchAndFamily = lf.lfPitchAndFamily;
m_cf.dwEffects &= (unsigned long) ~CFE_AUTOCOLOR;
_tcscpy(m_cf.szFaceName, lf.lfFaceName);
GetRichEditCtrl().SetSel(0, -1);
if(!GetRichEditCtrl().SetSelectionCharFormat(m_cf))
{
AfxMessageBox("Error setting character format");
}
SetFocus();
}
void CSendViewRE::SetupColor()
{
CColorDialog dlgColor(RGB(0,0,0));
if (dlgColor.DoModal() == IDOK)
{
// COLORREF txtClr = dlgColor.GetColor();
// CHARFORMAT2 cf;
// GetRichEditCtrl().GetSelectionCharFormat(cf);
m_cf.crTextColor = dlgColor.GetColor();
m_cf.dwEffects &= (unsigned long) ~CFE_AUTOCOLOR;
GetRichEditCtrl().SetSelectionCharFormat(m_cf);
SetFocus();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?