123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.Windows.Forms;
- namespace Microsoft.Drawing
- {
-
-
-
- public class DoubleBufferedGraphics : Disposable
- {
- private const int PRE_ALLOC = 10;
- private bool m_IsCreating;
- private IWindow m_Owner;
- private Graphics m_OwnerGraphics;
- private BufferedGraphics m_BufferedGraphics;
- #region 构造函数
-
-
-
-
- public DoubleBufferedGraphics(IWindow owner)
- {
- this.m_Owner = owner;
- }
- #endregion
- #region 字段属性
- private CompositingMode m_CompositingMode = CompositingMode.SourceOver;
-
-
-
- public CompositingMode CompositingMode
- {
- get
- {
- return this.m_CompositingMode;
- }
- set
- {
- if (value != this.m_CompositingMode)
- {
- this.m_CompositingMode = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.CompositingMode = value;
- }
- }
- }
- private CompositingQuality m_CompositingQuality = CompositingQuality.Default;
-
-
-
- public CompositingQuality CompositingQuality
- {
- get
- {
- return this.m_CompositingQuality;
- }
- set
- {
- if (value != this.m_CompositingQuality)
- {
- this.m_CompositingQuality = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.CompositingQuality = value;
- }
- }
- }
- private InterpolationMode m_InterpolationMode = InterpolationMode.Bilinear;
-
-
-
- public InterpolationMode InterpolationMode
- {
- get
- {
- return this.m_InterpolationMode;
- }
- set
- {
- if (value != this.m_InterpolationMode)
- {
- this.m_InterpolationMode = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.InterpolationMode = value;
- }
- }
- }
- private PixelOffsetMode m_PixelOffsetMode = PixelOffsetMode.Default;
-
-
-
- public PixelOffsetMode PixelOffsetMode
- {
- get
- {
- return this.m_PixelOffsetMode;
- }
- set
- {
- if (value != this.m_PixelOffsetMode)
- {
- this.m_PixelOffsetMode = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.PixelOffsetMode = value;
- }
- }
- }
- private SmoothingMode m_SmoothingMode = SmoothingMode.None;
-
-
-
- public SmoothingMode SmoothingMode
- {
- get
- {
- return this.m_SmoothingMode;
- }
- set
- {
- if (value != this.m_SmoothingMode)
- {
- this.m_SmoothingMode = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.SmoothingMode = value;
- }
- }
- }
- private int m_TextContrast = 4;
-
-
-
- public int TextContrast
- {
- get
- {
- return this.m_TextContrast;
- }
- set
- {
- if (value != this.m_TextContrast)
- {
- this.m_TextContrast = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.TextContrast = value;
- }
- }
- }
- private TextRenderingHint m_TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
-
-
-
- public TextRenderingHint TextRenderingHint
- {
- get
- {
- return this.m_TextRenderingHint;
- }
- set
- {
- if (value != this.m_TextRenderingHint)
- {
- this.m_TextRenderingHint = value;
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Graphics.TextRenderingHint = value;
- }
- }
- }
-
-
-
- public Graphics Graphics
- {
- get
- {
- return this.m_BufferedGraphics.Graphics;
- }
- }
- private Size m_Size = Size.Empty;
-
-
-
- public Size Size
- {
- get
- {
- return this.m_Size;
- }
- }
- #endregion
- #region 私有方法
-
-
-
-
- private void InitGraphics(Graphics g)
- {
- g.CompositingMode = this.m_CompositingMode;
- g.CompositingQuality = this.m_CompositingQuality;
- g.InterpolationMode = this.m_InterpolationMode;
- g.PixelOffsetMode = this.m_PixelOffsetMode;
- g.SmoothingMode = this.m_SmoothingMode;
- g.TextContrast = this.m_TextContrast;
- g.TextRenderingHint = this.m_TextRenderingHint;
- }
- #endregion
- #region 公共方法
-
-
-
-
- public bool Prepare()
- {
-
- this.CheckDisposed();
-
- this.m_Owner.CheckDisposed();
-
- if (!this.m_Owner.IsHandleCreated)
- return false;
-
- if (!this.m_Owner.Visible)
- return false;
-
- Size wndSize = this.m_Owner.Size;
- if (wndSize.Width <= 0 || (wndSize.Height <= 0))
- return false;
-
- Size bufferSize = this.m_Size;
- if (this.m_BufferedGraphics != null
- && bufferSize.Width >= wndSize.Width && bufferSize.Width <= wndSize.Width + PRE_ALLOC
- && bufferSize.Height >= wndSize.Height && bufferSize.Height <= wndSize.Height + PRE_ALLOC)
- return true;
-
- if (bufferSize.Width < wndSize.Width)
- wndSize.Width += PRE_ALLOC;
- if (bufferSize.Height < wndSize.Height)
- wndSize.Height += PRE_ALLOC;
-
- if (this.m_IsCreating)
- return false;
- this.m_IsCreating = true;
-
- BufferedGraphicsContext bufferedGraphicsContext = BufferedGraphicsManager.Current;
- bufferedGraphicsContext.MaximumBuffer = wndSize;
-
- if (this.m_OwnerGraphics == null)
- this.m_OwnerGraphics = this.m_Owner.CreateGraphics();
- if (this.m_BufferedGraphics != null)
- this.m_BufferedGraphics.Dispose();
- this.m_BufferedGraphics = bufferedGraphicsContext.Allocate(this.m_OwnerGraphics, new Rectangle(0, 0, wndSize.Width, wndSize.Height));
-
- this.InitGraphics(this.m_BufferedGraphics.Graphics);
- this.m_Size = wndSize;
-
- this.m_IsCreating = false;
- return true;
- }
-
-
-
-
- public void BlendRender(Graphics g)
- {
- BufferedGraphicsEx.BlendRender(this.m_BufferedGraphics, g);
- }
-
-
-
-
- public void BlendRender(PaintEventArgs e)
- {
- BufferedGraphicsEx.BlendRender(this.m_BufferedGraphics, e.Graphics, e.ClipRectangle);
- }
-
-
-
-
- public void Render(Graphics g)
- {
- this.m_BufferedGraphics.Render(g);
- }
-
-
-
-
- public void Render(PaintEventArgs e)
- {
- BufferedGraphicsEx.Render(this.m_BufferedGraphics, e.Graphics, e.ClipRectangle);
- }
- #endregion
- #region 释放资源
-
-
-
-
- protected override void Dispose(bool disposing)
- {
- if (this.m_BufferedGraphics != null)
- {
- this.m_BufferedGraphics.Dispose();
- this.m_BufferedGraphics = null;
- }
- if (this.m_OwnerGraphics != null)
- {
- this.m_OwnerGraphics.Dispose();
- this.m_OwnerGraphics = null;
- }
- this.m_Owner = null;
- }
- #endregion
- }
- }
|