Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
5.00/5 (5 votes)
See more:
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:

C++
case WM_CREATE:
     {
          CreateWindowEx( 0, WC_LISTVIEW, L"TEST",
               WS_CHILD | WS_VISIBLE | LVS_REPORT,
               50, 50, 250, 150, 
               hWnd, (HMENU)4000, hInst, NULL );

          // set listview style->LVS_EX_FULLROWSELECT messes up the transparentsy

          ListView_SetExtendedListViewStyle( GetDlgItem( hWnd, 4000 ),
               LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES );

          // add some columns

          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 );
          }

          // insert some items and subitems

          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 )      // it is item
                         ListView_InsertItem( GetDlgItem( hWnd, 4000 ), &lvi );
                    else          // it is subitem
                         ListView_SetItem( GetDlgItem( hWnd, 4000 ), &lvi );
               }
          }

          // make it transparent

          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:
C++
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:
C++
#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:
C++
// initialize common controls
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.
Posted
Updated 21-Dec-13 10:51am
v3
Comments
Richard MacCutchan 21-Dec-13 13:23pm    
I don't know the answer, but maybe this article may help.

By the way, I voted you a 5 for a very clear and comprehensive question; would that everyone took such care when posting.
AlwaysLearningNewStuff 21-Dec-13 13:36pm    
Thank you, I highly appreciate it.

I am not discouraged though, since I have an idea how to solve it, but do not know how to implement my solution.

If only I could delete the first column and re add it again, I think it would solve the problem.

Best regards.
The_Inventor 22-Dec-13 5:13am    
ListView_SetTextBkColor this one sets the color of the areas in the text-rectangle area that is not text, and not the whole of the control.
ListView_SetBkColor this one sets the initial background of the list view control itself.

For example: Normally the background of the list view is white, which can be changed with the ListView_SetBkColor macro, and normally the text is black, and the pixels within the areas of the text are the same color as the background, white normally, which can be changed using the ListView_SetTextBkColor macro. So when the two contrasting colors appear together you get to see the text in black against a white background. So if the text background is black and the text is black then you wont see the text, just black areas in the list, with maybe white at the end.
AlwaysLearningNewStuff 22-Dec-13 10:33am    
I do not understand.

Can you propose a solution, or at least point me in the right direction?

As I have said, if I turn off the LVS_EX_FULLROWSELECT then everything works fine.

Regards.
The_Inventor 22-Dec-13 21:34pm    
When you use the LVS_EX_FULLROWSELECT is it set to transparent? or is it black? Is it RGB, or RGBA? Usually when in a spreadsheet format, rows and columns, the border styles of each cell is reversed, and the backgrounds remain the same. You colored your LVS_EX_FULLROWSELECT as black I believe. And also since it works with ListView_SetExtendedListViewStyle, then that is your solution since this macro is the one you should be using for what it is that you are doing.

1 solution

OK I see what is the result of your code. What you don't seem to have is ROW LABELS like you do have for your columns. The column is different.
lvi.mask = LVIF_TEXT It is this line of code that appears suspect. One may have presumed that it is a format macro that only allows characters, upper and lower, A-Z, 0-9, to be displayed, which it may not be. I am not sure.

https://drive.google.com/file/d/0B7gJpKIW8nm1OS1Gd0pyeG1DVkE/edit?usp=sharing
This is an updated image of what shows up on my computer, using your code copied and pasted, also shows some of your other code.

It still bothers me that using LVS_EX_FULLROWSELECT affects the COLUMN, on XP, and I am presuming you have applied all of the service packs, SP3 being the last one I know.

When you use the LVS_EX_FULLROWSELECT is it set to transparent? or is it black? Is it RGB, or RGBA? Usually when in a spreadsheet format, rows and columns, the border styles of each cell is reversed, and the backgrounds remain the same. You colored your LVS_EX_FULLROWSELECT as black I believe. And also since it works with ListView_SetExtendedListViewStyle, then that is your solution since this macro is the one you should be using for what it is that you are doing.

So it really appears to be a system issue. I have a Win7Ult64bit OS running on an Intel i7 950 Quad.
 
Share this answer
 
v2
Comments
AlwaysLearningNewStuff 26-Dec-13 23:03pm    
It seems to me that you are right about it being the system issue.

I have posted the same question on StackOverflow and it got downvoted with the comment that "it can not be done on XP" and advising me to "scrape off that idea" of my mind.

The member who advised me so is one of the biggest authorities on that site.

Since my code is really really simple, and in the light of the comments on StackOverflow and yours, I accept the fact that maybe it can not be fixed, and that those API's simply malfunction with LVS_EX_FULLROWSELECT.

Best regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900