views:

177

answers:

1

I'm currently writing a custom-designed tab control. I created my own control instead of owner-drawing the TabControl because I figured it'd be faster and more flexible. My tab control styles itself after the VS2008 tab control; that is, when a tab is selected, part of that tab is in front of other, unselected tabs.

My tab control consists of a Panel containing all of my TabButton objects which are the actual tabs themselves. I've set the TabButton to be transparent like so:

public TabButton()
{
     ...
     SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
     UpdateStyles();
     this.BackColor = Color.Transparent;
}

However, when the tab is selected and in front of another tab, the portion of the unselected tab that should appear behind the portion of the selected tab is not rendered. It's the standard SystemColors.Control color that fills in the rest of the clipping rectangle for the selected button.

How can I achieve proper transparency in my custom control?

Also: TabButton inherits Control, that's why I needed to use SetStyle in order to use transparency.

-Eric Smith

+1  A: 

Have a look at this: http://saftsack.fs.uni-bayreuth.de/~dun3/archives/creating-a-transparent-panel-in-net/108.html

Last I was in .NET Windows Forms it wasn't readily apparent that you needed to do some of these psuedo hacks to get transparency to work. I'm not sure if they changed it in the last ~3 years, but I do remember doing something like this. I would try explicitly not drawing a background first.

popester
This works, to an extent. You'll see what I mean:Before 'Hack': http://i34.tinypic.com/35mp8xd.pngAfter 'Hack': http://i36.tinypic.com/351e8fq.pngNote both before and after pics are without SetStyle() used. Also, I attempt to use BringToFront() on the selected tab so it renders in front, however this seems to do nothing.While this definitely helped, it only created another problem :P
Eric Smith
Through more investigation, even adding the TabButton dead last and after my control-adding for loop, it still seems to render it with a z-index beneath the other tabs, even though I've specifically called BringToFront().
Eric Smith
So... you were correct. Upon rewriting part of my code it miraculously worked. My guess is I was accidentally adding it to "this" instead of tabPanel, or it was a miracle. Either way, thank you sir! :)
Eric Smith
That is odd, its definitely a z-order issue. What's even weirder is that it appears that the selected tab is behind both the left and right tabs. In your 'before' picture it does appear to draw on top of the right tab correctly. Its also curious about what's going on with the right most tab...?
popester
Oh cool, got me in mid-reply. Have fun!
popester