关于DuiLib下 实现关键字高亮、粗体显示的实现

有两种方案:
1.使用CRichEditUI  来做;
2.使用CLabelUI 来做。

CRichEditUI的做法:
1.首先继承CRichEditUI 生成CRichEditUIEx类
2.CRichEditUIEx类中实现PaintText方法,只需要在绘制文本的时候,添加三行代码:
    SetSel(begin, end);
    SetFontFormat();
    SetSle(-1, -1);
这三句话,首先是:设置选中位置; 设置选中区域内文本样式;设置为未选中状态
 void SetTextFont(bool bold, bool italic, bool underline, CString strFont, COLORREF color, int size)
 {
  CHARFORMAT2 cf,oldCf;
  GetSelectionCharFormat(oldCf);
  GetSelectionCharFormat(cf);
  if (bold)
  {
   cf.dwMask|=CFM_BOLD;
   cf.dwEffects|=CFE_BOLD;            //设置粗体,取消用cf.dwEffects&=~CFE_BOLD;
  }
  if (italic)
  {
   cf.dwMask|=CFM_ITALIC;
   cf.dwEffects|=CFE_ITALIC;        //设置斜体,取消用cf.dwEffects&=~CFE_ITALIC;
  }
  if (underline)
  {
   cf.dwMask|=CFM_UNDERLINE;
   cf.dwEffects|=CFE_UNDERLINE;    //设置下划线,取消用cf.dwEffects&=~CFE_UNDERLINE;
  }
  if (color)
  {
   cf.dwEffects &= ~CFE_AUTOCOLOR;    //这个最重要,设选中文字颜色的时候也要注意,dwEffects一定不能有CEF_AUTOCOLOR的属性
   cf.dwMask|=CFM_COLOR;
   cf.crTextColor = color;    //设置颜色
  }
  if (size)
  {
   cf.dwMask|=CFM_SIZE;
   cf.yHeight =200;                //设置高度
  }
  cf.dwMask|=CFM_FACE;
  wcscpy(cf.szFaceName , strFont.GetBuffer(0));//设置字体
  SetSelectionCharFormat(cf);
  SetSel(-1,-1);
  SetSelectionCharFormat(oldCf);
 }

GetSelectionCharFormat
SetSelectionCharFormat
这两个函数是CRichEditUI自带的,所以用起来很方便,只需要调用就可以实现。
还要考虑到的一个问题是:窗口透明(包括CRichEditUI的背景颜色、边框颜色、disabled颜色、窗口响应事件透明)


2.CLabelUI可以实现:

使用PaintText当中的

CRenderEngine::DrawText(hDCm_pManagerrcm_sTextm_dwTextColor, m_iFontDT_SINGLELINE | m_uTextStyle);
使用DrawText的缺陷是无法精确的计算字符串的宽度,比如一个字符串为“abc”,我想让b高亮的话,就会绘制三次来达到效果, a、 b(使用不同的样式)、 c、;但是问题就出在这里,不能精确计算字符串a、b、c、的宽度无法完成后面字符串的绘制,不是覆盖就是有空隙。。。。。。
下面使用DrawHTMLText,这个在实现起来就简单的多了。
CRenderEngine::DrawHtmlText(hDCm_pManagerrcm_strTextValue.c_str(), m_dwTextColor, NULLNULLnLinksDT_SINGLELINE | m_uTextStyle);
自行设置m_strTextValue让它自己去解析,完成绘制。
    //   Bold:             <b>text</b>
    //   Color:            <c #xxxxxx>text</c>  where x = RGB in hex
    //   Font:             <f x>text</f>        where x = font id
    //   Italic:           <i>text</i>
    //   Image:            <i x y z>            where x = image name and y = imagelist num and z(optional) = imagelist id
    //   Link:             <a x>text</a>        where x(optional) = link content, normal like app:notepad or http:www.xxx.com
    //   NewLine           <n>                  
    //   Paragraph:        <p x>text</p>        where x = extra pixels indent in p
    //   Raw Text:         <r>text</r>
    //   Selected:         <s>text</s>
    //   Underline:        <u>text</u>
    //   X Indent:         <x i>                where i = hor indent in pixels
    //   Y Indent:         <y i>                where i = ver indent in pixels 
按照上述样式,可进行绘制操作。

