Prevent a control from Redrawing (Refreshing)

A number of controls have a build-in way to do that, the BeginUpdate and
EndUpdate methods. You often find these not on the control itself but on
some object property that manages the controls data, e.g. for a TMemo you use
Lines.BeginUpdate and Lines.EndUpdate. The EndUpdate automatically forces a
redraw of the control.

Question:

Is there a way to stop a component from refreshing until I’ve completed some
processing?

Answer:

Yes. Most controls can be blocked from redrawing by sending a WM_SETREDRAW
message with wParam = 0 to them. Once you have finished the changes you send
another WM_SETREDRAW with wparam = 1. For a grid this would look like this:

procedure LockControl(c: TWinControl; bLock: Boolean);
begin
  if (c = nil) or (c.Handle = 0) then Exit;
  if bLock then
    SendMessage(c.Handle, WM_SETREDRAW, 0, 0)
  else
  begin
    SendMessage(c.Handle, WM_SETREDRAW, 1, 0);
    RedrawWindow(c.Handle, nil, 0,
      RDW_ERASE or RDW_FRAME or RDW_INVALIDATE or RDW_ALLCHILDREN);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  LockControl(DBGrid1, True);
  try
    // do convoluted things to the grid
  finally
    LockControl(DBGrid1, False);
  end;
end;