Advertisement
Guest User

Untitled

a guest
Dec 20th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace StaticDynamicDrawing
  6. {
  7.     public partial class BufferedGraphicsControl : UserControl
  8.     {
  9.         private readonly BufferedGraphicsContext _bufferedGraphicsContext;
  10.         private BufferedGraphics _staticGraphics;
  11.         private BufferedGraphics _dynamicGraphics;
  12.         private BufferedGraphics _outputGraphics;
  13.         private readonly List<RectangleF> _dirtyRects = new List<RectangleF>();
  14.         private readonly BufferedGraphicsControlModel _model;
  15.         private bool _redrawAll;
  16.         private bool _dirty;
  17.  
  18.         public BufferedGraphicsControl()
  19.         {
  20.             InitializeComponent();
  21.  
  22.             _model = new BufferedGraphicsControlModel(this);
  23.             _model.AttachEvents();
  24.  
  25.             _bufferedGraphicsContext = new BufferedGraphicsContext();
  26.             ResizeGraphicalBuffers();
  27.             SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
  28.             SetStyle(ControlStyles.DoubleBuffer, false);
  29.             SetStyle(ControlStyles.ResizeRedraw, true);
  30.         }
  31.  
  32.         internal bool Dirty
  33.         {
  34.             get { return _dirty; }
  35.             set
  36.             {
  37.                 if (!value)
  38.                     return;
  39.  
  40.                 _dirty = true;
  41.                 Invalidate();
  42.             }
  43.         }
  44.  
  45.         private void ResizeGraphicalBuffers()
  46.         {
  47.             if (_bufferedGraphicsContext == null || DisplayRectangle.IsEmpty)
  48.                 return;
  49.  
  50.             _staticGraphics.Do(_ => _.Dispose());
  51.             _dynamicGraphics.Do(_ => _.Dispose());
  52.             _outputGraphics.Do(_ => _.Dispose());
  53.  
  54.             using (Graphics graphics = CreateGraphics())
  55.             {
  56.                 _staticGraphics = _bufferedGraphicsContext.Allocate(graphics, DisplayRectangle);
  57.                 _dynamicGraphics = _bufferedGraphicsContext.Allocate(graphics, DisplayRectangle);
  58.                 _outputGraphics = _bufferedGraphicsContext.Allocate(graphics, DisplayRectangle);
  59.             }
  60.  
  61.             _dirty = true;
  62.         }
  63.  
  64.         protected override void OnPaintBackground(PaintEventArgs e)
  65.         { /* Do Nothing */  }
  66.  
  67.         protected override void OnSizeChanged(System.EventArgs e)
  68.         {
  69.             ResizeGraphicalBuffers();
  70.             base.OnSizeChanged(e);
  71.         }
  72.  
  73.  
  74.         protected override void OnPaint(PaintEventArgs e)
  75.         {
  76.             if (_outputGraphics == null)
  77.             {
  78.                 Draw(e.Graphics);
  79.                 return;
  80.             }
  81.  
  82.             if (_dirty)
  83.             {
  84.                 _dirty = false;
  85.                 Draw(_outputGraphics.Graphics);
  86.             }
  87.  
  88.             _outputGraphics.Render(e.Graphics);
  89.         }
  90.  
  91.         protected virtual void Draw(Graphics graphics)
  92.         {
  93.             _model.PaintStaticStuff();
  94.             _model.PaintDynamicContent();
  95.  
  96.             //_outputGraphics.Render(graphics);
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Clean up any resources being used.
  101.         /// </summary>
  102.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  103.         protected override void Dispose(bool disposing)
  104.         {
  105.             if (disposing)
  106.             {
  107.                 _model.Do(_ => _.DetachEvents());
  108.  
  109.                 _staticGraphics.Do(_ => _.Dispose());
  110.                 _dynamicGraphics.Do(_ => _.Dispose());
  111.                 _outputGraphics.Do(_ => _.Dispose());
  112.                 _bufferedGraphicsContext.Do(_ => _.Dispose());
  113.             }
  114.  
  115.             if (disposing && (components != null))
  116.             {
  117.                 components.Dispose();
  118.             }
  119.             base.Dispose(disposing);
  120.         }
  121.  
  122.         internal BufferedGraphics DynamicGraphics
  123.         {
  124.             get { return _dynamicGraphics; }
  125.         }
  126.  
  127.         internal BufferedGraphics StaticGraphics
  128.         {
  129.             get { return _staticGraphics; }
  130.         }
  131.  
  132.         internal BufferedGraphics OutputGraphics
  133.         {
  134.             get { return _outputGraphics; }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement