关闭

RichEdit

143人阅读 评论(0) 收藏 举报
分类:

// TextHandleView.cpp : CTextHandleView 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "TextHandle.h"
#endif

#include "TextHandleDoc.h"
#include "CntrItem.h"
#include "resource.h"
#include "TextHandleView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTextHandleView

IMPLEMENT_DYNCREATE(CTextHandleView, CRichEditView)

BEGIN_MESSAGE_MAP(CTextHandleView, CRichEditView)
	ON_WM_DESTROY()
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_ENTERIDLE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

// CTextHandleView 构造/析构

CTextHandleView::CTextHandleView()
{
	// TODO: 在此处添加构造代码

}

CTextHandleView::~CTextHandleView()
{
}

BOOL CTextHandleView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CRichEditView::PreCreateWindow(cs);
}

void CTextHandleView::OnInitialUpdate()
{
	CRichEditView::OnInitialUpdate();


	// 设置打印边距(720 缇 = 1/2 英寸)
	SetMargins(CRect(720, 720, 720, 720));
}

void CTextHandleView::OnDestroy()
{
	// 析构时停用此项;这在
	// 使用拆分视图时非常重要 
   COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
   CRichEditView::OnDestroy();
}


void CTextHandleView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CTextHandleView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CTextHandleView 诊断

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

void CTextHandleView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

CTextHandleDoc* CTextHandleView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextHandleDoc)));
	return (CTextHandleDoc*)m_pDocument;
}
#endif //_DEBUG


// CTextHandleView 消息处理程序


BOOL CTextHandleView::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类

	
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == VK_LEFT || pMsg->wParam == VK_BACK)
		{
			int nRow = 0, nCol = 0;
			GetCaretInfo(nRow, nCol);
			TRACE("nRow:%d nCol:%d\n", nRow, nCol);
			if (nCol == 1)
			{
				return TRUE;
			}
			
		}

	}



	return CRichEditView::PreTranslateMessage(pMsg);
}



void CTextHandleView::GetCaretInfo(int &nCurrentRow, int &nCurrentCol)
{
	nCurrentRow = 0;
	nCurrentCol = 0;

	CRichEditCtrl& theCtrl = this->GetRichEditCtrl();
	// 获取当前行号
	nCurrentRow = theCtrl.LineFromChar(-1);

	int nLineStartPos = theCtrl.LineIndex(-1); // 首字符索引
	TRACE("nLineStartPos: %d\n", nLineStartPos);

	long nSelStart, nSelEnd;
	theCtrl.GetSel(nSelStart, nSelEnd);

	TRACE("nSelStart: %d	nSelEnd: %d\n", nSelStart, nSelEnd);
	
	nCurrentCol = nSelStart - nLineStartPos + 1;

/*
	CPoint VarCharPoint;         // 指定字符的位置
	CPoint CurrPoint;            // 当前光标位置
	int LineFirstIndex;          // 当前行首字符位置
	int Length;                  // 当前行长度

	int CurrentCharIndex;        // 当前编辑光标所在字符序号。
	int CurrentLine;             // 当前编辑光标所在的行号
	int CurrentRow;              // 当前编辑光标所在的列号

	CRichEditCtrl &edit = GetRichEditCtrl();
	CurrPoint = edit.GetCaretPos();        //获取光标位置
	LineFirstIndex = edit.LineIndex(-1);   // 获取当前行首字符位置
	Length = edit.LineLength(-1);          // 获取当前行长度.
	
	int i = 0;
	for (; i < Length; i++)
	{
		VarCharPoint = edit.GetCharPos(LineFirstIndex);
		if (VarCharPoint.x >= CurrPoint.x)
		{
			CurrentCharIndex = LineFirstIndex;
			break;
		}
		LineFirstIndex++;
	}

	CurrentRow = i; //列号
	CurrentLine = edit.LineFromChar(CurrentCharIndex); //行号
*/

}


void CTextHandleView::OnEnterIdle(UINT nWhy, CWnd* pWho)
{
	CRichEditView::OnEnterIdle(nWhy, pWho);

	TRACE("OnEnterIdle");
	// TODO: 在此处添加消息处理程序代码
}

