Recent

Author Topic: Pause code in Form OnPaint event  (Read 6138 times)

polypress

  • Jr. Member
  • **
  • Posts: 91
Pause code in Form OnPaint event
« on: June 27, 2015, 12:55:32 pm »
I need to pause the flow of code in an android form OnPaint event which is controlled from elsewhere within the application.

I have tried global variable (onpaint doesn't respond to its changes), mutex (windows only - not supported for android), TTimer (does not affect the flow), Refresh, Repaint, Invalidate.

Within the OnPaint event I have a while loop which doesn't release until some other parameter is changed, as described above.

Does anyone have any suggestions, or alternative solutions?

Regards

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Pause code in Form OnPaint event
« Reply #1 on: June 27, 2015, 04:09:02 pm »
I need to pause the flow of code in an android form OnPaint event which is controlled from elsewhere within the application.

I have tried global variable (onpaint doesn't respond to its changes), mutex (windows only - not supported for android), TTimer (does not affect the flow), Refresh, Repaint, Invalidate.

Within the OnPaint event I have a while loop which doesn't release until some other parameter is changed, as described above.

Does anyone have any suggestions, or alternative solutions?

Regards
I really don't understand what you want to do but

1) setting any variable to true/false in order to break from a loop must happen in a second thread so start a new thread and when the time is right set the variable to exit. ]
2) If the time to pause is known before hand then try to use sleep(x) for a pause effect.

If by pause you do not mean delay the execution buttruly pause aka stop execution then you have to see what android has to offer on that regard and I'll let that answer from someone with android experience.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

polypress

  • Jr. Member
  • **
  • Posts: 91
Re: Pause code in Form OnPaint event
« Reply #2 on: June 27, 2015, 04:43:38 pm »
Thank you taazz.

A series of lines is placed on screen at specific coordinates during the paint operation. This works OK. However, if the screen is touched during this process, the lines are applied in the wrong position and the result is incorrect. I want to delay the flow of code during the draw operation if the form mousedown or mousemove is initiated, and then continued from the same place in the code on the form mouseup event. Alternatively, disabling the form (but not buttons or edit boxes) to mouse movements could create the same effect.

I hope this explains the situation.

Regards

polypress

  • Jr. Member
  • **
  • Posts: 91
Re: Pause code in Form OnPaint event
« Reply #3 on: June 27, 2015, 05:14:57 pm »
I have tried adding
Form.OnClick:=nil,
Form.OnMouseDown:=nil,
etc to the Form.OnCreate event, but this does not appear to disable the mouse events.

balazsszekely

  • Guest
Re: Pause code in Form OnPaint event
« Reply #4 on: June 27, 2015, 05:24:23 pm »
You should try something like this(I don't know if works on android though).
Code: [Select]
//...
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    procedure MyPaint(Sender: TObject);
    { private declarations }
  public
    { public declarations }
  end;
//...       

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.OnPaint := @MyPaint;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  I: Integer;
begin
  Form1.OnPaint := nil;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Form1.OnPaint := @MyPaint;
  Form1.Repaint;
end;

procedure TForm1.MyPaint(Sender: TObject);
var
  I: Integer;
begin
  I := 0
  while I < 100 do
  begin
    Form1.Canvas.Rectangle(I, I, I + 200, I + 200);
    Inc(I, 2);
  end;
end;                           

If works all you have to do is save the position of I.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Pause code in Form OnPaint event
« Reply #5 on: June 27, 2015, 05:29:36 pm »
I have no experience with android, but as far as I know asynchronous execution means that paint must exit before any mouse event can be handled. if you start a loop inside your paint event that never exits then the mouse event should never be handled by your application and in fact it should seem like the application is frozen. My first guess would be remove the application.processmessages from your paint event.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

polypress

  • Jr. Member
  • **
  • Posts: 91
Re: Pause code in Form OnPaint event
« Reply #6 on: June 27, 2015, 06:47:07 pm »
Thank you for your replies.

GetMem. I have tried your code and what it does is to prevent any form painting when the form mouse is down. What I need is to allow form painting from non mouse events, but disable painting from only mouse related events.

taazz. That makes sense as global variable changes are not recognised within the form paint. Would this apply to the MyPaint procedure suggested by GetMem?

Regards

balazsszekely

  • Guest
Re: Pause code in Form OnPaint event
« Reply #7 on: June 27, 2015, 07:15:37 pm »
Quote
GetMem. I have tried your code and what it does is to prevent any form painting when the form mouse is down. What I need is to allow form painting from non mouse events, but disable painting from only mouse related events.
Then you should extend the code to include all mouse events. Alternatively drop a TPanel to your form,  then drop a transparent Image(Align -->alClient) to the panel(no mouse event for the panel). Do the drawing to  panel canvas.

polypress

  • Jr. Member
  • **
  • Posts: 91
Re: Pause code in Form OnPaint event
« Reply #8 on: June 28, 2015, 09:12:18 am »
MyPaint doesn't recognise global variable changes, just like FormPaint.

I have tried

if (Sender is TForm) then ignore FormPaint

but unfortunately, all calls to FormPaint are from TForm, even those embedded in the code (eg from button clicks) which are required to call FormPaint.

I will try the asynchronous route. Is it possible to catch the FormPaint call before it reaches the procedure so that I can allow it to proceed only if called from the embedded code? There are Hooks in Windows code but I don't know if these can be used in android. I will investigate.

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Pause code in Form OnPaint event
« Reply #9 on: June 28, 2015, 09:55:56 am »
@polypress: onpaint must be a very short and quick procedure. All that happens in this event must be very simple code. Do not forget that all the events in the app are  executed in one thread so they occur one after another. Notthing is async or concurent in lazarus. Remeber that all the events first are handeled bu the OS and then transmited to the app. And finaly the onpaint is not for painting the forms content - it's for showing it on the canvas. In this event is guaranteed to have a valid cliprect. So my suggestion is to
1. Draw offscreen in separate thread controled by your logic and just draw the clip rect part in the onpaint

Or

2: make the on paint smart  calculate and draw the cliprect part of the data


Ps: the efect that you suffer in due to the fact that when you touch tbe screen the os scrolls tbe window but your onpaint slil paint on old coordinates. Nothing to do wiht pausing code etc... when the os scrolls it fire and invalidate rect event wit h calls onpaint ... but  your last on paint is still running...
Speak postscript or die!
Translate to pdf and live!

polypress

  • Jr. Member
  • **
  • Posts: 91
Re: Pause code in Form OnPaint event
« Reply #10 on: June 28, 2015, 01:01:01 pm »
I have found many references to async in lazarus, but these may not apply to android(?)

The ideal solution would be to intercept the call to FormPaint before it reaches there.

Meanwhile, I am using intercept at start of FormPaint where I can't use while loop, and am just using (if) go/no-go to jump out of FormPaint.

Thanks for all your suggestions.

Regards

 

TinyPortal © 2005-2018