1

I have spent today searching everywhere for a concrete explanation or example of coloring the thumb on a trackbar (slider) in win32 C++. Everything I've found has been partially explained, and in trying every conceivable variation I have come up blank.

The control I have been focused on is defined in my rc file as CONTROL "",IDC_PLAYSLIDER,"msctls_trackbar32",TBS_NOTICKS | WS_TABSTOP,5,22,187,15

Essentially, my message handling of NM_CUSTOMDRAW comes down to the following. I have no confidence on my color/hdc handling, but the lack of messages is my primary problem.

INT_PTR CALLBACK dialogproc(HWND h, UINT m, WPARAM w, LPARAM l)
{
    switch (m) {
    case WM_NOTIFY:
    {
        switch (((LPNMHDR)l)->code) {
        case NM_CUSTOMDRAW:
        {
            LPNMCUSTOMDRAW lpNMCD = (LPNMCUSTOMDRAW)l;
            UINT idc = lpNMCD->hdr.idFrom;

            switch (lpNMCD->dwDrawStage) {
            case CDDS_PREPAINT:
                return   CDRF_NOTIFYSUBITEMDRAW;
                break;
            case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
            {
                switch (lpNMCD->dwItemSpec)
                {
                case TBCD_THUMB:
                    HGDIOBJ old_pen = SelectObject(lpNMCD->hdc, penSlider);
                    HGDIOBJ old_brush = SelectObject(lpNMCD->hdc, brushSlider);
                    return CDRF_NEWFONT;
                }
            }
            break;
        }

What I am getting at runtime is a CDDS_PREPAINT on the correct control, but no matter what I have tried, I have had no further CDDS_ drawStage messages.

If anyone has done this on a trackbar (most examples are list controls) and is willing to share their message handler code, or can otherwise shed light on my confusion, that would be greatly appreciated.

  • Aside from the special steps that you need to take to return values from `DialogProc` (pointed out by Jonathan's answer), you are likely to be disappointed with the results. Custom-drawing the track bar control will disable visual styles (themes), making it look much like it did on Windows 95. It will be much more difficult to change the color of a control while retaining visual styles support. Which begs the question of why you need to change the color in the first place—the user's supposed to be able to pick that by selecting a theme. – Cody Gray - on strike Sep 05 '14 at 06:24
  • @CodyGray: Users haven't been able to select a theme since Windows XP :) – Jonathan Potter Sep 05 '14 at 06:49
  • @CodyS.Pumpkins: I don't know where you've seen that behavior. In Win 7 I was able to custom draw the tics of a trackbar using CDRF_SKIPDEFAULT on CDDS_ITEMPREPAINT TBCD_TICS while still getting the rest of trackbar drawn (by Windows itself) with themed visual style. – Fizz Oct 27 '17 at 21:13
  • @Fizz The question here is about the thumb, not the ticks. – Cody Gray - on strike Oct 28 '17 at 06:29
  • @CodyS.Pumpkins: For the thumb it works better actually, it turns out that the [tics are bugged](https://stackoverflow.com/questions/46984124/while-custom-drawing-tics-for-a-trackbar-i-get-all-zeros-or-nonsense-for-the-r): you get some nonsense RECT for the tics, but it's okay for the thumb. – Fizz Oct 28 '17 at 14:28

1 Answers1

1

From the docs for NM_CUSTOMDRAW:

If this message is handled in a dialog procedure, you must set the return value as part of the window data before returning TRUE. For more information, see DialogProc.

The DialogProc docs say:

If the dialog box procedure processes a message that requires a specific return value, the dialog box procedure should set the desired return value by calling SetWindowLong(hwndDlg, DWL_MSGRESULT, lResult) immediately before returning TRUE

Note that with the advent of 64-bit windows it is better practice to use SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, lResult).

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Fabulous! I have it tracking into the thumb handler code. I am using a globally defined HBRUSH and HPEN to give to hdc via SelectObject(). I'm assuming that is the wrong thing to do (based on it not working :p). Suggestions on the right approach? – Wayne McHugh Sep 05 '14 at 06:30
  • I've never tried to custom-draw the trackbar but I'd guess that it doesn't actually use the current brush/pen. Your best bet is probably to draw it yourself rather than trying to coerce it into changing color. – Jonathan Potter Sep 05 '14 at 06:48