CPoint pt;
void CTextHandleView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CRichEditCtrl& theCtrl = this->GetRichEditCtrl();
	pt = theCtrl.GetCaretPos();

	CRichEditView::OnLButtonDown(nFlags, point);
	
	theCtrl.SetCaretPos(pt);
}


void CTextHandleView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CRichEditView::OnLButtonUp(nFlags, point);

}


void CTextHandleView::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CRichEditView::OnMouseMove(nFlags, point);

	theCtrl.SetCaretPos(pt);
}

在一些交互性很强的程序中,经常需要使用CRichEditCtrl控件,但是如何进行编辑文本,如设置文本字体,颜色,链接,插入图片等等,如何进行操作,这儿就转载一篇文章,收集一些常见的问题和解决方法,说不定哪一天就用到了。我以前开发过一个聊天程序,调用了一个人开发的CMyRichEdit扩展,实现了字体,颜色,添加链接,插入图片(QQ控件)等等。

m_edit1代表ID为IDC_EDIT1的CEdit控件的control类型的变量
        m_richedit1代表ID为IDC_RICHEDIT1的CRichEditCtrl控件的control类型的变量
 
________________________________________
1.设置edit只读属性

    方法一:
                m_edit1.SetReadOnly(TRUE);
    方法二:
                ::SendMessage(m_edit1.m_hWnd, EM_SETREADONLY, TRUE, 0);

 
________________________________________
2.判断edit中光标状态并得到选中内容(richedit同样适用)

        int nStart, nEnd;
        CString strTemp;

        m_edit1.GetSel(nStart, nEnd);
        if(nStart == nEnd)
        {
            strTemp.Format(_T("光标在%d"), nStart);
            AfxMessageBox(strTemp);
        }
        else
        {
            //得到edit选中的内容     
            m_edit1.GetWindowText(strTemp);
            strTemp = strTemp.Mid(nStart) - strTemp.Mid(nEnd);
            AfxMessageBox(strTemp); 
        }
    注:GetSel后,如果nStart和nEnd,表明光标处于某个位置(直观来看就是光标在闪动);
             如果nStart和nEnd不相等,表明用户在edit中选中了一段内容。

________________________________________
3.在edit最后添加字符串

        CString str;
        m_edit1.SetSel(-1, -1);
        m_edit1.ReplaceSel(str);

 
________________________________________
4.随输入自动滚动到最后一行(richedit同样适用)

    方法一:(摘自msdn)
        // The pointer to my edit.
        extern CEdit* pmyEdit;
        int nFirstVisible = pmyEdit->GetFirstVisibleLine();

        // Scroll the edit control so that the first visible line
        // is the first line of text.
        if (nFirstVisible > 0)
        {
            pmyEdit->LineScroll(-nFirstVisible, 0);
        }
    方法二:
        m_richedit.PostMessage(WM_VSCROLL, SB_BOTTOM, 0);

________________________________________
5.如何限制edit输入指定字符

   可以从CEdit派生一个类,添加WM_CHAR消息映射。下面一个例子实现了限定输入16进制字符的功能。

   void CMyHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)  
   
        if ( (nChar >= '0' && nChar <= '9') || 
             (nChar >= 'a' && nChar <= 'f') || 
             (nChar >= 'A' && nChar <= 'F') || 
              nChar == VK_BACK ||  
              nChar == VK_DELETE)    //msdn的virtual key
       
            CEdit::OnChar(nChar, nRepCnt, nFlags); 
        }     
   }

 
________________________________________
6.如何使用richedit

    添加AfxInitRichEdit();
       CxxxApp::InitInstance()
        {
             AfxInitRichEdit();
          .............
       }

   AfxInitRichEdit()功能:装载 RichEdit 1.0 Control (RICHED32.DLL).

 
