flickering

Hello,

In my application I have a form with a lot of TImages on it. Simultaneously changing their appearance in the rutime causes the window to unpleasantly flicker for a second or even more. How can I block it until all the procedures are over?

Vadim Smolensky,
Saint-Petersburg, Russia
VSmolenskyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mokuleCommented:
Hi,
Do You set DoubleBuffered property of frame to true?
0
VSmolenskyAuthor Commented:
A frame? It's not a frame, it's a form. Or don't I understand anything?
0
mokuleCommented:
Sorry. Form, I mean form of course.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Newt6398Commented:
Try this you may need to adapt it a bit to your needs.


{ This little demo shows how to make a selection rectangle to be
controlled with your
  mouse, without having to endure the pains of flicker.

  What we do in this scenario is consider two rectangles: one rectangle
  being larger than the other. We then paste the smaller rectangle over
  top of the larger rectangle matching them at x1, y1. Then, using XOR
  addition of bits, we subtract the difference in regions and construct a
  new region of difference between the two. This region is then inverted
  and pasted onto the screen.  

  Written by Greg Holowatinc for SK Computer Solutions WEB Page TIPS
  Section contact:  nwillams@telusplanet.net (MetZ on #Delphi on EFNET}

  Note: If testing this in Delphi, make sure that in object inspector, you
  set the                 OnMouseDown and OnMouseUp events to
  FormMouseDown. Also, set the OnMouseMove to
        FormMouseMove.  
}    

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    RBeg, REnd: TRect;
    Capturing: Boolean;
  public
    procedure DrawDelta(Loc: HDC; RNew, ROld: TRect);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


{ Capture the OnMouseDown event; we set up the first rectangle upon
  the mouse being clicked and set the capturing flag on. }

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if not Capturing then
  begin
    Capturing := True;
    RBeg.Top := Y;
    RBeg.Left := X;
    RBeg.Bottom := Y;
    RBeg.Right := X;
    REnd.Top  := Y;
    REnd.Left := X;
  end else
    Capturing := False;
end;


{ Capture the OnMouseMove event; we update the X2, Y2 of the big
rectangle, then call DrawDelta and make the small rectangle the
same as the big rectangle }

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if Capturing then
  begin
    REnd.Bottom := Y;
    REnd.Right := X;
    DrawDelta(Form1.Canvas.Handle, RBeg, REnd);
    RBeg.Bottom := Y;
    RBeg.Right := X;
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 Capturing := False;
end;

{ This is the main procedure for filling in the delta region }

procedure TForm1.DrawDelta(Loc: HDC; RNew, ROld: TRect);
var
  { Set some variables of Handle To Region HGRN }
  RegBig, RegSmall, RegNew: HRGN;
begin
  { Create all the regions out of the rectangles passed as
val. parameters }
 RegSmall := CreateRectRgn(ROld.Left, ROld.Top, ROld.Right, ROld.Bottom);
 RegBig := CreateRectRgn(RNew.Left, RNew.Top, RNew.Right, RNew.Bottom);
 RegNew := CreateRectRgn(0, 0, 0, 0);
{ Combine the two rectangles using GRN_XOR flag to check;
if both bits are present then used then the region does not
exist, hence the area that is already marked as inverted,
does not updated again. This is the way we  prevent flicker. }
  CombineRgn(RegNew, RegSmall, RegBig, RGN_XOR);
  { Invert then newly created region. }
  InvertRgn(Loc, RegNew);
  { Remove all the regions from memory }
  DeleteObject(RegBig);
  DeleteObject(RegSmall);
  DeleteObject(RegNew);
end;

end.
0
Imthiyaz_phCommented:
Try using the LockWindowUpdate API to disable painting for a window.

LockWindowUpdate(
    HWND hWndLock       // handle of window to lock  
   );

Pass the handle of the form as parameter to LockWindowUpdate before changing the appearance. After the change is complete, use LockWindowUpdate(0) to unlock the window.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VSmolenskyAuthor Commented:
LockWindowUpdate works perfectly.
DoubleBuffered:=True make the flickering even worse.
I accept the comment from Imthiyaz_ph. Thank you!
0
mehdi_mCommented:
ok
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.