Search code examples
delphidelphi-xe7

Drawing on the delphi form without OnPaint event


I have an problem, this is my code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Canvas.MoveTo(0, 0);
  Form1.Canvas.LineTo(100, 100);
end;    

This code work fine, there is a line on the form. But when i click minimize button and then show form normal, the line disappear. I want draw without OnPaint and OnResize event. Please help me


Solution

  • What you are attempting to do is not possible. Windows don't have persistent canvases. When they are hidden, minimised, moved under other windows, etc. the previous contents are lost. You must repaint them. That is the very essence of how Windows is designed.

    Either paint the form in response to paint messages or events, or use a control like TImage to hold a persistent image.