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

📄 chatviewre.cpp

📁 Wordpad source code very simple
💻 CPP
字号:
/**************************************************************************
   THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY OF
   ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
   PARTICULAR PURPOSE.
   Author: Barretto VN  7/2001
**************************************************************************/

// ChatViewRE.cpp : implementation file
//

#include "stdafx.h"
//#include "chatclient.h"
#include "ChatViewRE.h"

#include "SimpleWordPad.h"

//#include "AboutBox.h"

#include "IndentDialog.h"

#include "MyAboutDlg.h"

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

#include <afxrich.h>


/////////////////////////////////////////////////////////////////////////////
// CChatViewRE

IMPLEMENT_DYNCREATE(CChatViewRE, CRichEditView)

CChatViewRE::CChatViewRE()
{

	bBoldSelected = false;
	bUnderlineSelected = false;
	bStrikeOutSelected = false;
	bItalicSelected = false;

	bLeftAlign = true;
	bRightAlign = false;
	bCenterAlign = false;

	bIsBulleted = false;

}

CChatViewRE::~CChatViewRE()
{
}


BEGIN_MESSAGE_MAP(CChatViewRE, CRichEditView)
	//{{AFX_MSG_MAP(CChatViewRE)
		// NOTE - the ClassWizard will add and remove mapping macros here.

	ON_COMMAND(ID_INSERT_DATE, OnInsertDate)
	ON_COMMAND(ID_INSERT_TIME, OnInsertTime)
	ON_COMMAND(ID_SET_FONT, OnSetFont)
	ON_COMMAND(ID_SET_COLOR, OnSetColor)

	ON_COMMAND(ID_BOLD, OnBold)
	ON_COMMAND(ID_BULLETED, OnBulleted)
	ON_COMMAND(ID_ITALIC, OnItalic)
	ON_COMMAND(ID_STRIKEOUT, OnStrikeout)
	ON_COMMAND(ID_UNDERLINED, OnUnderlined)

	ON_COMMAND(ID_MYABOUTBOX, OnMyaboutbox)

	ON_COMMAND(ID_INDENTATION, OnIndentation)

	ON_UPDATE_COMMAND_UI(ID_BULLETED, OnUpdateBulleted)
	ON_UPDATE_COMMAND_UI(ID_BOLD, OnUpdateBold)
	ON_UPDATE_COMMAND_UI(ID_ITALIC, OnUpdateItalic)
	ON_UPDATE_COMMAND_UI(ID_STRIKEOUT, OnUpdateStrikeout)
	ON_UPDATE_COMMAND_UI(ID_UNDERLINED, OnUpdateUnderlined)

	ON_UPDATE_COMMAND_UI_RANGE(ID_ALIGNMENT_LEFT, ID_ALIGNMENT_CENTER, OnUpdateAlignment)
	ON_COMMAND_RANGE(ID_ALIGNMENT_LEFT, ID_ALIGNMENT_CENTER, OnAlignment)

	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChatViewRE drawing

void CChatViewRE::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CChatViewRE diagnostics

#ifdef _DEBUG
void CChatViewRE::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CChatViewRE::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChatViewRE message handlers

void CChatViewRE::OnInitialUpdate() 
{
	CRichEditView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class

	CHARFORMAT2 cf;
	cf.cbSize = sizeof(CHARFORMAT2);
	cf.dwMask = 
	      		CFM_BOLD|
//		        CFM_ITALIC|
//				CFM_UNDERLINE|
//				CFM_STRIKEOUT|
				CFM_SIZE|
		        CFM_COLOR;
//				CFM_OFFSET|
//				CFM_PROTECTED;

	cf.dwEffects = ~CFE_AUTOCOLOR;
	cf.yHeight = 200; //10pt
	cf.wWeight = FW_NORMAL;
	cf.yOffset = 0;
	cf.crTextColor = RGB(0, 0, 0);
	cf.bCharSet = 0;
	cf.bPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
	strcpy(cf.szFaceName, "Verdana");
	cf.dwMask |= CFM_FACE;
	bBoldSelected = true;
	GetRichEditCtrl().SetDefaultCharFormat(cf);	

	PARAFORMAT2 pf;
	GetRichEditCtrl().GetParaFormat(pf);
	pf.dwMask = PFM_ALIGNMENT;
	pf.wAlignment = PFA_LEFT;



}

void CChatViewRE::OnInsertDate() 
{
	// TODO: Add your command handler code here
	SYSTEMTIME m_time;
	GetLocalTime(&m_time);
	TCHAR buf[256];
	GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &m_time,  "dddd MMMM yyyy", buf, 256);
	GetRichEditCtrl().ReplaceSel(buf);	
}

void CChatViewRE::OnInsertTime() 
{
	// TODO: Add your command handler code here
	SYSTEMTIME m_time;
	GetLocalTime(&m_time);
	TCHAR buf[256];
	VERIFY(GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, &m_time, "hh:mm:ss tt", buf, 256));
	GetRichEditCtrl().ReplaceSel(buf);
	
}

