矢量中的按钮不响应自定义绘图

Buttons in vector not responding to custom drawing

我有一个 Win32 程序,其中有一个矢量,其中包含一些我想自定义绘制的按钮的 HWND。我可以自己绘制它们,但我只想更改背景颜色,因此似乎没有必要这样做。然而,它们似乎并没有因为定制绘制而改变。他们看起来一样。

我这样创建按钮:

#define ID_BUTTON1 40000

std::vector<HWND> ButtonsVector;

for (int i = 0, i > NumButtons, i++) {
    ButtonsVector.push_back(CreateWindowEx(
                NULL,
                L"BUTTON",  // Predefined class; Unicode assumed 
                L"X",      // Button text 
                WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
                x,         // x position 
                y,         // y position (substitute some x and y values for these) 
                20,        // Button width
                20,        // Button height
                hWnd,     // Parent window 
                (HMENU)ID_BUTTON1 + i,       // Unique button ID
                (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
                NULL));
}

我用这个WM_NOTIFY画的:

HBRUSH hHotBrush;
HBRUSH hDefaultBrush;

case WM_NOTIFY:
    {
            LPNMHDR some_item = (LPNMHDR)lParam;

            if (some_item->idFrom >= ID_BUTTON1 && some_item->code == NM_CUSTOMDRAW)
            {
                LPNMCUSTOMDRAW item = (LPNMCUSTOMDRAW)some_item;


                if (item->uItemState & CDIS_HOT) //Our mouse is over the button
                {
                    //Select our color when the mouse hovers our button
                    if (hHotBrush == NULL)
                        hHotBrush = CreateSolidBrush(RGB(230, 0, 0));

                    HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));

                    HGDIOBJ old_pen = SelectObject(item->hdc, pen);
                    HGDIOBJ old_brush = SelectObject(item->hdc, hHotBrush);

                    RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);

                    SelectObject(item->hdc, old_pen);
                    SelectObject(item->hdc, old_brush);
                    DeleteObject(pen);

                    return CDRF_DODEFAULT;
                }

                //Select our color when our button is doing nothing
                if (hDefaultBrush == NULL)
                    hDefaultBrush = CreateSolidBrush(RGB(255, 0, 0));

                HPEN pen = CreatePen(PS_INSIDEFRAME, 0, RGB(0, 0, 0));

                HGDIOBJ old_pen = SelectObject(item->hdc, pen);
                HGDIOBJ old_brush = SelectObject(item->hdc, hDefaultBrush);

                RoundRect(item->hdc, item->rc.left, item->rc.top, item->rc.right, item->rc.bottom, 5, 5);

                SelectObject(item->hdc, old_pen);
                SelectObject(item->hdc, old_brush);
                DeleteObject(pen);

                return CDRF_DODEFAULT;
            }
    }
    break;

我需要添加以下行:

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version = '6.0.0.0' processorArchitecture = '*' publicKeyToken = '6595b64144ccf1df' language = '*'\"")

这确保使用 ComCtrl32.dll 版本 6,允许 custom-drawing 按钮控件。但是,您必须注意的一件事是您的应用程序要针对哪个版本的 Windows,因为早于 XP 的版本不包含此版本的 dll。