Reputation: 11
I am trying to customize tabcontrol in compact framework but i cannot find solution for set transparent background for this control. I am trying to override "OnPaintBackground()" method to begin set background on it but this function not called. How i can use this function to be called when the control created?
Upvotes: 1
Views: 1987
Reputation: 121
I cannot find ondrowitem in tabcontrol to override it. windows CE environment(smartDevice) not support all functionallity in windows forms
Upvotes: 1
Reputation: 2436
EDIT: I read your comment about trying to set the background color of the container, not the individual tabs, and I did some experimenting and research.
It seems that the OnDrawItem method of the TabControl class is used to draw the tab "headers" (the part of the control that contain each tab's text, which the user clicks on to select tabs), as well as the background of the container (everything besides the selected tab's contents, which are drawn by the tab itself in its OnPaintBackground method).
You can get the background of the TabControl to be transparent by overriding its OnDrawItem
method, but simply filling the bounds passed with the DrawItemEventArgs
will also make the tab headers transparent, making them un-clickable (the click will go through the form, onto whatever's behind it).
The way I see it, you've got a couple of options to try to work around this:
class TransparentisTabControl : TabControl { //Without declaring this as new, you'd probably get a warning saying this property hides a member of the base class. //The base class's BackColor property, as I'm sure you've found out, //is hidden with attributes anyway, so it doesn't really matter. public new Color BackColor {get; set;} public TransparentishTabControl(Color backColor) { if (backColor.A == 0) throw new ArgumentException("The alpha component of backColor cannot be zero, or this TransparentisTabControl's tab pages won't be selectable."); BackColor = backColor; } protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(fake); e.Graphics.Clear(BackColor); } }
This code works fine for me, but it might behave differently on your target platform. Let me know if you need any more help/clarification :)
Are you trying to make the tab control transparent, or the individual tab pages? Overriding OnPaintBackground in a TabControl-derrived class isn't enough because each TabPage paints itself as well. You need a custom class that is derived from TabPage and has an override of OnPaintBackground.
class TransparentTabPage : TabPage
{
public TransparentTabPage()
: base("TransparentTabPage")
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
Form form = FindForm();
e.Graphics.CompositingMode = CompositingMode.SourceCopy;
using (SolidBrush sb = new SolidBrush(form.TransparencyKey))
e.Graphics.FillRectangle(sb, Bounds);
}
}
For this to work, your form needs to have its TransparencyKey set to something, and the value of its AllowTransparency property must be true.
Upvotes: 1