Recent

Author Topic: Carbon Application: strange things with Canvas and Forms  (Read 8485 times)

Ant_222

  • New Member
  • *
  • Posts: 24
Carbon Application: strange things with Canvas and Forms
« on: January 14, 2009, 12:07:03 am »
Hello all!

I have written a simple Carbon program just to test Lazarus, and here are two problems that I have met:

1. Although the KeyPress event handler works, nothing is drawn on the Cavas.

2. As seen from the code, the FormKeyPress method should only move the form to a position 4 pixels to the right of the left side of the screen, but in fact only the first key press does it. All subsequent presses move the form 6 pixels to the right, as if the third line of the method was not executed.

I'll appreciate your help,
Anton

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
  private
    { private declarations }
  public
    procedure PutCell(x,y:integer);
    { public declarations }
  end;

var
  Form1: TForm1;

implementation
  const cellSize = 12;
  const fieldWidth = 20;
  const fieldHeight = 20;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  self.ClientHeight := fieldHeight*cellSize;
  self.ClientWidth  := fieldWidth*cellSize;
end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
  PutCell(5,10);
  inc(Self.Left,6);
  self.Left:=4; //This line is executed only on the first invocation of the event
end;


procedure TForm1.PutCell(x,y:integer);
begin
  self.Canvas.Brush.Color := clBlack;
  self.Canvas.TextOut(30,60,'Hello!');
  self.Canvas.FillRect(x*cellSize, y*cellSize, (x+1)*cellSize, (y+1)*cellSize); //Not working
end;

initialization
  {$I unit1.lrs}
end.


Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Carbon Application: strange things with Canvas and Forms
« Reply #1 on: January 14, 2009, 08:38:48 am »
The Carbon widget set cannot draw outside the OnPaint event. Move the painting code to the OnPaint event of the form and trigger an repaint with Invalidate in the FormKeyPress.

Ant_222

  • New Member
  • *
  • Posts: 24
Re: Carbon Application: strange things with Canvas and Forms
« Reply #2 on: January 14, 2009, 12:53:36 pm »
Thanks, it works now.

Everything that I want to draw on the Canvas I have to draw in the OnPaint event. But that doesn't seem very comfortable...

Is it a platform- or Lazarus- specific thing? Can you point me to the corresponding documentation?

Anton


Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Carbon Application: strange things with Canvas and Forms
« Reply #3 on: January 14, 2009, 01:01:49 pm »
It is a platform specific thing. Carbon cannot handle it. All I found it is this in the wiki. I think some more explanation would be useful, I just don't know enough about it.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946
Re: Carbon Application: strange things with Canvas and Forms
« Reply #4 on: January 14, 2009, 01:52:48 pm »
Even with Delphi, drawing outside the OnPaint event is only transient, so not what you really want in most cases.
If the window needs redrawing, (for ex. if it was overlapped by another window) the drawing is gone if you paint outside the OnPaint event.

If you don't want to draw in the OnPaint event, then create a buffer TBitmap and draw on its canvas. Then in the OnPaint event, paint the buffer to the screen. This also reduces flickering.
Take care that the TBitmap has the same dimensions as your painting area.
« Last Edit: January 14, 2009, 02:24:20 pm by theo »

Ant_222

  • New Member
  • *
  • Posts: 24
Re: Carbon Application: strange things with Canvas and Forms
« Reply #5 on: January 15, 2009, 12:33:51 am »
Many thanks, theo. I'll follow your advice :)

 

TinyPortal © 2005-2018