Маленькое, такое, исправление...
От: Timozey Россия  
Дата: 27.08.07 09:15
Оценка:
Ситуация такая: необходимо сделать контрол — плоский TabСontrol, все сделал (назв. iTab), вроде нигде не глючит, но когда тащищь компонент из панели (toolbox) на форму (в дизайнере), то он создается вместе с двумя вкладками по умолчанию как и стандартный TabControl, но эти две вкладки — стандартные TabPages. Все следующие вкладки которые создаются (кодом или в дизайнере) уже iTabPage, то есть те, которые мне нужны. Как сделать так, чтобы с самого начала iTab создавал iTabPages, а не стандартные TabPages? (какое-то дурацкое предложение получилось...).

Код уже в студии:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Reflection;

namespace MyNamespace.Items
{
    //[ToolboxBitmap(typeof(iTab), "tab.png")]
    public class iTab : TabControl
    {


        private iTabPageCollection tpc;

        private int hoverindex;
        private bool pressed;
        private int height;

        public iTab()
        {
            tpc = new iTabPageCollection(this);

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.Size = new Size(116, 116);
            this.height = 116;
            this.Dock = DockStyle.Top;

        }

        protected override void OnCreateControl()
        {
           
            this.SelectedIndex = 0;
            this.hoverindex = -1;
        }

        public iTabPageCollection TabPages
        {
            get
            {
                return tpc;
            }
        }

             
        public static Color SC1 = Color.FromArgb(106, 195, 53);
        public Color SelectedFoneColor
        {
            get
            {
                return SC1;
            }
            set
            {
                SC1 = value;
                this.Refresh();

            }
        }
        
        public Color SC2=Color.White;
        public Color SelectedForeColor
        {
            get
            {
                return SC2;
            }
            set
            {
                SC2 = value;
                this.Refresh();

            }
        }

        public static Color UC1=Color.White;
        public Color UnselectedFoneColor
        {
            get
            {
                return UC1;
            }
            set
            {
                UC1 = value;
                this.Refresh();

            }
        }

        public Color UC2=Color.Black;
        public Color UnselectedForeColor
        {
            get
            {
                return UC2;
            }
            set
            {
                UC2 = value;
                this.Refresh();

            }
        }

        public static void iSelectionTop(Graphics g, Rectangle rectangle, float radius, bool pressed)
        {
            RectDraw(g, new Pen(SC1), rectangle);
                rectangle.Offset(1, 1);
                rectangle.Width -= 10;
                rectangle.Height -= 10;
        }

        public static void RectDraw(Graphics g, Pen pen, Rectangle rectangle)
        {
            GraphicsPath gfx = new GraphicsPath();
            gfx.AddRectangle(rectangle);
            gfx.CloseFigure();
            g.DrawPath(pen, gfx);
            gfx.Dispose();

        }
        public static void RectFillTop(Graphics g, Brush brush, Rectangle rectangle)
        {
            GraphicsPath gp = new GraphicsPath();
            
            rectangle.Offset(0, -1);
            gp.AddRectangle(rectangle);
            
            gp.CloseFigure();
            g.FillPath(brush, gp);
            gp.Dispose();
        }
        public static void RectDrawTop(Graphics g, Pen pen, Rectangle rectangle)
        {
            
            GraphicsPath gp = new GraphicsPath();
            gp.AddRectangle(rectangle);
            
            gp.CloseFigure();
            g.DrawPath(pen, gp);
            gp.Dispose();
        }

        protected override void OnSelecting(TabControlCancelEventArgs e)
        {
            base.OnSelecting(e);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (!this.GetTabRect(0).Contains(e.X, e.Y) && this.Height == 26)
                this.Size = new Size(this.Width, this.height);            
        }
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);

            if (!this.GetTabRect(0).Contains(e.X, e.Y))
            {
                this.height = this.Height;
                this.Size = new Size(this.Width, 26);
            }
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            for (int i = 0; i < this.TabCount; i++)
            {
                if (this.GetTabRect(i).Contains(e.X, e.Y))
                {
                    this.hoverindex = i;

                    break;
                }
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            this.pressed = true;
            this.Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            this.pressed = false;
            this.Invalidate();
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            this.hoverindex = -1;

            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

            
                        e.Graphics.Clear(UnselectedFoneColor);
                        Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);

