想要在运行时在MFC中的Richedit控件周围显示彩色框 [英] Want to show colored box around Richedit control in MFC at runtime

查看:137
本文介绍了想要在运行时在MFC中的Richedit控件周围显示彩色框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mfc应用程序.我在对话框上有一些richedit控件.我想在控件周围显示一个黄色的填充框架.这样做的方式是什么?

I have an mfc application. I have some richedit controls on the dialog. I want to show a yellow colored filled frame around the controls. What is the way to do this?

我试图围绕现有的richedit ctrl创建一个更丰富的edit ctrl,并在其变量上使用SetBackgroundColor,但是它会使整个区域着色,而其他richedit ctrl变得不可见.另外,我想在运行时更改周围的颜色. 请帮我.我对此感到困惑.

I tried to create one more rich edit ctrl around the existing richedit ctrl and use SetBackgroundColor on its variable, but it colors the entire area and other richedit ctrls become invisible. Also, I want to change the surrounding color at run time. Please help me. I am stuck with this.

推荐答案

也许有更好的方法可以做到这一点,但是以下方法应该可以工作.如果从CRichEditCtrl派生自己的类,则可以利用 WM_NCPAINT 消息来渲染边框.像……

There may be a better way to accomplish this, but, the following should work. If you derive your own class from CRichEditCtrl, you can leverage the WM_NCPAINT message to render the border. Something like…

void RichEdit::OnNcPaint()
    {
    CPaintDC dc(this); // device context for painting
    CRect rect;
    GetWindowRect(&rect);
    ScreenToClient(rect);

    CPen pen;
    pen.CreatePen(PS_SOLID, 10, RGB(255, 255, 0));
    dc.SelectObject(pen);
    dc.Rectangle(&rect);

    CHARFORMAT cf = { 0 };
    int txtLen = GetTextLength();

    cf.cbSize = sizeof(cf);
    cf.dwMask = CFM_ITALIC;

    SetSel(txtLen, -1); ReplaceSel("Some text"); 

    // Apply formating to the just inserted text.
    SetSel(txtLen, GetTextLength());
    SetSelectionCharFormat(cf);
    SetFocus();

    // Do not call CRichEditCtrl::OnNcPaint() for painting messages
    }

将边框显示为黄色,并写入相应的文本.这就是它的样子.

Will render the border as Yellow, and, write the corresponding text. Here’s what it will look like.

这篇关于想要在运行时在MFC中的Richedit控件周围显示彩色框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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