Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi/GDI+: When is a Device Context created/destroyed?
    text
    copied!<p>Normally using GDI+ in Delphi you can use a <strong>TPaintBox</strong>, and paint during the <strong>OnPaint</strong> event:</p> <pre><code>procedure TForm1.PaintBox1Paint(Sender: TObject); var g: TGPGraphics; begin g := TGPGraphics.Create(PaintBox1.Canvas.Handle); try g.DrawImage(FSomeImage, 0, 0); finally g.Free; end; end; </code></pre> <p>The problem with this paradigm is that creating a destroying a <strong>Graphics</strong> object each time is wasteful and poorly performing. Additionally, there are a few <a href="http://msdn.microsoft.com/en-us/library/ms534425.aspx" rel="nofollow noreferrer">constructs availabe in GDI+</a> you can <em>only</em> use when you have a persistent <strong>Graphics</strong> object.</p> <p>The problem, of course, is <em>when</em> can i create that <strong>Graphics</strong> object? i need to know when the handle becomes available, and then when it is no longer valid. i need this information so i can create and destroy my <strong>Graphics</strong> object.</p> <hr> <h2>Solution Attempt Nº1</h2> <p>i can solve the creation problem by creating it when it is really needed - on the first time the <em>paint cycle</em> is called:</p> <pre><code>procedure TForm1.PaintBox1Paint(Sender: TObject); begin if FGraphics = nil then FGraphics := TGPGraphics.Create(PaintBox1.Canvas.Handle); FGraphics.DrawImage(FSomeImage, 0, 0); end; </code></pre> <p>But i have to know when the device context is no longer valid, so i can destroy my <strong>FGraphcis</strong> object, so that it is re-created the next time it's needed. If for some reason the <strong>TPaintBox</strong>'s device context gets recreated, i would be drawing on an invalid device context the next time <strong>OnPaint</strong> is called.</p> <p>What is the intended mechanism in Delphi for me to know when the <strong>device context handle</strong> of a <strong>TPaintBox</strong> is created, destroyed, or re-created?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload