一个TabPage页的源程序

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{
 public delegate void onTabpageChanged(int pos);

 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class CtlTabPage : System.Windows.Forms.UserControl
 {
  private System.Windows.Forms.ImageList imageList1;
  private System.Windows.Forms.Panel panel1;
  private System.Windows.Forms.Timer timerShow;
  private System.Windows.Forms.Panel panel2;
  private System.ComponentModel.IContainer components;

  private Stream GetResourceStream(string iconName)
  {
   return GetType().Assembly.GetManifestResourceStream(String.Format("WindowsApplication3.{0}", iconName)) ;
  }

  public CtlTabPage()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   Image img = Image.FromStream(this.GetResourceStream("WindsAdm.Bmp"));
   this.imageList1.Images.AddStrip(img);
   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.imageList1 = new System.Windows.Forms.ImageList(this.components);
   this.panel1 = new System.Windows.Forms.Panel();
   this.timerShow = new System.Windows.Forms.Timer(this.components);
   this.panel2 = new System.Windows.Forms.Panel();
   this.SuspendLayout();
   //
   // imageList1
   //
   this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
   this.imageList1.ImageSize = new System.Drawing.Size(18, 18);
   this.imageList1.TransparentColor = System.Drawing.Color.Fuchsia;
   //
   // panel1
   //
   this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.panel1.Location = new System.Drawing.Point(2, 238);
   this.panel1.Name = "panel1";
   this.panel1.Size = new System.Drawing.Size(428, 32);
   this.panel1.TabIndex = 0;
   this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
   this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
   this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
   this.panel1.MouseLeave += new System.EventHandler(this.panel1_MouseLeave);
   //
   // timerShow
   //
   this.timerShow.Interval = 50;
   this.timerShow.Tick += new System.EventHandler(this.timerShow_Tick);
   //
   // panel2
   //
   this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    | System.Windows.Forms.AnchorStyles.Left)
    | System.Windows.Forms.AnchorStyles.Right)));
   this.panel2.Location = new System.Drawing.Point(2, 2);
   this.panel2.Name = "panel2";
   this.panel2.Size = new System.Drawing.Size(428, 236);
   this.panel2.TabIndex = 1;
   this.panel2.SizeChanged += new System.EventHandler(this.panel2_SizeChanged);
   this.panel2.Paint += new System.Windows.Forms.PaintEventHandler(this.panel2_Paint);
   //
   // Form1
   //
   this.ClientSize = new System.Drawing.Size(432, 270);
   this.Controls.Add(this.panel2);
   this.Controls.Add(this.panel1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  public event onTabpageChanged tabClick;

  #region when draw a tab page , that variable is need
  private int selIndex = 0;
  private int mouseIndex = 9999;
  private Color borderColor = Color.Navy;
  private Pen borderPen = Pens.Navy;
  #endregion

  private Rectangle getIconRect(int pos, Rectangle rec)
  {
   int x, width;
   if (pos > selIndex && pos > mouseIndex)
    
   {
    x = rec.Left + 30 * pos + 150;
   }
   else if (pos > selIndex || pos > mouseIndex)
   {
    x = rec.Left + 30 * pos + 80;
   }
   else
   {
    x = rec.Left + 30 * pos + 10;
   }
   if (pos == selIndex || pos == mouseIndex)
   {
    width = 105;
   }
   else
    width = 35;

   Rectangle recIcon = new Rectangle(x, rec.Top, width, rec.Height - 5);

   return recIcon;
  }

  private Rectangle getBackRect(Rectangle rec)
  {
   // get background rect
   int x;
   if (999 != mouseIndex)
   {
    x = rec.Left + 30 * 8 + 140;
   }
   else
   {
    x = rec.Left + 30 * 8 + 80;
   }
   Rectangle rec2 = new Rectangle(x, rec.Top, rec.Width - x, rec.Height);

   return rec2;
  }

  private Point[] getPointsSine(Rectangle rec)
  {
   int w = rec.Width; int l = rec.Left;
   int h = rec.Height; int t = rec.Top;
   Point[] ps= new Point[]{
        new Point(l ,t),
    new Point(l, t + h - 5),
    new Point(l + 1, t + h - 4),
    new Point(l + 1, t + h - 3),
    new Point(l + 2, t + h - 2),
    new Point(l + 3, t + h - 1),
    new Point(l + 4, t + h - 1),
    new Point(l + 5, t + h),
    new Point(l + w - 15, t + h),
    new Point(l + w - 14, t + h - 1),
    new Point(l + w - 13, t + h - 1),
    new Point(l + w - 12, t + h - 2),
    new Point(l + w - 11, t + h - 3),
    new Point(l + w - 11, t + h - 4),
    new Point(l + w - 10, t + h - 5),
    new Point(l + w, t) };
   return ps;
  }

  private Point[] getPointsNon(Rectangle rec)
  {
   int w = rec.Width; int l = rec.Left;
   int h = rec.Height; int t = rec.Top;
   Point[] ps= new Point[]{
            new Point(l ,t),
            new Point(l, t + h - 5),
            new Point(l + 1, t + h - 4),
            new Point(l + 1, t + h - 3),
            new Point(l + 2, t + h - 2),
            new Point(l + 3, t + h - 1),
            new Point(l + 4, t + h - 1),
            new Point(l + 5, t + h),
            new Point(l + w - 5, t + h),
            new Point(l + w - 4, t + h - 1),
            new Point(l + w - 3, t + h - 1),
            new Point(l + w - 2, t + h - 2),
            new Point(l + w - 1, t + h - 3),
            new Point(l + w - 1, t + h - 4),
            new Point(l + w - 0, t + h - 5),
            new Point(l + w, t) };
   return ps;
  }

  /// <summary>
  ///  draw a icon , can draw selIndex && mouseIndex
  /// </summary>
  /// <param name="rec"></param>
  /// <param name="img"></param>
  /// <param name="isSel"></param>
  /// <param name="isMouse"></param>
  private void drawIcon(Rectangle rec, Graphics g,
   Image img, string text, bool isSel, bool isMouse)
  {
   //g.FillRectangle(Brushes.White, rec);
   // draw border
   Point[] ps;
   if (isSel)
    ps = getPointsSine(rec);
   else
    ps = getPointsNon(rec);
   if (isSel)
    // draw selected button
    g.FillPolygon(Brushes.White, ps);
   else if (isMouse)
   {
    // mouse move button
    Brush b = new LinearGradientBrush(rec, Color.LightBlue, Color.White,
     270, false);
    g.FillPolygon(b, ps);
   }
   else
   {
    // normal button
    Brush b = new LinearGradientBrush(rec, Color.LightGray, Color.White,
     270, false);
    g.FillPolygon(b, ps);
   }
   g.DrawPolygon(borderPen, ps);
   // draw white line
   if (isSel)
    g.DrawLine(Pens.White, rec.Location,
     new Point(rec.Right, rec.Top));

   // draw image
   if (!isSel && !isMouse)
   {
    // normal
    g.DrawImage(img,  rec.Left + 10, rec.Top + 6);
   }
   else if (!isSel)
   {
    // mouse move
    g.DrawImage(img,  rec.Left + 10, rec.Top + 6);
    // draw text
    g.DrawString(text, this.Font, Brushes.Navy, rec.Left + 30, rec.Top + 6);
   }
   else
   {
    g.DrawImage(img,  rec.Left + 6, rec.Top + 6);
    // draw text
    g.DrawString(text, this.Font, Brushes.Black, rec.Left + 30, rec.Top + 6);
   }
  }

  private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   if (!e.ClipRectangle.IsEmpty)
   {
    Rectangle rec = panel1.ClientRectangle;
    // draw line
    e.Graphics.DrawLine(borderPen, rec.Location, new Point(rec.Right, rec.Top));
    // draw buttons
    for (int i =7; i>= 0; i--)
    {
     if (i != selIndex)
     {
      Rectangle recIcon = getIconRect(i, rec);
      // get Image from list
      Image img = this.imageList1.Images[i];
      // text
      string text = "Book" + i.ToString();
      // draw icon
      drawIcon(recIcon, e.Graphics, img, text, i == selIndex, i == mouseIndex);

     }
    }
    //draw selected Button
    Rectangle rec2 = getIconRect(selIndex, rec);
    // get Image from list
    Image img2 = this.imageList1.Images[selIndex];
    // text
    string text2 = "Mark" +  selIndex.ToString();
    // draw icon
    drawIcon(rec2, e.Graphics, img2, text2, true, false);
   }
  }

  private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   // calculate size
   Rectangle cRec = panel1.ClientRectangle;
   for (int i=7; i>=0; i--)
   {
    Rectangle rec = this.getIconRect(i, cRec);
    if (rec.Contains(e.X, e.Y) && i != selIndex && i != mouseIndex)
    {
     // draw selIndex
     mouseIndex = i;
     timerShow.Interval = 100;
     timerShow.Enabled = true;
     
    }
   } // end for
   Rectangle recBack = this.getBackRect(cRec);   
   if ((e.Y > panel1.ClientRectangle.Height - 5 || recBack.Contains(e.X, e.Y))
    && mouseIndex != 9999)
   { // when mouse in the blank area, resume
    mouseIndex = 9999;
    timerShow.Interval = 100;
    timerShow.Enabled = true;
   }

  }

  private void timerShow_Tick(object sender, System.EventArgs e)
  {
   panel1.Invalidate();
   timerShow.Enabled = false;
  }

  private void panel1_MouseLeave(object sender, System.EventArgs e)
  { // when mouse leave
   if (mouseIndex != 9999)
   {
    mouseIndex = 9999;
    timerShow.Interval = 100;
    timerShow.Enabled = true;
   }
  }

  private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   // mouse move button can be selected
   if (mouseIndex != 9999 && e.Button == MouseButtons.Left)
   {
    // measure point in the rec
    Rectangle cRec = panel1.ClientRectangle;
    Rectangle rec = this.getIconRect(mouseIndex, cRec);
    if (rec.Contains(e.X, e.Y))
     onTabSelected(mouseIndex);
   }
  }

  private void onTabSelected(int pos)
  {
   selIndex = pos;
   mouseIndex = 9999;
   timerShow.Interval = 30;
   timerShow.Enabled = true;
   // on tabpage changed , revovle method 
   if (this.tabClick != null)
    this.tabClick(pos);
  }

  private void panel2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   // draw border
   if (!e.ClipRectangle.IsEmpty)
   {
    Rectangle rec = panel2.ClientRectangle;
    // draw back ground
    e.Graphics.FillRectangle(new SolidBrush(panel2.BackColor), rec);
    rec.Width --; rec.Height --;
    
    // draw border
    e.Graphics.DrawRectangle(borderPen, rec);

    // draw bottom line
    Point p1 = new Point(rec.Left + 1, rec.Bottom);
    Point p2 = new Point(rec.Right - 1, rec.Bottom);
    e.Graphics.DrawLine(new Pen(panel2.BackColor), p1, p2);
   }
  }

  private void panel2_SizeChanged(object sender, System.EventArgs e)
  {
   panel2.Invalidate();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值