首页 > 解决方案 > 矢量中的按钮不响应自定义绘图

问题描述

我有一个 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;

标签: c++winapivectorhwnd

解决方案


我需要添加以下行:

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

这可确保使用 ComCtrl32.dll 版本 6,从而允许自定义绘制按钮控件。但是,您必须注意的一件事是您的应用程序所针对的 Windows 版本,因为 XP 之前的版本不包含此版本的 dll。


推荐阅读