________________________________________
7.如何使用richedit2.0 or richedit3.0

    使用原因:由于RichEdit2.0A自动为宽字符(WideChar),所以它可以解决中文乱码以及一些汉字问题

    方法一:(msdn上的做法,适用于用VC.NET及以后版本创建的工程)
            To update rich edit controls in existing Visual C++ applications to version 2.0,
            open the .RC file as text, change the class name of each rich edit control from   "RICHEDIT" to  "RichEdit20a". 
            Then replace the call to AfxInitRichEdit with AfxInitRichEdit2.
    方法二:以对话框为例:
       (1)    增加一全局变量 HMODULE hMod;
       (2)    在CxxxApp::InitInstance()中添加一句hMod = LoadLibrary(_T("riched20.dll"));
              在CxxxApp::ExitInstance()中添加一句FreeLibrary(hMod);
       (3)      在对话框上放一个richedit,文本方式打开.rc文件修改该richedit控件的类名"RICHEDIT" to  "RichEdit20a".
       (4)      在对话框头文件添加 CRichEditCtrl m_richedit;
              在OnInitDialog中添加 m_richedit.SubclassDlgItem(IDC_RICHEDIT1, this);

________________________________________
8.改变richedit指定区域的颜色及字体

        CHARFORMAT cf;
        ZeroMemory(&cf, sizeof(CHARFORMAT));
        cf.cbSize = sizeof(CHARFORMAT);
        cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
                            CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
        cf.dwEffects = 0;
        cf.yHeight = 12*12;//文字高度
        cf.crTextColor = RGB(200, 100, 255); //文字颜色
        strcpy(cf.szFaceName ,_T("隶书"));//设置字体
     
        m_richedit1.SetSel(1, 5); //设置处理区域
        m_richedit1.SetSelectionCharFormat(cf);

 
________________________________________
9.设置行间距(只适用于richedit2.0)

        PARAFORMAT2 pf;
        pf2.cbSize = sizeof(PARAFORMAT2);
        pf2.dwMask = PFM_LINESPACING | PFM_SPACEAFTER;
        pf2.dyLineSpacing = 200;
        pf2.bLineSpacingRule  = 4;
        m_richedit.SetParaFormat(pf2);

 
________________________________________
10.richedit插入位图

Q220844:How to insert a bitmap into an RTF document using the RichEdit control in Visual C++ 6.0
http://support.microsoft.com/default.aspx?scid=kb;en-us;220844
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2417/
http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/

 
________________________________________
11.richedit插入gif动画

http://www.codeproject.com/richedit/AnimatedEmoticon.asp

________________________________________
12.richedit嵌入ole对象

http://support.microsoft.com/kb/141549/en-us

________________________________________
13.使richedit选中内容只读

http://www.codeguru.com/cpp/controls/richedit/article.php/c2401/

 
________________________________________
14.打印richedit

http://www.protext.com/MFC/RichEdit3.htm

 
________________________________________
15.richeidt用于聊天消息窗口

http://www.vckbase.com/document/viewdoc/?id=1087
http://www.codeproject.com/richedit/chatrichedit.asp
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2395/

________________________________________
16.解决richedit的EN_SETFOCUS和EN_KILLFOCUS无响应的问题

http://support.microsoft.com/kb/181664/en-us

 
________________________________________
17.richedit拼写检查

http://www.codeproject.com/com/AutoSpellCheck.asp

 
________________________________________
18.改变edit背景色

Q117778:How to change the background color of an MFC edit control
http://support.microsoft.com/kb/117778/en-us

 
________________________________________
19.当edit控件的父窗口属性是带标题栏WS_CAPTION和子窗口WS_CHILD时,不能设置焦点SetFocus

Q230587:PRB: Can't Set Focus to an Edit Control When its Parent Is an Inactive Captioned Child Window
http://support.microsoft.com/kb/230587/en-us

________________________________________
20. 在Edit中回车时,会退出对话框 

选中Edit的风格Want Return。

MSDN的解释如下:
ES_WANTRETURN   Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. Without this style, pressing the ENTER key has the same effect as pressing the dialog box's default pushbutton. This style. has no effect on a single-line edit control.

 
________________________________________
21. 动态创建的edit没有边框的问题

    m_edit.Create(....);
    m_edit.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:31058次
    • 积分:1500
    • 等级:
    • 排名:千里之外
    • 原创:116篇
    • 转载:43篇
    • 译文:0篇
    • 评论:0条
    文章分类
    博客链接