简体   繁体   English

WINAPI:使用自定义绘图更改(单选)按钮的颜色?

[英]WINAPI: Change color of (Radio) Button using Custom Draw?

I simply need to change the text color of a radio control sometimes, so in the dialog procedure I setup:我有时只需要更改单选控件的文本颜色,因此在我设置的对话框过程中:

case WM_NOTIFY:
{
  if (reinterpret_cast<LPNMCUSTOMDRAW>(lparam)->hdr.idFrom==IDC_RADIOMYCONTROLID) {
   if (reinterpret_cast<LPNMCUSTOMDRAW>(lparam)->hdr.code==NM_CUSTOMDRAW) {
      switch (reinterpret_cast<LPNMCUSTOMDRAW>(lparam)->dwDrawStage) {
        case CDDS_PREPAINT:
          // let it know we want to modify things.
          SetWindowLongPtr(hwnddlg, DWLP_MSGRESULT, CDRF_NOTIFYITEMDRAW);
          return TRUE;

        case CDDS_ITEMPREPAINT:
          // this is where we set up the colors
          SetTextColor(reinterpret_cast<LPNMCUSTOMDRAW>(lparam)->hdc, RGB(255, 0, 0));
          SetBkMode(reinterpret_cast<LPNMCUSTOMDRAW>(lparam)->hdc,TRANSPARENT);
          SetWindowLongPtr(hwnddlg, DWLP_MSGRESULT, CDRF_NEWFONT);
          return TRUE;
      }
    }
    break;
  }

  //...

  break;
}

The problem is that the CDDS_ITEMPREPAINT is not received.问题是没有收到 CDDS_ITEMPREPAINT。 In fact there are no more NM_CUSTOMDRAW messages after the CDDS_PREPAINT.事实上,在 CDDS_PREPAINT 之后没有更多的 NM_CUSTOMDRAW 消息。

At first I was returning CDRF_NOTIFYITEMDRAW directly until I remembered you have to put it in DWLP_MSGRESULT , but dang it, it still not working.起初我直接返回 CDRF_NOTIFYITEMDRAW 直到我记得你必须把它放在DWLP_MSGRESULT中,但是该死,它仍然无法正常工作。

Any ideas on how to do this?关于如何做到这一点的任何想法?

Thanks!!谢谢!!

Buttons don't have "items" and so don't send the item (or sub-item) notifications.按钮没有“项目”,因此不发送项目(或子项目)通知。 The only notifications they send are CDDS_PREERASE , CDDS_PREPAINT , CDDS_POSTPAINT and CDDS_POSTERASE .他们发送的唯一通知是CDDS_PREERASECDDS_PREPAINTCDDS_POSTPAINTCDDS_POSTERASE

However even if you change your code to set the text color when CDDS_PREPAINT is received, I don't think this is going to work for you.但是,即使您在收到CDDS_PREPAINT时更改代码以设置文本颜色,我认为这对您不起作用。 Unless themes are disabled, the button control takes its colors from the current system theme (visual style) and as far as I know ignores changes you make in response to NM_CUSTOMDRAW .除非主题被禁用,否则按钮控件会从当前系统主题(视觉样式)中获取其 colors,据我所知,它会忽略您为响应NM_CUSTOMDRAW的更改。

One solution would be to completely custom draw the control when handling CDDS_PREPAINT , but that's a lot more work.一种解决方案是在处理CDDS_PREPAINT时完全自定义绘制控件,但这需要更多的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM