Code-Lover
Code-Lover

Reputation: 309

How to change border color of a listview

I have created a list view using win32 api.

InitCommonControls();
HWND hwndList1 = CreateWindow(WC_LISTVIEW , L"", WS_VISIBLE|WS_CHILD | LVS_REPORT | LVS_EDITLABELS |  LVS_ICON  | LV_VIEW_TILE |  LVS_EX_GRIDLINES | WS_BORDER | LVS_EX_FULLROWSELECT | ES_LEFT , 10, 10, 300, 190, hwnd, NULL, GetModuleHandle(NULL), 0); 

SendMessageW( hwndList1,
            LVM_SETEXTENDEDLISTVIEWSTYLE,
            LVS_EX_FULLROWSELECT ,
            LVS_EX_FULLROWSELECT );



CreateItem(hwndList1 , (char*)L"fault RS458");
CreateItem(hwndList1 , (char*)L"fault RS455");
CreateColumn(hwndList1 , 0 , (char*)L"Insert column" , 300);

I see a black border around the list view. How can i change its color?

Upvotes: 3

Views: 1802

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

You can subclass the window using SetWindowSubclass (requires comctl32.lib) and handle WM_NCPAINT to paint the non-client area of the control as follows:

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

LRESULT CALLBACK ListViewProc(HWND hwnd, 
    UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR)
{
    switch(msg)
    {
    case WM_NCPAINT:
    {
        RECT rc;
        GetWindowRect(hwnd, &rc);
        OffsetRect(&rc, -rc.left, -rc.top);
        auto hdc = GetWindowDC(hwnd);
        auto hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
        auto oldpen = SelectObject(hdc, hpen);
        SelectObject(hdc, GetStockObject(NULL_BRUSH));
        Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);//draw red frame
        SelectObject(hdc, oldpen);
        DeleteObject(oldpen);
        ReleaseDC(hwnd, hdc);

        //*** EDIT
        //documentation says we should return 0
        //but that causes problem with vertical scrollbar
        //maybe we should break for this subclass case

        break; //not return 0!
    }

    case WM_NCDESTROY:
        RemoveWindowSubclass(hwnd, ListViewProc, 0);
        break;
    }

    return DefSubclassProc(hwnd, msg, wp, lp);
}
...
HWND hwndList1 = CreateWindow(...); 
SetWindowSubclass(hwndList1, ListViewProc, 0, NULL);

Side note, (char*)L"text" doesn't make sense. Either use ANSI ((char*)"text") or Unicode ((wchar_t*)L"text", recommended). You can change CreateItem to accept const wchar_t*, then cast to (wchar_t*) for LVITEM at the last step to avoid errors.

Edit
WM_NCPAINT will break, not return zero.

Upvotes: 3

Related Questions