GDI+及滚动条的处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/donowtop/article/details/79957002
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.AutoScrollMinSize = new Size(250, 350);//文档大小,当窗口大小于文档大小时出现滚动条
        }
        int c1, c2;//矩形和椭圆的绘制次数
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (Graphics g = e.Graphics)
            {
                Rectangle c = e.ClipRectangle;//失效区域
                Rectangle r1 = new Rectangle(0, 0, 200, 200);
                r1.Offset(this.AutoScrollPosition);//考虑滚动条的位置,转换文档位置到客户区
                if (c.IntersectsWith(r1))//如果矩形与失效区域相交
                {
                    Debug.Print("绘制矩形{0}", ++c1);
                    using (Pen p = new Pen(Color.Blue, 3))
                    {
                        g.DrawRectangle(p, r1);
                    }
                }
                Rectangle r2 = new Rectangle(50, 200, 200, 150); r2.Offset(this.AutoScrollPosition);
                if (c.IntersectsWith(r2))
                {
                    Debug.Print("绘制椭圆{0}", ++c2);
                    using (Pen p = new Pen(Color.Yellow, 2))
                    {
                        g.DrawEllipse(p, r2);
                    }
                    using (SolidBrush b = new SolidBrush(Color.Red))
                    {
                        g.FillEllipse(b, r2);
                    }
                }
            }

        }
    }
}

阅读更多

没有更多推荐了,返回首页