Home > Net >  WINAPI: Change color of (Radio) Button using Custom Draw?
WINAPI: Change color of (Radio) Button using Custom Draw?

Time:08-04

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. In fact there are no more NM_CUSTOMDRAW messages after the CDDS_PREPAINT.

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.

Any ideas on how to do this?

Thanks!!

CodePudding user response:

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.

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. 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.

One solution would be to completely custom draw the control when handling CDDS_PREPAINT, but that's a lot more work.

  • Related