NM_CUSTROMDRAW消息用于TOOLBAR如何设置高亮时按钮文本

在TOOLBAR中设置 NMTBCUSTOMDRAW结构体,以改变文本的颜色, 我已经成功改变TOOLBAR文本颜色, 但是 如何设置高亮文本 那就一头雾水 了

void MyToolBar::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) //on draw item
{

   NMTBCUSTOMDRAW* pLVCD = reinterpret_cast<NMTBCUSTOMDRAW*> (pNMHDR);

   DWORD DW = *pResult;
   *pResult = 0;    // First thing - check the draw stage(阶段). If it's the control's prepaint(preprint)

    COLORREF crText; 
    COLORREF crHighText;
   if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )           //before a painting cycle begin 
   {
       *pResult = CDRF_NOTIFYITEMDRAW;                    //自己完成绘制
   }
   else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )          //before an item is draw
   {
       // This is the prepaint stage for an item. Here's where we set the
       // item's text color. Our return value will tell Windows to draw the
       // item itself, but it will use the new color we set here.
       // We'll cycle the colors through red, green, and light blue.

        crText = RGB(255,0,0);
        crHighText = RGB(0, 255, 0);
        pLVCD->clrText = crText;        // Tell Windows to paint the control itself.
        pLVCD->clrTextHighlight = crHighText;
        *pResult = CDRF_NEWFONT;

   }

}

如上, 我设置了,但是鼠标移动到TOOLBAR控件按钮上时, 文本颜色没有变化啊~

该回答引用ChatGPT

NM_CUSTOMDRAW 消息用于自定义绘制控件的外观。对于工具栏控件,为了改变高亮时按钮文本的颜色,你需要在 CDDS_ITEMPREPAINT 阶段的处理过程中,设置 clrHighlightHotTrack 属性为你想要的高亮文本颜色即可。修改后的代码如下:

void MyToolBar::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) //on draw item
{

   NMTBCUSTOMDRAW* pLVCD = reinterpret_cast<NMTBCUSTOMDRAW*> (pNMHDR);

   DWORD DW = *pResult;
   *pResult = 0;    // First thing - check the draw stage(阶段). If it's the control's prepaint(preprint)

    COLORREF crText; 
    COLORREF crHighText;
   if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )           //before a painting cycle begin 
   {
       *pResult = CDRF_NOTIFYITEMDRAW;                    //自己完成绘制
   }
   else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )          //before an item is draw
   {
       // This is the prepaint stage for an item. Here's where we set the
       // item's text color. Our return value will tell Windows to draw the
       // item itself, but it will use the new color we set here.
       // We'll cycle the colors through red, green, and light blue.

        crText = RGB(255,0,0);
        crHighText = RGB(0, 255, 0);
        pLVCD->clrText = crText;       
        pLVCD->clrTextHighlight = crHighText;
        pLVCD->clrHighlightHotTrack = crHighText; // 设置高亮时的文本颜色
        *pResult = CDRF_NEWFONT;

   }
}

这样,当鼠标移动到按钮上时,文本颜色就会变成你所设置的高亮文本颜色。