                        int ti = 0;
                        foreach (TabPage tab in this.TabPages)
                        {
                            Rectangle taberect = this.GetTabRect(ti);
                            taberect.Height += 4;
                            taberect.Offset(2, 0);

                            if (this.Height > 26 && tab == this.SelectedTab)
                            {
                                
                                RectFillTop(e.Graphics, new SolidBrush(SelectedFoneColor), taberect);
                                RectDrawTop(e.Graphics, new Pen(SelectedFoneColor), taberect);
                                taberect.Offset(2, 1);
                                taberect.Width -= 2;
                                taberect.Height--;
                                
                                Region region = new Region();
                                region.Exclude(new Rectangle(taberect.X - 4, 0, taberect.Width + 1, 2));
                                tab.Region = region;
                            }
                            else if (ti == this.hoverindex)
                            {
                                taberect.Width--;

                                if (ti == 0)
                                {                                    
                                    iSelectionTop(e.Graphics, taberect, 3f, this.pressed);
                                }
                                else
                                {                                    
                                    iSelectionTop(e.Graphics, taberect, 3f, this.pressed);
                                }

                                taberect.Width++;
                            }

                            taberect.Height -= 2;

                            StringFormat stringformat = new StringFormat();
                            stringformat.Alignment = StringAlignment.Center;
                            stringformat.LineAlignment = StringAlignment.Center;

                            if(tab==this.SelectedTab)
                                e.Graphics.DrawString(tab.Text, tab.Font, new SolidBrush(SelectedForeColor), taberect, stringformat);
                            else
                                e.Graphics.DrawString(tab.Text, tab.Font, new SolidBrush(UnselectedForeColor), taberect, stringformat);

                            ti++;
                        }
                    }
                    
        }

    public class iTabPage : TabPage
    {
        public iTabPage()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
        
        public static void RectDraw(Graphics g, Pen pen, Rectangle rectangle)
        {
            GraphicsPath gfx = new GraphicsPath();
            gfx.AddRectangle(rectangle);
            gfx.CloseFigure();
            g.DrawPath(pen, gfx);
            gfx.Dispose();

        }        

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            
                        e.Graphics.Clear(iTab.UC1);

                        Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1); 
                        RectDraw(e.Graphics, new Pen(iTab.SC1), rect);
                        
        }
    }
    
    public class iTabPageCollection : iTab.TabPageCollection
    {
        /*public iTabPageCollection(TabControl tc) : base(tc)
        {           
            
        }*/
        public iTabPageCollection(iTab tc)
            : base(tc)
        {

        }


        public new iTabPage this[int index]
        {
            get
            {
                return ((iTabPage)base[index]);
            }
            set
            {
                base[index] = value;
            }

        }
        public void Add(iTabPage Value)
        {
            base.Add((iTabPage)Value);
        }
        public void AddRange(iTabPage[] pages)
        {
            base.AddRange((iTabPage[])pages);
        }

    }

}
... << RSDN@Home 1.1.4 stable SR1 rev. 568>>
Re: Маленькое, такое, исправление...
От: Timozey Россия  
Дата: 27.08.07 19:13
Оценка:
Неужели никто не писал свой TabControl
... << RSDN@Home 1.1.4 stable SR1 rev. 568>>
Re[2]: Маленькое, такое, исправление...
От: Дьяченко Александр Россия  
Дата: 27.08.07 20:12
Оценка:
Здравствуйте, Timozey, Вы писали:

T>Неужели никто не писал свой TabControl


Да ты не первый кто спрашивает.

TabControl со своими TabPage
Автор:
Дата: 23.11.06

Re: Создание своего контрола с заполненной коллекцией.
Автор: kiple
Дата: 10.08.07


Поищи по слову ControlDesigner это не раз обсуждалось.
... << RSDN@Home 1.2.0 alpha rev. 726>>
Re[3]: Маленькое, такое, исправление...
От: Timozey Россия  
Дата: 28.08.07 07:57
Оценка:
Здравствуйте, Дьяченко Александр, Вы писали:

Ясно, спасибо, я просто не знал в какую сторону копать...
... << RSDN@Home 1.1.4 stable SR1 rev. 568>>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.