FMX: Control.Invalidate / Force repaint of a FMX control

  • I don't know why this has not be encoded into the FireMonkey core, but the often used VCL "Invalidate" can be simply implemented into a control class as

    InvalidateRect( RectF(0,0,Width,Height) )


    If a standard FMX control should be drawn the call would be


    SomeControl.InvalidateRect( RectF(0,0,SomeControl.Width,SomeControl.Height) )

  • It happens often that despite calling Invalidate the control is not painted.

    This is usually the case when the Invalidate routine is called in the context of user input.


    To solve the issue you need to set a flag in "Invalidate" and as soon as the application is idle call Invalidate.


    This code can be used to create a procedure which is called when the application is Idle:



    This is the message handler which is called when in the Idle cycle:


    Code
    1. procedure TCustomTool.IdleMessageHandler(const Sender: TObject; const Msg: System.Messaging.TMessage);
    2. begin
    3.   if FNeedInvalidate then
    4.   begin
    5.     FNeedInvalidate := false;
    6.     InvalidateRect(RectF(0,0,Width,Height));
    7.   end;
    8. end;

    I hope this is helpful,

    Julian