c++ 更改ListView滚动条颜色

kupeojn6  于 2023-08-09  发布在  其他
关注(0)|答案(1)|浏览(163)

我一直在尝试自定义Win32控件,如按钮,编辑和静态等。几天前,我试图自定义一个ListView,我已经改变了几乎所有关于它的外观,但后来我意识到它的滚动条。我不喜欢在一个黑暗的主题为基础的列表视图白色滚动条。我做了一些搜索,发现我可以使用WM_CTLCOLORSCROLLBAR,所以我立即改变了ListView的子类,并尝试WM_CTLCOLORSCROLLBAR,但没有得到任何结果。然后我想可能sublcass函数根本不起作用,并尝试在WM_NCPAINT上绘制夫妇,它正在工作。所以唯一不起作用的是WM_CTLCOLORSCROLLBAR。然后我做了一个新的干净的项目,并在其中做了一个简单的窗口和一个ListView,然后改变了子类,只是为了看看它是否能在一个干净的项目上工作,它没有。下面是一个不起作用的示例:

#include <Windows.h>
#include <CommCtrl.h>
#include <string>
#include <iostream>

#pragma comment(lib, "Comctl32.lib")

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

bool isRunning = false;
int exitCode = 0;

HFONT listViewFont;

LRESULT CALLBACK ListViewProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR idSubclass, DWORD_PTR dwRefData) {
    switch (msg) {
    case WM_CTLCOLORSCROLLBAR: {
        HDC hdc = (HDC)wParam;

        SetBkColor(hdc, RGB(240, 240, 240));

        return (LRESULT)CreateSolidBrush(RGB(240, 240, 240));
    } break;
    default:
        return DefSubclassProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_CREATE: {
        HWND listView = CreateWindowA("SysListView32", "", WS_CHILD | LVS_REPORT | LVS_NOCOLUMNHEADER, 50, 50, 250, 20 * 20, hWnd, NULL, NULL, NULL);
        SetWindowSubclass(listView, ListViewProc, 0, 0);
        ListView_SetBkColor(listView, RGB(51, 51, 51));
        ListView_SetExtendedListViewStyle(listView, LVS_EX_FULLROWSELECT);
        SendMessageA(listView, WM_SETFONT, (WPARAM)listViewFont, NULL);
        ShowWindow(listView, true);
        UpdateWindow(listView);

        LVCOLUMN column;
        column.mask = LVCF_WIDTH | LVCF_TEXT;
        column.pszText = LPWSTR(L"Column");
        column.cx = 250;
        ListView_InsertColumn(listView, 0, &column);

        for (size_t i = 0; i < 50; i++) {
            LVITEM item;
            item.pszText = LPWSTR(L"item");
            item.mask = LVIF_TEXT;
            item.iItem = i;
            item.iSubItem = 0;
            ListView_InsertItem(listView, &item);
        };
    } break;
    case WM_CLOSE: {
        isRunning = false;
        PostQuitMessage(exitCode);
    } break;
    default: {
        return DefWindowProcA(hWnd, msg, wParam, lParam);
    } break;
    };
    return DefWindowProcA(hWnd, msg, wParam, lParam);
};

void InitFonts() {
    listViewFont = CreateFontA(
        16, 
        0, 0, 0, 
        FW_NORMAL, 
        FALSE, 
        FALSE, 
        FALSE, 
        DEFAULT_CHARSET,
        OUT_DEFAULT_PRECIS,
        CLIP_DEFAULT_PRECIS,
        DEFAULT_QUALITY,
        DEFAULT_PITCH | FF_DONTCARE,
        "Georgia"
    );
};

int main() {
    InitFonts();

    WNDCLASSEXA wc = { 0 };
    wc.cbSize = sizeof(WNDCLASSEXA);
    wc.hbrBackground = CreateSolidBrush(RGB(255, 255, 255));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "_MainWindow";
    wc.lpfnWndProc = WndProc;

    RegisterClassExA(&wc);

    HWND hWnd = CreateWindowExA(WS_EX_OVERLAPPEDWINDOW, "_MainWindow", "", WS_OVERLAPPEDWINDOW, 500, 200, 500, 500, NULL, NULL, NULL, NULL);
    ShowWindow(hWnd, true);
    UpdateWindow(hWnd);

    isRunning = true;

    MSG msg;
    while (isRunning) {
        if (GetMessageA(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessageA(&msg);
        };
    };

    return exitCode;
}

字符串

gudnpqoy

gudnpqoy1#

在我看来,ListView控件中的滚动条不是滚动条控件。WM_CTLCOLORSCROLLBAR message用于设置scroll bar control的背景色。
就我所知,你不能通过WM_CTLCOLORSCROLLBAR message改变ListView滚动条的颜色。您可以为列表视图控件设置三种颜色:文本颜色、文本背景颜色和窗口背景颜色。
请参阅文件:
列表视图颜色
WM_CTLCOLORSCROLLBAR message

相关问题