INTRODUCTION AND RELEVANT INFORMATION:
I wanted to create a transparent list view using macros
ListView_SetTextBkColor
and
ListView_SetBkColor
.
I have made a test application with
Visual Studio project wizard
( a simple Win32 project ) to test these API's.
I have added this
WM_CREATE
handler:
case WM_CREATE:
{
CreateWindowEx( 0, WC_LISTVIEW, L"TEST",
WS_CHILD | WS_VISIBLE | LVS_REPORT,
50, 50, 250, 150,
hWnd, (HMENU)4000, hInst, NULL );
ListView_SetExtendedListViewStyle( GetDlgItem( hWnd, 4000 ),
LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES );
wchar_t temp[3];
LVCOLUMN lvc;
memset( &lvc, 0, sizeof(lvc) );
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvc.cx = 30;
lvc.fmt = LVCFMT_LEFT;
for( int i = 0; i < 8; i++ )
{
lvc.iSubItem = i;
memset( temp, L'\0', sizeof(temp) );
swprintf_s( temp, 3, L"%d", i );
lvc.pszText = temp;
ListView_InsertColumn( GetDlgItem( hWnd, 4000 ), i, &lvc );
}
LVITEM lvi;
lvi.mask = LVIF_TEXT;
for( int i = 0; i < 9; i++ )
{
lvi.iItem = i;
for( int j = 0;
j < Header_GetItemCount( ListView_GetHeader(
GetDlgItem( hWnd, 4000 ) ) ); j++ )
{
lvi.iSubItem = j;
memset( temp, L'\0', sizeof(temp) );
swprintf_s( temp, 3, L"%d%d", i, j );
lvi.pszText = temp;
if( !j ) ListView_InsertItem( GetDlgItem( hWnd, 4000 ), &lvi );
else ListView_SetItem( GetDlgItem( hWnd, 4000 ), &lvi );
}
}
ListView_SetTextBkColor( GetDlgItem( hWnd, 4000 ), CLR_NONE );
ListView_SetBkColor( GetDlgItem( hWnd, 4000 ), CLR_NONE );
}
return 0L;
To make things more apparent for the testing, I have changed the background of the main window, in my window class to light gray like this:
wcex.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
In the
stdafx.h
I have added
#include<commctrl.h>
, and I have performed linking in my
.cpp
file with
#pragma comment( lib, "comctl32.lib")
.
I have
enabled Visual Styles
with this directive:
#pragma comment( linker, "/manifestdependency:\"type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
language='*'\"")
I have initiated common controls in my
_tWinMain
like this:
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_STANDARD_CLASSES | ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&iccex);
I work in
Visual Studio Express 2008
, using
C++
and
pure Win32 API
.
These are all the relevant information required to reproduce the problem I am facing, which I will continue to describe bellow.
PROBLEM:
When I run the test application, items in the first column become black ( it is as if the edit control does not paint its background ).
Subitems are all properly transparent.
When I select one row, it gets highlighted properly, with blue color.
It is then when I can see the text of the first item ( since the background is now blue ) and the text is OK, there are no artifacts nor any other problems as far as I can tell.
I have tried to add items only, without subitems, thinking that the problem lies in my inserting of the items and subitems, but the effect is the same.
If I comment out the adding of
LVS_EX_FULLROWSELECT
style in my call to
ListView_SetExtendedListViewStyle
then everything works fine.
I have tried disabling
Visual Styles
but the problem persists.
I am targeting
XP
as well, so including
LVS_EX_TRANSPARENTBKGND
style will not help me.
MY EFFORTS TO SOLVE THE PROBLEM:
I have tried to find the answer in
StackOverflow
and
CodeProject
archives, but there is nothing documented on this topic.
I have tried searching the Internet but have found nothing.
UPDATED CONTENT:
If I delete the first column, the remaining subitems remain, and are properly transparent.
The only way I can exploit this would be to add "dummy" column at the beginning, then fill the items with some "dummy" values, which would allow me to put the relevant data into subitems. After I am done with that, I could simply delete the first column.
QUESTION:
Is there a better way to solve this problem?
I really would like to keep both grid lines and full row selection, and also to avoid the "hack" listed in the section MY EFFORTS TO SOLVE THE PROBLEM.
Thank you.
Best regards.