比如构造一个字符串设置颜色:<c #ff00ff>test</c>  此字符串前缀长度11 后缀长度4
<c #ff00ff><b><s>test</s></b></c>    构造成类似这种字符串,控件会自动以HTML格式解析绘制。
……
使用数组(非常简单)
先得到关键字(这个不必多数奥,还是切分)
新建bool数组     ( new一个bool数组长度为当前字符串的长度。 用来标识当前字符是否需要set style,哈哈)
bool数组位置对应的地方前后为true则需要加上标签开始与结尾 ,组合成的字符串拿来绘制就可以了。







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Duilib中,我们可以使用按钮控件(CButtonUI)来实现一个按住就变化的按钮。具实现步骤如下: 1. 创建一个CButtonUI控件,并设置它的普通状态和按下状态的背景图片。 ```cpp <Control name="btnChange" class="ButtonUI" pos="100, 100, 100, 40" normalimage="btn_normal.png" hotimage="btn_hot.png" pushedimage="btn_pushed.png" /> ``` 2. 在按钮控件的事件处理函数中,监听鼠标按下和松开事件。 ```cpp class CMyWnd : public CWindowWnd, public INotifyUI { public: virtual void Notify(TNotifyUI& msg) { if (msg.sType == _T("click")) { if (msg.pSender->GetName() == _T("btnChange")) { // do something } } else if (msg.sType == _T("mouseenter")) { if (msg.pSender->GetName() == _T("btnChange")) { msg.pSender->SetBkImage(_T("btn_hot.png")); } } else if (msg.sType == _T("mouseleave")) { if (msg.pSender->GetName() == _T("btnChange")) { msg.pSender->SetBkImage(_T("btn_normal.png")); } } } }; ``` 3. 在鼠标进入按钮控件和离开按钮控件时,更改按钮控件的背景图片。 ```cpp else if (msg.sType == _T("mouseenter")) { if (msg.pSender->GetName() == _T("btnChange")) { msg.pSender->SetBkImage(_T("btn_hot.png")); } } else if (msg.sType == _T("mouseleave")) { if (msg.pSender->GetName() == _T("btnChange")) { msg.pSender->SetBkImage(_T("btn_normal.png")); } } ``` 完整代码示例: ```cpp #include <Windows.h> #include <stdio.h> #include "UIlib.h" using namespace DuiLib; class CMyWnd : public CWindowWnd, public INotifyUI { public: virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); } virtual CDuiString GetSkinFile() { return _T("test.xml"); } virtual CDuiString GetSkinFolder() { return _T(""); } virtual void Notify(TNotifyUI& msg) { if (msg.sType == _T("click")) { if (msg.pSender->GetName() == _T("btnChange")) { // do something } } else if (msg.sType == _T("mouseenter")) { if (msg.pSender->GetName() == _T("btnChange")) { msg.pSender->SetBkImage(_T("btn_hot.png")); } } else if (msg.sType == _T("mouseleave")) { if (msg.pSender->GetName() == _T("btnChange")) { msg.pSender->SetBkImage(_T("btn_normal.png")); } } } virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; BOOL bHandled = TRUE; if (uMsg == WM_CREATE) { m_PaintManager.Init(m_hWnd); CDialogBuilder builder; CControlUI* pRoot = builder.Create(_T("test.xml"), (UINT)0, NULL, &m_PaintManager); ASSERT(pRoot && "Failed to parse XML"); m_PaintManager.AttachDialog(pRoot); m_PaintManager.AddNotifier(this); return lRes; } else if (uMsg == WM_NCACTIVATE) { if (!::IsIconic(*this)) { return (wParam == 0) ? TRUE : FALSE; } } if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes)) bHandled = TRUE; if (!bHandled) return CWindowWnd::HandleMessage(uMsg, wParam, lParam); return lRes; } protected: CPaintManagerUI m_PaintManager; }; int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow) { HRESULT Hr = ::CoInitialize(NULL); if (FAILED(Hr)) return 0; CMyWnd* pFrame = new CMyWnd(); if (pFrame == NULL) return 0; pFrame->Create(NULL, _T("Duilib Test"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); pFrame->CenterWindow(); pFrame->ShowWindow(true); CPaintManagerUI::MessageLoop(); ::CoUninitialize(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值