滚动条在应删除后仍被绘制 [英] Scrollbar still is painted after it should be removed

查看:102
本文介绍了滚动条在应删除后仍被绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下自定义控件,并且可以放置在表单上(将AutoScroll设置为true,并且该控件锚定在左侧,顶部和右侧).

I have the following custom control and can place on a form (with AutoScroll set to true and the control anchored left, top and right).

如果窗体对于控件来说太短,则窗体会正确调整控件的大小(以便为滚动腾出空间)并显示滚动条.

If the form is too short for the control, the form correctly resizes the control (to make room for the scroll) and displays the scroll bar.

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

When the control is closed using the close glyph, the control is resized and the scroll bar is removed, but occasionally the scroll bar appears to remain painted. If the form is minimized or moved off-screen, the leftover paint is removed.

我尝试了Parent.Invalidate并以多种方式对其进行了戏弄,但无济于事.有什么建议吗?

I've tried Parent.Invalidate and have toyed with it in many ways but to no avail. Any suggestions?

(使用VS 2008 Standard)

(Using VS 2008 Standard)

替代文字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
    }
}

推荐答案

您必须在Control.Invalidate()之后调用Control.Update()才能获得所需的效果.或通过简单地调用.Refresh()来使您的生活更加轻松.

You must call Control.Update() after Control.Invalidate() to get the desired effect. Or make your life slightly easier by simply calling .Refresh().

这篇关于滚动条在应删除后仍被绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