在MFC的CEDIT控件中仅更改选定的文本颜色而不更改整个文本 [英] changing only selected text color not entire text in CEDIT control of mfc

查看:157
本文介绍了在MFC的CEDIT控件中仅更改选定的文本颜色而不更改整个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mfc的CEDIT控件中仅更改选定的文本颜色而不更改整个文本

changing only selected text color not entire text in CEDIT control of mfc

推荐答案



根据我的理解,字符格式在MFC CEdit控件中不可用.如果更改了CEdit控件的文本颜色,它将影响整个字符串.为了更好地使用Rich Edit控件,它具有此功能 http://msdn.microsoft.com/zh-cn/library/06832atz.aspx [ ^ ].如果要坚持使用CEdit控件,则可能需要使用一些困难的逻辑.例如,使用"GetSel"方法找到选定的字符串,然后找到坐标位置并使用"TextOut"方法,但是对此不确定.
Hi,

According to my understanding, character formatting is not available with MFC CEdit control. If you changed the text color of CEdit control, it will affect the entire string. For this purpose better to use Rich Edit Controls, it had this functionality http://msdn.microsoft.com/en-us/library/06832atz.aspx[^]. If you want to stick with CEdit control, you may need to use some difficult logic. For example using "GetSel" method to find the selected string then find the co-ordinate positions and use "TextOut" method, but am not sure about this.


这里是解决方案单行编辑控件...稍后将实现多行

Here is a solution for a single-line edit control ... will implement multi-line later

void CODEdit::OnPaint()
{
	// Prepare to draw
	CPaintDC dc(this); 
	CRect rClient;
	GetClientRect(&rClient);
	DWORD dwStyle = GetStyle();
	CString cstrText;
	GetWindowText(cstrText);

	// Double buffer 
	CDC dcMem;
	dcMem.CreateCompatibleDC(&dc);
	CBitmap bmp;
	bmp.CreateDiscardableBitmap(&dc, 
		rClient.Width(), rClient.Height());
	dcMem.SelectObject(bmp);
	dcMem.SelectObject(GetFont());

	// Draw border
	CPen penBorder;
	penBorder.CreatePen(PS_SOLID, 1, m_crBorder);
	CPen* pOldPen = dcMem.SelectObject(&penBorder);
	dcMem.FillSolidRect(rClient, m_crBack);
	dcMem.Rectangle(rClient);
	dcMem.SelectObject(pOldPen);

	// Draw text
	CRect rTxt = rClient;
	UINT nFormat = DT_LEFT|DT_TOP|DT_SINGLELINE;
	// Handle margins
	DWORD dwMargins = GetMargins();
	rTxt.InflateRect(-LOWORD(dwMargins), 0, -HIWORD(dwMargins), 0);
	rTxt.InflateRect(-1,-1,-1,-1);
	// Handle selected text
	DWORD dwSel = GetSel();
	int iStartSel = LOWORD(dwSel);
	int iEndSel = HIWORD(dwSel);

	if((iStartSel == 0) && (iEndSel == 0))
	{
		// No selection, just draw
		dcMem.DrawText(cstrText, cstrText.GetLength(), rTxt, nFormat);
	}
	else
	{
		int iCurX = rTxt.left;
		int iStartChar = 0;
		int iEndChar = (iStartSel == 0 ? iEndSel : iStartSel);

		while(iStartChar < cstrText.GetLength())
		{
			CString cstrPart = cstrText.Mid(iStartChar, (iEndChar - iStartChar));

			BOOL bSel = (iStartChar == iStartSel);
			dcMem.SetTextColor(bSel ? ::GetSysColor(COLOR_HIGHLIGHTTEXT) : ::GetSysColor(COLOR_WINDOWTEXT));
			dcMem.SetBkColor(bSel ? ::GetSysColor(COLOR_HIGHLIGHT) : m_crBack);

			CRect rSel = rTxt;
			rSel.left = iCurX;
			dcMem.DrawText(cstrPart, cstrPart.GetLength(), rSel, nFormat);
			
			iCurX += dcMem.GetTextExtent(cstrPart, cstrPart.GetLength()).cx;
			iStartChar = iEndChar;
			iEndChar = ((iStartChar == iStartSel) ? iEndSel :  cstrText.GetLength());
		}
	}

	// Update the screen
	dc.BitBlt(0, 0, rClient.Width(), rClient.Height(), &dcMem, 0, 0, SRCCOPY);
}




颜色(COLORREF类型)是我的类的成员变量,我将像这样设置它们:...看起来不错




The colors (type COLORREF)are member variables of my class, I would set them like this: ... looks nice

m_crBorder		= RGB(128,160,192);
m_crBack		= RGB(255,255,255);


这篇关于在MFC的CEDIT控件中仅更改选定的文本颜色而不更改整个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