TabControl question...

just a thought ..but is there any to make individual tabpages use windows XP style icons? [32bit with 8bit alpha] ..a couple of my developers are wanting to use the new style icons on a tab control and i don't really want to write a custom tabcontrol just to add support for the icons...any thoughts?
[301 byte] By [codefund.com] at [2008-2-10]
# 1
Sure.

- Add an ImageList to your Form.
- Add the icons to your ImageList.
- Set the ImageList property on the TabControl to point to your ImageList
- Set the ImageIndex property on the TabPage to be the icon of choice.

This can all be done in the designer.

- mike

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2
dont think thats going to work as ImageList returns Images() which i believe get rendered down as bitmaps() and dont draw the alpha channel properly..whenever i do it the way you suggested the transparency is missing, resulting in the icons drawing what *should* be the drop shadow with transparency with a harsh jagged black outline around them...so far the suggestion im proposing is to use ownerDraw and use gdi+'s DrawIcon() method and offset the text accordingly...sound like a decent proposal?
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
Unfortunately, OwnerDraw isnt supported on the TabControl. What you could do is create your own class which inherits from TabControl. Then override OnPaint, call base.OnPaint() (MyBase.OnPaint() in VB) and then manually use GDI+ to paint over the area where the icon goes.

Make sense?

- mike

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 4
yea, makes perfect sense..but i did this yesterday and it worked fine...:
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
private Rectangle tabArea;
private System.Windows.Forms.TabControl tabControl1;
private RectangleF tabTextArea;

public Form1()
{
TabControl tabControl1 = new TabControl();
TabPage tabPage1 = new TabPage();
TabPage tabPage2 = new TabPage();
TabPage tabPage3 = new TabPage();

// Allows access to the DrawItem event.
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;

tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.Controls.Add(tabPage1);
tabControl1.Controls.Add(tabPage2);
tabControl1.Controls.Add(tabPage3);
tabControl1.ItemSize = new Size(80, 20);
tabControl1.Location = new Point(25, 25);
tabControl1.Size = new Size(350, 100);
TabControl tc = new TabControl();
TabPage tp = new TabPage("hello");
TabPage tp2 = new TabPage("hello2");
TabPage tp3 = new TabPage("hello3");
tc.ItemSize = new Size(80,20);
tc.Location = new Point(25,200);
tc.Size = new Size(350,100);
tp.TabIndex=0;
tp2.TabIndex=1;
tc.Controls.Add(tp);
tc.Controls.Add(tp2);
tc.Controls.Add(tp3);
tabPage1.TabIndex = 0;
tabPage2.TabIndex = 1;
tabPage3.TabIndex = 2;
ClientSize = new Size(400, 500);
Controls.Add(tabControl1);
Controls.Add(tc);
// Binds the event handler 1DrawOnTab to the DrawItem event
// through the DrawItemEventHandler delegate.
tabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab);
}

// Declares the event handler DrawOnTab which is a method that
// draws a string and Rectangle on the tabPage1 tab.
private void DrawOnTab(object sender, DrawItemEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromName("Control")),e.Bounds);
tabArea = e.Bounds;
tabTextArea = (RectangleF)e.Bounds;
Graphics g = e.Graphics;
Pen p = new Pen(Color.Blue);
Font font = new Font("Arial", 10.0f);
SolidBrush brush = new SolidBrush(Color.Red);
tabArea = new Rectangle(e.Bounds.Left+2,e.Bounds.Top+2,e.Bounds.Width-3,e.Bounds.Height-3);
Icon x = new Icon(this.Icon,16,16);
Rectangle iconRect = new Rectangle(e.Bounds.Left+2,e.Bounds.Top+2,e.Bounds.Height-3,e.Bounds.Width-3);
Rectangle textRect = new Rectangle(e.Bounds.Left+10,e.Bounds.Top+2,e.Bounds.Height-3,e.Bounds.Width-22);
g.DrawIconUnstretched(x,iconRect);
g.DrawString("tab" + e.Index,font,brush,e.Bounds.Left+20,e.Bounds.Top+1);
}

private void InitializeComponent()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Location = new System.Drawing.Point(208, 296);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(648, 413);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tabControl1});
this.Name = "Form1";
this.ResumeLayout(false);

}

static void Main()
{
Application.Run(new Form1());
}
}

am i missing something or is this not ownerdrawn?

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 5
Yes, you are correct, this is indeed Owner Draw. I was getting this confused with a new feature we're working on. I apologize for the confusion.

BTW - those are some clean looking icons :)

- mike

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Designer...