⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customrenderer.cs

📁 章立民-visual stadio 2005 c#编程技巧
💻 CS
📖 第 1 页 / 共 2 页
字号:
                r = new Rectangle(e.GripBounds.Left,
                    e.GripBounds.Top + 2,
                    e.GripBounds.Width - 2,
                    e.GripBounds.Height - 6);
            }

            using (LinearGradientBrush lgb = new LinearGradientBrush(r,
                scaleColorPct(this.m_color1, 80),
                scaleColorPct(this.m_color2, 80),
                lgm))
            {
                e.Graphics.FillRectangle(lgb, r);
            }

            using (Pen p = new Pen(this.m_itemBorderColor))
            {
                e.Graphics.DrawRectangle(p, r);
            }
        }

        // 呈现整个工具列系列控件的背景。
        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            LinearGradientMode lgm;
            Rectangle r = new Rectangle(Point.Empty, e.ToolStrip.Size);

            if (e.ToolStrip.Orientation == Orientation.Vertical)
            {
                lgm = LinearGradientMode.Vertical;
            }
            else
            {
                lgm = LinearGradientMode.Horizontal;
            }

            using (LinearGradientBrush lgb = new LinearGradientBrush(r,
                this.m_currentc1, this.m_currentc2, lgm))
            {
                e.Graphics.FillRectangle(lgb, r);
            }
        }

        // 描绘一般项目的背景。
        protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
        {
            OnRenderButtonBackground(e);
        }

        // 描绘一个箭号。
        protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
        {
            drawLittleArrow(e.Graphics, e.ArrowRectangle, e.Direction);
        }

        // 描绘一个 ToolStripLabel 的背景。
        protected override void OnRenderLabelBackground(ToolStripItemRenderEventArgs e)
        {
            // 没有撰写任何程序代码,表示采用整个工具列系列控件的预设描绘。
        }

        // 描绘个别菜单项目的背景。
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            OnRenderItemBackground(e);
        }

        // 描绘一个 ToolStripPanel 的容器工作区。
        protected override void OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs e)
        {
            LinearGradientMode lgm;
            Rectangle r = new Rectangle(Point.Empty, e.ToolStripPanel.Size);

            if (e.ToolStripPanel.Width == 0 || e.ToolStripPanel.Height == 0)
            {
                return;
            }

            if (e.ToolStripPanel.Dock == DockStyle.Left || e.ToolStripPanel.Dock == DockStyle.Right)
            {
                lgm = LinearGradientMode.Vertical;
            }
            else
            {
                lgm = LinearGradientMode.Horizontal;
            }

            using (LinearGradientBrush lgb = new LinearGradientBrush(r, this.m_currentc1, this.m_currentc2, lgm))
            {
                e.Graphics.FillRectangle(lgb, r);
            }
        }

        // 如果一个工具列系列控件太大,将会建立一个溢位按钮。这使得我们有机会去描绘该项目。
        protected override void OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs e)
        {
            LinearGradientMode lgm;
            Color c1, c2;
            Rectangle r;
            ArrowDirection d;

            if (e.ToolStrip.Orientation == Orientation.Vertical)
            {
                lgm = LinearGradientMode.Vertical;
                r = new Rectangle(0, 0, e.Item.Bounds.Width - 2, e.Item.Bounds.Height - 2);
            }
            else
            {
                lgm = LinearGradientMode.Horizontal;
                r = new Rectangle(0, 0, e.Item.Bounds.Width - 2, e.Item.Bounds.Height - 2);
            }

            if (e.Item.Pressed)
            {
                c1 = scaleColorPct(this.m_color1, 70);
                c2 = scaleColorPct(this.m_color2, 70);
            }
            else if (e.Item.Selected)
            {
                c1 = scaleColorPct(this.m_color1, 120);
                c2 = scaleColorPct(this.m_color2, 120);
            }
            else
            {
                c1 = scaleColorPct(this.m_color1, 90);
                c2 = scaleColorPct(this.m_color2, 90);
            }

            using (LinearGradientBrush lgb = new LinearGradientBrush(r, c1, c2, lgm))
            {
                e.Graphics.FillRectangle(lgb, r);
            }

            // 绘制一个边框。
            using (Pen p = new Pen(this.m_itemBorderColor))
            {
                e.Graphics.DrawRectangle(p, r);
            }

            // 最后,绘制一个箭号。
            r = new Rectangle(Point.Empty, e.Item.Size);

            if (e.ToolStrip.Orientation == Orientation.Horizontal)
            {
                d = ArrowDirection.Down;
            }
            else
            {
                d = ArrowDirection.Right;
            }

            drawLittleArrow(e.Graphics, r, d);
        }

        // 描绘 ToolStripSplitButton 的背景。
        protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
        {
            ToolStripSplitButton tssb;
            Rectangle r;

            // 描绘一个按钮背景,我们希望它看起来与其它项目一样。
            OnRenderButtonBackground(e);

            // 取得所要绘制的分隔器边框。
            tssb = (ToolStripSplitButton)(e.Item);
            r = tssb.SplitterBounds;

            using (Pen p = new Pen(this.m_itemBorderColor))
            {
                e.Graphics.DrawLine(p, r.Left + r.Width / 2, r.Top + 2, r.Left + r.Width / 2, r.Bottom);
            }

            // 最后,绘制一个箭号。
            drawLittleArrow(e.Graphics, tssb.DropDownButtonBounds, ArrowDirection.Down);
        }

        // 时间到了,该变更色彩、重新整理....。
        private void m_timer_Tick(object sender, System.EventArgs e)
        {
            byte c1r, c1g, c1b;
            byte c2r, c2g, c2b;

            // 如果已经到达两个端点,就变更方向。
            if (this.m_positiveDirection == true && this.m_currentTick == NUM_INCREthisNTS)
            {
                this.m_positiveDirection = false;

                this.m_rinc = (short)(-(this.m_rinc));
                this.m_ginc = (short)(-(this.m_ginc));
                this.m_binc = (short)(-(this.m_binc));
            }
            else if (this.m_positiveDirection == false && this.m_currentTick == 0)
            {
                this.m_positiveDirection = true;

                this.m_rinc = (short)(-(this.m_rinc));
                this.m_ginc = (short)(-(this.m_ginc));
                this.m_binc = (short)(-(this.m_binc));
            }

            // 在适当的方向上递增计数器。
            if (this.m_positiveDirection)
            {
                this.m_currentTick += 1;
            }
            else
            {
                this.m_currentTick -= 1;
            }

            // 使用递增来更新色彩。
            c1r = valueInLimitsByte(this.m_currentc1.R - this.m_rinc, 0, 255);
            c1g = valueInLimitsByte(this.m_currentc1.G - this.m_ginc, 0, 255);
            c1b = valueInLimitsByte(this.m_currentc1.B - this.m_binc, 0, 255);
            c2r = valueInLimitsByte(this.m_currentc2.R + this.m_rinc, 0, 255);
            c2g = valueInLimitsByte(this.m_currentc2.G + this.m_ginc, 0, 255);
            c2b = valueInLimitsByte(this.m_currentc2.B + this.m_binc, 0, 255);

            this.m_currentc1 = Color.FromArgb(255, c1r, c1g, c1b);
            this.m_currentc2 = Color.FromArgb(255, c2r, c2g, c2b);


            // 告知我们所有的工作区项目去重新描绘。
            foreach (ToolStrip ts in this.m_toolstrips)
            {
                ts.Invalidate(true);
            }

            foreach (ToolStripPanel rc in this.m_ToolStripPanels)
            {
                rc.Invalidate(true);
            }

            foreach (ToolStripItem tsi in this.m_toolstripitems)
            {
                tsi.Invalidate();
            }
        }

        private void computeIncrethisnts()
        {
            this.m_rinc = (short)(((short)(this.m_color1.R) - (short)(this.m_color2.R)) / NUM_INCREthisNTS);
            this.m_ginc = (short)(((short)(this.m_color1.G) - (short)(this.m_color2.G)) / NUM_INCREthisNTS);
            this.m_binc = (short)(((short)(this.m_color1.B) - (short)(this.m_color2.B)) / NUM_INCREthisNTS);
        }

        private byte valueInLimitsByte(int val, byte lower, byte upper)
        {
            if (val < lower)
            {
                val = lower;
            }
            else if (val > upper)
            {
                val = upper;
            }

            return (byte)(val);
        }

        private Color scaleColorPct(Color c, int pct)
        {
            byte r, g, b;
            double p;

            p = (double)(pct) / (double)100;

            r = valueInLimitsByte((int)((double)(c.R) * p), 0, 255);
            g = valueInLimitsByte((int)((double)(c.G) * p), 0, 255);
            b = valueInLimitsByte((int)((double)(c.B) * p), 0, 255);

            return Color.FromArgb(c.A, r, g, b);
        }

        private void postInitialize()
        {
            if (this.m_timer == null)
            {
                this.m_timer = new Timer();
                this.m_currentTick = 1;
                this.m_positiveDirection = true;
                this.m_timer.Interval = TIthisR_SPEED_MS;
                this.m_timer.Start();
            }
        }

        private void OnColorChanged()
        {
            computeIncrethisnts();
            this.m_currentc1 = this.m_color1;
            this.m_currentc2 = this.m_color2;

            this.m_itemBorderColor = scaleColorPct(this.m_color1, 20);
            this.m_arrowColor = scaleColorPct(this.m_color1, 10);
        }

        private void drawLittleArrow(Graphics g, Rectangle bounds, ArrowDirection dir)
        {
            int left, aleft, top, atop;

            aleft = (int)(((bounds.Width + 5) / 2)) - 5;
            atop = (int)(((bounds.Height + 5) / 2)) - 5;
            left = bounds.Left;
            top = bounds.Top;

            switch (dir)
            {
                case ArrowDirection.Down:
                    using (Pen p = new Pen(this.m_arrowColor))
                    {
                        for (int x = 0; x <= 2; x++)
                        {
                            g.DrawLine(p, left + aleft + x, top + atop + x,
                                       left + aleft + (5 - x), top + atop + x);
                        }
                    }

                    break;
                case ArrowDirection.Right:
                    using (Pen p = new Pen(this.m_arrowColor))
                    {
                        for (int x = 0; x <= 2; x++)
                        {
                            g.DrawLine(p, left + aleft + x, top + atop + x,
                                       left + aleft + x, top + atop + (5 - x));
                        }
                    }

                    break;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -