GDI+及滚动条的处理

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);
                    }
                }
            }

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值