How to draw on the form with scroll bars using "absolute" coordinates? lazarus / 31-LCL ---------------------------------------------------------------------- zamtmn · 2014-10-14 18:15 Hi All! I need to draw something on a form in a certain place, excluding the position of scrollbars. I can `t do it. Look at the test case. Label "abs" moves with the scrolling, and returns to the coordinates 100,100 only when resizing. I need to stop it moving Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs; type   { TForm1 }   TForm1 = class(TForm)     procedure _onCreate(Sender: TObject);     procedure _onPaint(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1._onCreate(Sender: TObject); begin      VertScrollBar.Range:=1000;      VertScrollBar.Tracking:=true;      HorzScrollBar.Range:=1000;      HorzScrollBar.Tracking:=true; end; procedure TForm1._onPaint(Sender: TObject); var   arect:trect; begin      //UpdateScrollbars;{uncomment this line - draw good, but other errors (app not close)}      ARect := GetClientRect;      canvas.TextRect(ARect,100+HorzScrollBar.Position,100+VertScrollBa r.Position,'Abs'); end; end.How to do it? win7, trunk lazarus, trunk fpc ---------------------------------------------------------------------- howardpc · 2014-10-14 20:39 You should not do anything in an OnPaint handler (or Paint procedure) except painting, or you will set up an endless loop of resizing/repainting, as you discovered when your app could not be closed. Try this as an alternative. Drop a label named absLabel on the main form of a new project, and complete the code as follows: Code: [Select]unit testScrolling; {$mode objfpc}{$H+} interface uses   Classes, Forms, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)     absLabel: TLabel;     procedure FormCreate(Sender: TObject);   protected     procedure ScrollbarHandler(ScrollKind: TScrollBarKind; OldPosition: Integer);       override;   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin   absLabel.Caption:='abs';   absLabel.Top:=100;   absLabel.Left:=100;   VertScrollBar.Range:=1000;   VertScrollBar.Tracking:=True;   VertScrollBar.Smooth:=True;   HorzScrollBar.Range:=1000;   HorzScrollBar.Tracking:=True;   HorzScrollBar.Smooth:=True; end; procedure TForm1.ScrollbarHandler(ScrollKind: TScrollBarKind;   OldPosition: Integer); begin   inherited ScrollbarHandler(ScrollKind, OldPosition);   case ScrollKind of     sbHorizontal: absLabel.Left:=100+OldPosition;     sbVertical:   absLabel.Top:=100+OldPosition;   end; end; end. ---------------------------------------------------------------------- zamtmn · 2014-10-14 21:17 >>You should not do anything in an OnPaint handler (or Paint procedure) except painting, or you will set up an endless loop of resizing/repainting, as you discovered when your app could not be closed. I understand that, just trying to understand why I did not get >>Try this as an alternative. Thanks! Unfortunately it does not suit me. I need to draw, and do not use controls ---------------------------------------------------------------------- howardpc · 2014-10-14 21:45 You can adapt what I showed earlier to paint on the form's canvas: Code: [Select]unit testScrolling; {$mode objfpc}{$H+} interface uses   Classes, Forms, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)     procedure FormCreate(Sender: TObject);     procedure FormPaint(Sender: TObject);   protected     procedure ScrollbarHandler(ScrollKind: TScrollBarKind; OldPosition: Integer);       override;   private     const lbl = 'abs';   private     scrollPosX: integer;     scrollPosY: integer;   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin   VertScrollBar.Range:=1000;   VertScrollBar.Tracking:=True;   VertScrollBar.Smooth:=True;   HorzScrollBar.Range:=1000;   HorzScrollBar.Tracking:=True;   HorzScrollBar.Smooth:=True; end; procedure TForm1.FormPaint(Sender: TObject); var   r: TRect; begin   r:=Rect(0, 0, HorzScrollBar.Range+100, VertScrollBar.Range+100);   Canvas.TextRect(r, 100+scrollPosX, 100+scrollPosY, lbl); end; procedure TForm1.ScrollbarHandler(ScrollKind: TScrollBarKind;   OldPosition: Integer); begin   inherited ScrollbarHandler(ScrollKind, OldPosition);   case ScrollKind of     sbHorizontal: scrollPosX:=OldPosition;     sbVertical:   scrollPosY:=OldPosition;   end; end; end. ---------------------------------------------------------------------- zamtmn · 2014-10-15 04:38 >>You can adapt what I showed earlier to paint on the form's canvas: This not works on windows, in fact it is almost the same as the code in my first post. Thanks for the tip on ScrollbarHandler, I will use such variant: Code: [Select]... procedure TForm1.FormPaint(Sender: TObject); var   r: TRect; begin   r:=Rect(0, 0, HorzScrollBar.Range+100, VertScrollBar.Range+100);   canvas.TextRect(r,100+HorzScrollBar.Position,100+VertScrollBar.Posit ion,'Abs'); end; procedure TForm1.ScrollbarHandler(ScrollKind: TScrollBarKind;   OldPosition: Integer); begin   invalidate;   inherited; end; ...Maybe he's not quite true, but it works ---------------------------------------------------------------------- zamtmn · 2014-10-15 16:34 >>Maybe he's not quite true, but it works Not work on GTK2(( text leaps +/- 1 pixel. Is such a simple problem has no solution?