Click here to Skip to main content
14,732,999 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone!

I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.

I have made a tree view control in dialog box, using resource editor.

Tree view also has images, lines and buttons.

I just need instructions on how to make it's background transparent.

Tree's nodes also should have transparent background.

I think that I should use custom draw for this, but I am not very experienced, that is why I ask for help.

Code example, or snippet would be very helpful, but I would also appreciate written instructions as well, but make them detailed please, since I am not very experienced.

I work in MS Visual Studio Express 2008, on Windows XP, in C++, using pure WIN32 API.

That would be all, again I thank everyone who tries to help.Thank you very very much!

EDIT #1:
------------------------------

I have tried subclassing the tree view, and got it to have transparent background, but tree's items don't show.

I am posting the code of the subclassing procedure here in hope that someone can help me with some advice:

LRESULT CALLBACK wpTree(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{

	case WM_PAINT:
		{
			PAINTSTRUCT ps;

			HDC hdc = BeginPaint(hWnd, &ps), 
                            hdcMem = CreateCompatibleDC(hdc);
		
			RECT r;

			GetClientRect( hWnd, &r );

			HBITMAP bmp = CreateCompatibleBitmap( hdcMem,
                                          r.right - r.left, r.bottom - r.top ),
                                bmpOld;

			bmpOld = (HBITMAP)SelectObject( hdcMem, bmp );

			TransparentBlt( hdc, 0, 0, r.right - r.left,
                                        r.bottom - r.top, hdcMem, 0, 0, 
                                        r.right - r.left,
                                        r.bottom - r.top, 
                                        RGB( 0, 0, 0 ) );

			SelectObject( hdcMem, bmpOld );

			DeleteObject(bmp);

			DeleteDC(hdcMem);

			EndPaint(hWnd, &ps);

		}
		return 0L;

	case WM_ERASEBKGND:
		
		return 0L;
	}

	return CallWindowProc( wp, hWnd, message, wParam, lParam );
}
Posted
Updated 21-Jul-13 15:56pm
v2
Comments
[no name] 20-Jul-13 11:06am
   
http://www.google.com/search?q=win32+transparent+tree+control
MyOldAccount 20-Jul-13 14:30pm
   
I need one example in pure WIN32 API.
Unfortunately I couldn't find any, but thank you anyway...
I will keep trying.
KarstenK 22-Jul-13 5:12am
   
an MFC example can manually divided in Win32-API by doing every step manually.

This task should help you to learn and understand that stuff. (it would help you to train your skills as programmer) ,-)
MyOldAccount 22-Jul-13 14:03pm
   
Do you have a simple MFC example where tree's nodes and background are transparent ?

I have tried Google, of course, but I just couldn't find what I need.

Anyway, I am a beginner, I thought this site is for helping people with instructions, at least, so please help me if you can, I ask you kindly since I am desperate at this moment.

Here is outstanding and deep information about this area:

Custom Controls in Win32 API: The Basics[^]

And look at this: A custom-drawn TreeList Control[^]

BTW: customizing a MS-control is some"heavy lifting", so it isnt suitable for a beginner ;-)
   
v2
Comments
MyOldAccount 23-Jul-13 5:26am
   
Thank you.

