如何更改列表视图的边框颜色 [英] How to change border color of a listview
问题描述
我已经使用win32 api创建了一个列表视图.
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?
推荐答案
您可以使用 WM_NCPAINT
来绘制控件的非客户区域,如下所示:
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);
旁注,(char*)L"text"
没有任何意义.使用ANSI((char*)"text"
)或Unicode(推荐(wchar_t*)L"text"
).您可以将CreateItem
更改为接受const wchar_t*
,然后在最后一步将LVITEM
强制转换为(wchar_t*)
,以避免出现错误.
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.
编辑
WM_NCPAINT
将中断,不返回零.
Edit
WM_NCPAINT
will break, not return zero.
这篇关于如何更改列表视图的边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!