zoukankan      html  css  js  c++  java
  • 双缓冲画线示例

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
       
        public partial class DrawLine : UserControl
        {
            public delegate void DrawChangeDataDelegate(ref string data);

            [Description("Callback event for the ChangeData window")]
            public event DrawChangeDataDelegate DrawChangeData;
            private BufferedGraphicsContext context;
            private BufferedGraphics grafx;
            public DrawLine()
            {
                InitializeComponent();
                SetStyle(ControlStyles.UserPaint,true );
                SetStyle(ControlStyles.ResizeRedraw,true );
                SetStyle(ControlStyles.OptimizedDoubleBuffer ,true );
                context = BufferedGraphicsManager.Current;
                context.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
                grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height));
                DrawToBuffer(grafx.Graphics );
            }
            private void DrawToBuffer(Graphics g)
            {
                string data=string.Empty ;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawLine(new Pen (Color.White ),new Point (3,3),new Point (this.Width-3,this.Height-3));
                if (DrawChangeData != null)
                    DrawChangeData(ref data);
                using (Font ft = new Font("Arial", 8f, FontStyle.Regular))
                    g.DrawString(data,ft,new SolidBrush (Color.White ),new PointF (3,3));
            }
            protected override void OnResize(EventArgs e)
            {
                context.MaximumBuffer = new Size(this.Width +1,this.Height +1);
                if(grafx !=null)
                {
                    grafx.Dispose ();
                    grafx=null;
                }
                grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height));
                DrawToBuffer(grafx.Graphics);
                base.OnResize(e);
            }      
            protected override void OnPaint(PaintEventArgs e)
            {
                System.Diagnostics.Debug.WriteLine(string.Format ("MousePosition:{0}", MousePosition ));
                DrawToBuffer(grafx.Graphics);
                grafx.Render(e.Graphics );
            }
            protected override void OnMouseEnter(EventArgs e)
            {
               
                context.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
                if (grafx != null)
                {
                    grafx.Dispose();
                    grafx = null;
                }
                grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height));
                DrawToBuffer(grafx.Graphics);
                base.OnMouseEnter(e);
            }
            protected override void OnMouseLeave(EventArgs e)
            {
             
                context.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
                if (grafx != null)
                {
                    grafx.Dispose();
                    grafx = null;
                }
                grafx = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, this.Width, this.Height));
                DrawToBuffer(grafx.Graphics);
                base.OnMouseLeave(e);
            }

        }
    }

  • 相关阅读:
    5W1H聊开源之What——开源协议有哪些?
    5W1H聊开源之What——开源是什么?
    测试开发专题-开篇
    痞子衡嵌入式:在串口波特率识别实例里逐步展示i.MXRT上提升代码执行性能的十八般武艺
    痞子衡嵌入式:以i.MXRT1xxx的GPIO模块为例谈谈中断处理函数(IRQHandler)的标准流程
    痞子衡嵌入式:超级下载算法RT-UFL v1.0发布,附J-Link下安装教程
    《痞子衡嵌入式半月刊》 第 34 期
    痞子衡嵌入式:Keil在线调试时设不同复位类型可能会导致i.MXRT下调试现象不一致(J-Link/DAPLink)
    痞子衡嵌入式:超级下载算法(RT-UFL)开发笔记番外(1)
    痞子衡嵌入式:嵌入式里串口(UART)自动波特率识别程序设计与实现(轮询)
  • 原文地址:https://www.cnblogs.com/teyond/p/OptimizedDoubleBuffer.html
Copyright © 2011-2022 走看看