绘制带滚动条的窗体

public partial class FormScroll : Form
    {
        Pen pen = new Pen(Color.Black);
        Rectangle recE = new Rectangle(0, 0, 100, 200);
        Rectangle recR = new Rectangle(200, 200, 100, 200);

        public FormScroll()
        {
            InitializeComponent();
            this.AutoScaleMode = AutoScaleMode.Font;

            //正常显示,而且图形的右侧和下侧还会出现空白区域。因为图像显示区域为(400,400)
            //this.AutoScrollMinSize = new Size(500, 500);

            //设置为窗体的高度和宽度的时候,会出现滚动条,但是图形不能显示完全。
            //this.AutoScrollMinSize = new Size(this.Width, this.Height);

            //此时不会出滚动条,因为最小滚动区域小于窗体大小。
            this.AutoScrollMinSize = new Size(200, 200);
            this.AutoScroll = true;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var dc = e.Graphics;
            Size s = new Size(AutoScrollPosition);
            System.Diagnostics.Debug.WriteLine(s.Width + ":" + s.Height);
            if (e.ClipRectangle.Top + s.Height < 300)
            {
                var nrecE = new Rectangle(recE.Location + s, recE.Size);
                dc.DrawEllipse(pen, nrecE);
                var nrecR = new Rectangle(recR.Location + s, recR.Size);
                dc.DrawRectangle(pen, nrecR);
            }
            //e.Graphics.TranslateTransform(this.AutoScrollOffset.X, this.AutoScrollOffset.Y);
            //e.Graphics.DrawEllipse(pen, recE);
            //e.Graphics.DrawRectangle(pen, recR);
        }
    }

  

原文地址:https://www.cnblogs.com/363546828/p/3103323.html