void CChatViewRE::AlignText(int iAlign)
{
	PARAFORMAT2 pf;
	GetRichEditCtrl().GetParaFormat(pf);
	pf.dwMask = PFM_ALIGNMENT;
	pf.wAlignment = iAlign;
	GetRichEditCtrl().SetParaFormat(pf);
// Reset the dirty bit.
	GetRichEditCtrl().SendMessage (EM_SETMODIFY, (WPARAM)TRUE, 0L);	

}

void CChatViewRE::OnSetFont() 
{
	// TODO: Add your command handler code here
	//
	//
	// Code in this member Function 
	// was pasted (and Modified)
	// from a program named REITP
	// available on the MSDN CD-1.
	// Thanks to the Author.
	//
	//

	LOGFONT lf;
	CHOOSEFONT csf = {0};
	CHARFORMAT cf;
	LONG yPerInch;
	HDC hdc;

	cf.cbSize = sizeof(CHARFORMAT);
	hdc = (HDC)GetDC();
	yPerInch = GetDeviceCaps(hdc, LOGPIXELSY);

	GetRichEditCtrl().SendMessage(EM_GETCHARFORMAT, (WPARAM) true,
			(LPARAM) &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 = cf.crTextColor;
	csf.lpszStyle = NULL;
	csf.nFontType = REGULAR_FONTTYPE | SCREEN_FONTTYPE;

	lf.lfHeight = -(INT) ((cf.yHeight * yPerInch) / 1440);
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = (cf.dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
	lf.lfItalic = (cf.dwEffects & CFE_ITALIC) ? true : false;
	lf.lfUnderline = (cf.dwEffects & CFE_UNDERLINE) ? true : false;
	lf.lfStrikeOut = (cf.dwEffects & CFE_STRIKEOUT) ? true : false;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	lf.lfQuality = DRAFT_QUALITY;
	lf.lfCharSet = cf.bCharSet;
	lf.lfPitchAndFamily = cf.bPitchAndFamily;
	_tcscpy(lf.lfFaceName, cf.szFaceName);

	if(!ChooseFont(&csf))
		return;

	cf.cbSize = sizeof(CHARFORMAT);

	// don't change read-only bit
	cf.dwMask = CFM_SIZE | CFM_EFFECTS | CFM_COLOR | CFM_FACE | CFM_CHARSET;
	cf.yHeight = (LONG) csf.iPointSize * 2;
	cf.dwEffects = CFM_EFFECTS;
	if(lf.lfWeight < FW_BOLD)
		cf.dwEffects &= ~CFE_BOLD;
	if(!lf.lfItalic)
		cf.dwEffects &= ~CFE_ITALIC;
	if(!lf.lfUnderline)
		cf.dwEffects &= ~CFE_UNDERLINE;
	if(!lf.lfStrikeOut)
		cf.dwEffects &= ~CFE_STRIKEOUT;
	cf.crTextColor = csf.rgbColors;
	cf.bCharSet = lf.lfCharSet;
	cf.bPitchAndFamily = lf.lfPitchAndFamily;
	_tcscpy(cf.szFaceName, lf.lfFaceName);

	if(!GetRichEditCtrl().SetSelectionCharFormat(cf))
	{
		AfxMessageBox("Error setting character format");
	}
}

void CChatViewRE::OnSetColor() 
{
	// TODO: Add your command handler code here

	CColorDialog dlgColor(RGB(0,0,0));
	if (dlgColor.DoModal() == IDOK)
	{
		COLORREF txtClr = dlgColor.GetColor();
		CHARFORMAT2 cf;
		GetRichEditCtrl().GetSelectionCharFormat(cf);
	    cf.crTextColor = dlgColor.GetColor();
		cf.dwEffects &= (unsigned long) ~CFE_AUTOCOLOR;

		SetFormat(cf);
	}

}


void CChatViewRE::OnBold() 
{
	// TODO: Add your command handler code here
	CHARFORMAT2 cf;
	GetRichEditCtrl().GetSelectionCharFormat(cf);

	if(bBoldSelected)
	{
		cf.dwEffects &= ~CFE_BOLD;
		cf.wWeight = FW_NORMAL;
		bBoldSelected = false;
	}
	else
	{
		cf.dwMask = cf.dwMask | CFM_WEIGHT | CFM_BOLD;
		cf.dwEffects = CFE_BOLD;
		cf.wWeight = FW_BOLD;
		bBoldSelected = true;
	}
	SetFormat(cf);

}

void CChatViewRE::OnBulleted() 
{
	// TODO: Add your command handler code here
	PARAFORMAT2 pf;
	
	GetRichEditCtrl().GetParaFormat(pf);
	pf.dwMask = PFM_NUMBERING;
	if(pf.wNumbering == PFN_BULLET)
	{
		bIsBulleted = false;
		pf.wNumbering = 0;
	}
	else
	{
		bIsBulleted = true;
		pf.wNumbering = PFN_BULLET;
	}

	GetRichEditCtrl().SetParaFormat(pf);
// Reset the dirty bit.
	GetRichEditCtrl().SendMessage (EM_SETMODIFY, (WPARAM)TRUE, 0L);	

}

void CChatViewRE::OnItalic() 
{
	// TODO: Add your command handler code here
	CHARFORMAT2 cf;
	GetRichEditCtrl().GetSelectionCharFormat(cf);
	if(bItalicSelected)
	{
		cf.dwEffects &= ~CFE_ITALIC;
		bItalicSelected = false;
	}
	else
	{
		cf.dwMask = CFM_ITALIC;
		cf.dwEffects = CFE_ITALIC;
		bItalicSelected = true;
	}
	SetFormat(cf);

}

void CChatViewRE::OnStrikeout() 
{
	// TODO: Add your command handler code here
	CHARFORMAT2 cf;
	GetRichEditCtrl().GetSelectionCharFormat(cf);
	if(bStrikeOutSelected)
	{
		cf.dwEffects &= ~CFM_STRIKEOUT;
		bStrikeOutSelected = false;
	}
	else
	{
		cf.dwMask = CFM_STRIKEOUT;
		cf.dwEffects = CFE_STRIKEOUT;
		bStrikeOutSelected = true;
	}
	SetFormat(cf);
	
}

void CChatViewRE::OnUnderlined() 
{
	// TODO: Add your command handler code here
	CHARFORMAT2 cf;
	GetRichEditCtrl().GetSelectionCharFormat(cf);
	if(bUnderlineSelected)
	{
		cf.dwEffects &= ~CFE_UNDERLINE;
		bUnderlineSelected = false;
	}
	else
	{
		cf.dwMask = CFM_UNDERLINE | CFM_UNDERLINETYPE;
		cf.dwEffects = CFE_UNDERLINE;
		cf.bUnderlineType = CFU_UNDERLINE;
		bUnderlineSelected = true;
	}
	SetFormat(cf);
}

void CChatViewRE::OnMyaboutbox() 
{
	// TODO: Add your command handler code here
//	CMyAboutDlg* dlg = new CMyAboutDlg();
//	dlg->DoModal();
//	ShowAboutBox("Barrys Simple WORDPAD", NULL);

}

void CChatViewRE::OnIndentation() 
{
	// TODO: Add your command handler code here

	CIndentDialog* dlg = new CIndentDialog();
	if(dlg->DoModal() == IDOK)
	{
		PARAFORMAT pf;
		pf.cbSize = sizeof(pf);
		if (dlg->m_nRight != 0)
			pf.dwMask |= PFM_RIGHTINDENT;
		if (dlg->m_nLeft != 0)
			pf.dwMask |= PFM_STARTINDENT;
		if (dlg->m_nFirst != 0)
			pf.dwMask |= PFM_OFFSET;
		
		pf.dxRightIndent = dlg->m_nRight*1440;
		long iVal = dlg->m_nFirst*1440;
		pf.dxOffset = -iVal;
		pf.dxStartIndent = (dlg->m_nLeft*1440) + iVal;

		GetRichEditCtrl().SetParaFormat(pf);
	}
}


void CChatViewRE::OnUpdateBulleted(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

	pCmdUI->SetCheck(bIsBulleted);
}


void CChatViewRE::OnUpdateBold(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(bBoldSelected);
}

void CChatViewRE::OnUpdateItalic(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(bItalicSelected);
}

void CChatViewRE::OnUpdateStrikeout(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(bStrikeOutSelected);
}

void CChatViewRE::OnUpdateUnderlined(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(bUnderlineSelected);
	
}

void CChatViewRE::SetFormat(CHARFORMAT2 cf)
{
	long lStart, lEnd;
	GetRichEditCtrl().GetSel(lStart , lEnd);
	GetRichEditCtrl().SetSel(lStart , (lStart == lEnd ? -1 : lEnd));
	GetRichEditCtrl().SetSelectionCharFormat(cf);	
}

void CChatViewRE::OnUpdateAlignment(CCmdUI* pCmdUI)
{
	pCmdUI->Enable();
	BOOL bChecked = FALSE;

	PARAFORMAT2 pf;
	GetRichEditCtrl().GetParaFormat(pf);

	switch (pCmdUI->m_nID)
	{
	case ID_ALIGNMENT_RIGHT:
		{
		bChecked = (pf.wAlignment == PFA_RIGHT);
		break;
		}
	case ID_ALIGNMENT_LEFT:
		{
		bChecked = (pf.wAlignment == PFA_LEFT);
		break;
		}

	case ID_ALIGNMENT_CENTER:
		{
		bChecked = (pf.wAlignment == PFA_CENTER);
		break;
		}

	default:
		{
		bChecked = false;
		break;
		}
	}

	pCmdUI->SetRadio(bChecked ? 1 : 0);	
}

void CChatViewRE::OnAlignment(UINT nCommand)
{

	switch(nCommand)
	{
	case ID_ALIGNMENT_LEFT:
		AlignText(PFA_LEFT);
		break;
	case ID_ALIGNMENT_RIGHT:
		AlignText(PFA_RIGHT);
		break;
	case ID_ALIGNMENT_CENTER:
		AlignText(PFA_CENTER);
		break;
	}

}

⌨️ 快捷键说明

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