I know that customizing MS controls is hard for beginner, but I have no other choice, unfortunately :((
KarstenK 23-Jul-13 6:12am
   
So work carefully through the stuff for which i posted the stuff. It is really the KNOWLEDGE you neesd. Read also the online docs in msdn.com for the API. There is a complete overview about all details.

Why have you "no other choice"?
MyOldAccount 24-Jul-13 12:01pm
   
I have been through both articles you have provided.
It is just too much for me :((

I tried to find an example with the transparency in the first article, yet it only stretches the bitmap, using double buffering.At this point I just can't figure out how to achieve transparency by going through this example :((

I have tried to cope with the second article despite my lack of knowledge of MFC or Windows Forms ( I think it is written in one of those ), but I have failed miserably there as well :((.

Solution to my problem doesn't need to include subclassing, I just need instructions on how to create transparent tree view.
Something like this:

Subclass it;
Handle WM_PAINT like this:
Create compatible bitmap and compatible hdc;
Do this;
Do that;
...

If you can please help me in this manner I would be very very grateful.
I know i am a pain, but please have some understanding, we were all young and inexperienced once, and I am sure you had those days when you "hit the wall" and feel helpless.
I believe that I don't ask too much, just a small set of instruction, to help me push through this.

Thank you, again, for all the help.
KarstenK 25-Jul-13 2:14am
   
One way show transparence is to draw parts of the background image in the control. For that you need to bitblt some bits from the bg image in the control in the background drawi.

Finally set the controls to transparent.

PS: as i said, it isnt a newbies jobs ;-)
MyOldAccount 25-Jul-13 4:30am
   
Thank you for the reply!
Please confirm if I have understood you right:
The way I understand it, you want to say that I should copy the parent's background image into some bitmap, and then BitBlt that bitmap as my tree view's control background, right ?
KarstenK 25-Jul-13 4:53am
   
that is one way, but imho not the best, because you have many objects and operations.

You better have one big global background picture (or try backgrund memory dc with the picture) which is available for each drawiing operation. And from it to copy the needed parts.

tip from own experience: work with pointers and not make copies of objects WHEREEVER possible
MyOldAccount 25-Jul-13 10:08am
   
The second way is with using BitBlt and ROP I guess ?
Is that what you had in mind ?
KarstenK 26-Jul-13 1:53am
   
Yes!!!! :-)
MyOldAccount 26-Jul-13 9:15am
   
But I thought that TransparentBlt API does the same thing as BitBlt, and makes chosen color transparent.

My tree's background IS transparent when I subclass it this way, but I CAN NOT SEE ANY NODES. That is the problem!

Do you know why this is happening ? I suspect on WM_ERASEBKGND, but I don't know what to do...
enhzflep 28-Jul-13 23:19pm
   
Glad to hear you appear to have the problem solved. I didn't really make any headway with it. The best I could do was to set the background of the treeView. The problem however, is that the labels still have an opaque white background.

On the problem of nodes not being displayed once the WindowProc had been changed for the treeView, I found that calling DefWindowProc replicated this problem, while using CallWindowProc along with the value returned from SetWindowLong caused the tree nodes to draw again - albeit with the white text background. It seems the treeview isn't intended to be custom drawn as many other I.e the list-control are. As a consequence, all of the drawing and calculations are done in WM_PAINT - you can't just hijack WM_DRAWITEM or WM_MEASUREITEM messages. Just another legacy of windows, I guess. :)
enhzflep 28-Jul-13 23:19pm
   
Glad to hear you appear to have the problem solved. I didn't really make any headway with it. The best I could do was to set the background of the treeView. The problem however, is that the labels still have an opaque white background.

On the problem of nodes not being displayed once the WindowProc had been changed for the treeView, I found that calling DefWindowProc replicated this problem, while using CallWindowProc along with the value returned from SetWindowLong caused the tree nodes to draw again - albeit with the white text background. It seems the treeview isn't intended to be custom drawn as many other I.e the list-control are. As a consequence, all of the drawing and calculations are done in WM_PAINT - you can't just hijack WM_DRAWITEM or WM_MEASUREITEM messages. Just another legacy of windows, I guess. :)
KarstenK 29-Jul-13 2:08am
   
you need to call the default painting.
MyOldAccount 31-Jul-13 12:30pm
   
I have tried adding ::CallWindowProc(wp, hWnd, message, wParam, lParam); just above PAINTSTRUCT ps; line. The nodes show up, but then entire tree view is painted by default white color. Any help?

Maybe I am calling the default painting the wrong way ?
KarstenK 1-Aug-13 3:01am
   
I know only that the treeview gets draw in stages. Trace the tv-messages Look in the sample codes.
MyOldAccount 1-Aug-13 5:22am
   
OK, I will try that.
There was a solution provided as a link to the project file, on StackOverflow.

Sadly, the poster of it has deleted it recently.

Still, it helped me to make an acceptable transparent tree view control.

Here is the link to the question on StackOverflow, in case that link to the solution becomes active again:

http://stackoverflow.com/questions/17888038/transparent-tree-view-control[^]
   

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