This error occurs due to the fact that Image1 is not a global object, but a field of class TForm1 . The handler is a method of this class and sees this field, but it is not available for a third-party function.
Can do so
function f():byte; begin Form1.Image1.Canvas.Brush.Color:=clBlack; Form1.Image1.Canvas.Rectangle(1, 1, 50, 50); end;
But this is VERY wrong. Because There is an unnecessary reference to the global variable.
You can pass a pointer to an object of class TForm1
function f(AForm: TForm1):byte; begin AForm.Image1.Canvas.Brush.Color:=clBlack; AForm.Image1.Canvas.Rectangle(1, 1, 50, 50); end; procedure TForm1.Button2Click(Sender: TObject); begin f(Self); end;
but this is stupid. With the same success, you can make f method of class TForm . You, as I understand it, want to write a function for drawing a rectangle on an arbitrary canvas. Then all you need is to pass to the function a pointer to the desired canvas.
procedure f(ACanvas: TCanvas) begin ACanvas.Brush.Color:=clBlack; ACanvas.Rectangle(1, 1, 50, 50); end; procedure TForm1.Button2Click(Sender: TObject); begin f(Image1.Canvas); end;
Starting with Delphi 2006, you can write a helper for the TCanvas class and make f pseudo TCanvas this class.
type TCanvasHelper = class helper for TCanvas public procedure f; end; procedure TCanvasHelper.f begin Brush.Color:=clBlack; Rectangle(1, 1, 50, 50); end; procedure TForm1.Button2Click(Sender: TObject); begin Image1.Canvas.f; end;