滚动条仍应在被删除后进行绘制

时间:2009-03-10 22:17:51

标签: c# winforms controls scrollbar

我有以下自定义控件,可以放在表单上(AutoScroll设置为true,控件左,上,右)。

如果表单对于控件来说太短,表单会正确调整控件的大小(为滚动腾出空间)并显示滚动条。

当使用关闭字形关闭控件时,控件将调整大小并移除滚动条,但有时滚动条似乎仍保持绘制状态。如果表格最小化或移出屏幕,则删除剩余的油漆。

我尝试过Parent.Invalidate,并在很多方面玩弄它,但无济于事。有什么建议吗?

(使用VS 2008标准版)

alt text http://waltware.homedns.org/screenshot.jpg

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;



namespace GroupPanelTest
{
    public class GroupPanel : GroupBox
    {
        #region Members
        private const Int32 iHeaderHeight = 20;
        private Int32 iFullHeight = 200;

        private Boolean bClosed = false;
        private Rectangle rectCloseGlyphBounds = Rectangle.Empty;
        private Boolean bIsMoveOverCloseGlyph = false;
        #endregion

        #region Properties
        [DefaultValue(false)]
        public Boolean Closed
        {
            get
            {
                return (this.bClosed);
            }
            set
            {
                if (this.bClosed != value)
                {
                    this.bClosed = value;
                    if (this.bClosed)
                    {
                        this.iFullHeight = base.Height;
                        base.Height = GroupPanel.iHeaderHeight;
                    }
                    else
                    {
                        base.Height = this.iFullHeight;
                    }

                    foreach (Control con in base.Controls)
                        con.Visible = !this.bClosed;

                    this.Invalidate();
                }
            }
        }

        public new Int32 Height
        {
            get
            {
                return (base.Height);
            }
            set
            {
                if (value != base.Height)
                {
                    if (this.Closed)
                    {
                        this.iFullHeight = value;
                    }
                    else
                    {
                        Int32 iOldHeight = base.Height;
                        base.Height = value;
                    }
                }
            }
        }

        [DefaultValue(typeof(Size), "350,200")]
        public new Size Size
        {
            get
            {
                return (base.Size);
            }
            set
            {
                if (base.Size != value)
                {
                    base.Size = value;
                    if (!this.Closed)
                        this.iFullHeight = value.Height;
                }
            }
        }

        [DefaultValue(typeof(Padding), "0,7,0,0")]
        public new Padding Padding
        {
            get
            {
                return (base.Padding);
            }
            set
            {
                base.Padding = value;
            }
        }
        #endregion

        #region Construction
        public GroupPanel ()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.Selectable, true);

            this.Size = new Size(350, 200);
            this.Padding = new Padding(0, 7, 0, 0); // the groupbox will add to that
            this.rectCloseGlyphBounds = new Rectangle(base.ClientSize.Width - 24, 2, 16, 16);
        }
        #endregion

        #region Overrides
        protected override void OnSizeChanged (EventArgs e)
        {
            this.rectCloseGlyphBounds = new Rectangle(base.ClientSize.Width - 24, 2, 16, 16);
            base.OnSizeChanged(e);
        }

        protected override void OnPaint (PaintEventArgs e)
        {
            base.OnPaint(e); // we want all the delegates to receive the events, but we do this first so we can paint over it

            Graphics g = e.Graphics;

            g.FillRectangle(SystemBrushes.Window, this.ClientRectangle);
            Rectangle rectTitle = new Rectangle(0, 0, this.ClientRectangle.Width, GroupPanel.iHeaderHeight);
            g.FillRectangle(SystemBrushes.Control, rectTitle);
            g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, new PointF(5.0f, 3.0f));

            if (this.bIsMoveOverCloseGlyph)
            {
                g.FillRectangle(SystemBrushes.ButtonHighlight, this.rectCloseGlyphBounds);
                Rectangle rectBorder = this.rectCloseGlyphBounds;
                rectBorder.Inflate(-1, -1);
                g.DrawRectangle(SystemPens.Highlight, rectBorder);
            }

            using (Pen pen = new Pen(SystemColors.ControlText, 1.6f))
            {
                if (this.Closed)
                {
                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 3, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 8);
                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 3, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 8);

                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 7, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 12);
                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 7, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 12);
                }
                else
                {
                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 8, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 3);
                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 8, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 3);

                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 3, this.rectCloseGlyphBounds.Top + 12, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 7);
                    g.DrawLine(pen, this.rectCloseGlyphBounds.Left + 13, this.rectCloseGlyphBounds.Top + 12, this.rectCloseGlyphBounds.Left + 8, this.rectCloseGlyphBounds.Top + 7);
                }
            }
        }

        protected override void OnMouseDown (MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && this.rectCloseGlyphBounds.Contains(e.Location))
                this.Closed = !this.Closed; // close will call invalidate
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove (MouseEventArgs e)
        {
            this.bIsMoveOverCloseGlyph = this.rectCloseGlyphBounds.Contains(e.Location);
            this.Invalidate(this.rectCloseGlyphBounds);
            base.OnMouseMove(e);
        }
        #endregion
    }
}

1 个答案:

答案 0 :(得分:0)

您必须在Control.Invalidate()之后调用Control.Update()以获得所需的效果。或者只需调用.Refresh()。

,让您的生活更轻松