MFC编辑控件颜色 [英] MFC Edit Control Color
本文介绍了MFC编辑控件颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个MFC Edit控件,用于将多行文本输出到该控件上.我可以给文本加上颜色,但只能给所有颜色加上颜色,不会给文本的各个单独的颜色加上颜色.
我知道这是因为我将文本视为一个大缓冲区,但是有没有解决的办法?
我的课程:
I have a MFC Edit control which i use to output multiple lines of text onto. I can color the text but it only colors everything, it won''t color individual lines of text.
I know this is because i treat the text as one big buffer, but is there a way around this?
My Class:
CColorEdit::CColorEdit()
{
BackColor = GetSysColor( COLOR_3DFACE );
TextColor = RGB( 0, 0, 0 );
BackBrush.CreateSolidBrush( BackColor );
}
CColorEdit::~CColorEdit()
{
}
void CColorEdit::SetReadOnly ()
{
CEdit::SetReadOnly( true );
SetBGColor( RGB( 255, 255, 255 ) );
SetTextColor( RGB( 0, 0, 0 ) );
}
void CColorEdit::SetTextColor ( COLORREF Color )
{
TextColor = Color;
RedrawWindow();
}
void CColorEdit::SetBGColor ( COLORREF Color )
{
BackColor = Color;
BackBrush.DeleteObject();
BackBrush.CreateSolidBrush( BackColor );
RedrawWindow( );
}
HBRUSH CColorEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkColor( BackColor );
pDC->SetTextColor( TextColor );
nCtlColor = CTLCOLOR_EDIT;
return BackBrush;
}
BEGIN_MESSAGE_MAP(CColorEdit, CEdit)
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
我这样称呼:
I call like:
Tools.AppendWindowText( RGB( 0, 192, 0 ), "[%s] %s", CurrentService->Status, CurrentService->Name );
功能如下:
Here''s the function:
void CTools::AppendWindowText( COLORREF Color, char* AppendText, ... )
{
static char AppendData[ 256 ];
static char CurrentData[ 2048000 ];
ZeroMemory( CurrentData, sizeof( CurrentData ) );
ZeroMemory( AppendData, sizeof( AppendData ) );
va_list valist;
va_start( valist, AppendText );
_vsnprintf( AppendData, sizeof( AppendData ) - strlen( AppendData ), AppendText, valist );
va_end( valist );
int CurrentTextLen = pClientDlg->m_ResultsWindow.GetWindowTextLengthA( );
int AppendDataLen = strlen( AppendData );
int DataLength = ( CurrentTextLen + AppendDataLen + 2 );
pClientDlg->m_ResultsWindow.GetWindowTextA( CurrentData, DataLength );
if ( CurrentTextLen > 0 ) { strcat( CurrentData, "\r\n" ); }
strcat( CurrentData, AppendData );
pClientDlg->m_ResultsWindow.SetTextColor( Color );
pClientDlg->m_ResultsWindow.SetWindowTextA( CurrentData );
}
谢谢
Thanks
推荐答案
您好,我相信您有4个选择:
1)使用丰富的编辑控件
2)使用列表框
3)自己绘制文本(但是我没有在上面的代码中看到用于存储哪种代码获得什么颜色的机制)
4)使用html和html视图(如果您的应用程序使用框架窗口)
您可以尝试完成什么,只需要一些额外的代码
Hello, I believe you have 4 options:
1) Use a rich edit control
2) Use a listbox
3) Draw the text yourself (but I dont see in the code above a mechanism for storing which code gets what color)
4) Use html and a html view (if your application is using frame windows)
what your trying to do can be done, just needs a little extra code
这篇关于MFC编辑控件颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文