Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace StaticDynamicDrawing
- {
- public partial class BufferedGraphicsControl : UserControl
- {
- private readonly BufferedGraphicsContext _bufferedGraphicsContext;
- private BufferedGraphics _staticGraphics;
- private BufferedGraphics _dynamicGraphics;
- private BufferedGraphics _outputGraphics;
- private readonly List<RectangleF> _dirtyRects = new List<RectangleF>();
- private readonly BufferedGraphicsControlModel _model;
- private bool _redrawAll;
- private bool _dirty;
- public BufferedGraphicsControl()
- {
- InitializeComponent();
- _model = new BufferedGraphicsControlModel(this);
- _model.AttachEvents();
- _bufferedGraphicsContext = new BufferedGraphicsContext();
- ResizeGraphicalBuffers();
- SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
- SetStyle(ControlStyles.DoubleBuffer, false);
- SetStyle(ControlStyles.ResizeRedraw, true);
- }
- internal bool Dirty
- {
- get { return _dirty; }
- set
- {
- if (!value)
- return;
- _dirty = true;
- Invalidate();
- }
- }
- private void ResizeGraphicalBuffers()
- {
- if (_bufferedGraphicsContext == null || DisplayRectangle.IsEmpty)
- return;
- _staticGraphics.Do(_ => _.Dispose());
- _dynamicGraphics.Do(_ => _.Dispose());
- _outputGraphics.Do(_ => _.Dispose());
- using (Graphics graphics = CreateGraphics())
- {
- _staticGraphics = _bufferedGraphicsContext.Allocate(graphics, DisplayRectangle);
- _dynamicGraphics = _bufferedGraphicsContext.Allocate(graphics, DisplayRectangle);
- _outputGraphics = _bufferedGraphicsContext.Allocate(graphics, DisplayRectangle);
- }
- _dirty = true;
- }
- protected override void OnPaintBackground(PaintEventArgs e)
- { /* Do Nothing */ }
- protected override void OnSizeChanged(System.EventArgs e)
- {
- ResizeGraphicalBuffers();
- base.OnSizeChanged(e);
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- if (_outputGraphics == null)
- {
- Draw(e.Graphics);
- return;
- }
- if (_dirty)
- {
- _dirty = false;
- Draw(_outputGraphics.Graphics);
- }
- _outputGraphics.Render(e.Graphics);
- }
- protected virtual void Draw(Graphics graphics)
- {
- _model.PaintStaticStuff();
- _model.PaintDynamicContent();
- //_outputGraphics.Render(graphics);
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- _model.Do(_ => _.DetachEvents());
- _staticGraphics.Do(_ => _.Dispose());
- _dynamicGraphics.Do(_ => _.Dispose());
- _outputGraphics.Do(_ => _.Dispose());
- _bufferedGraphicsContext.Do(_ => _.Dispose());
- }
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- internal BufferedGraphics DynamicGraphics
- {
- get { return _dynamicGraphics; }
- }
- internal BufferedGraphics StaticGraphics
- {
- get { return _staticGraphics; }
- }
- internal BufferedGraphics OutputGraphics
- {
- get { return _outputGraphics; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement