tabcontrol重绘的问题
using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form
{
private Rectangle tabArea;
private RectangleF tabTextArea;
public Form1()
{
TabControl tabControl1 = new TabControl();
TabPage tabPage1 = new TabPage();
// Allows access to the DrawItem event.
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl1.SizeMode = TabSizeMode.Fixed;
tabControl1.Controls.Add(tabPage1);
tabControl1.ItemSize = new Size(80, 30);
tabControl1.Location = new Point(25, 25);
tabControl1.Size = new Size(250, 250);
tabPage1.TabIndex = 0;
ClientSize = new Size(300, 300);
Controls.Add(tabControl1);
TabPage tabPage2 = new TabPage();
tabControl1.Controls.Add(tabPage2);
tabPage2.TabIndex = 1;
tabArea = tabControl1.GetTabRect(0);
tabTextArea = (RectangleF)tabControl1.GetTabRect(0);
tabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab);
}
private void DrawOnTab(object sender, DrawItemEventArgs e)
{
MessageBox.Show(e.Index.ToString());
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red);
Font font = new Font("Arial", 10.0f);
SolidBrush brush = new SolidBrush(Color.Red);
g.DrawRectangle(p, tabArea);
g.DrawString("tabPage1", font, brush, tabTextArea);
}
static void Main()
{
Application.Run(new Form1());
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}
我想重绘tabcontrol,但是只有第一个tab得到了重绘,
------解决方案--------------------
因为你的重绘范围始终是第一个tabpage,所以。。。
tabArea = tabControl1.GetTabRect(0);
tabTextArea = (RectangleF)tabControl1.GetTabRect(0);
//DrawOnTab改成这样就可以了
private void DrawOnTab(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red);
Font font = new Font("Arial", 10.0f);
SolidBrush brush = new SolidBrush(Color.Red);
for (int i = 0; i < tabControl1.TabPages.Count; i++)
{
tabArea = tabControl1.GetTabRect(i);
tabTextArea = (RectangleF)tabControl1.GetTabRect(i);
g.DrawRectangle(p, tabArea);
g.DrawString("tabPage"+(i+1).ToString(),font,brush,tabTextArea);
}
}