tags:

views:

244

answers:

2

I have a form with 50 or more controls which I create and add at runtime. I don't want to see then appear one by one; I would rather disable drawing/start buffering at the start & then see them all appear at once.

I seem to recall doing something like this in BCB about 10 years ago, but forget how.

+14  A: 

I'm not sure if there's a Delphi-specific method to do this, but using the Win32 API, this is done through the WM_SETREDRAW message.

Edit: Thanks to Ken White and Sertac Akyuz for the sample code below.

begin
  // Defer updates
  SendMessage(Handle, WM_SETREDRAW, WPARAM(False), 0);
  try
    // Create all your controls here
  finally
    // Make sure updates are re-enabled
    SendMessage(Handle, WM_SETREDRAW, WPARAM(True), 0);
    // Invalidate;  // Might be required to reflect the changes  
  end;
end;
casablanca
+1, I hope your answer gets a lot of upvotes quickly enough to prevent answers with [`LockWindowUpdate`](http://msdn.microsoft.com/en-us/library/dd145034%28VS.85%29.aspx).
Sertac Akyuz
just out of curiosity, why?
Mawg
LockWindowUpdate was designed to stop drawing for drag and drop, it goes a bit further than WM_SETREDRAW because it could make assumptions about what the user was doing. One assumption was that since a drag and drop was occurring, window moving could be disabled/ignored. You can still drag a window around that has WM_SETREDRAW set to false.
gbrandt
@Mawg: See "With what operations is LockWindowUpdate not meant to be used?" (http://blogs.msdn.com/b/oldnewthing/archive/2007/02/22/1742084.aspx)
mghie
Ok, so if I don't use LockWindowUpdate, can anyone post a short code snippet using WM_SETREDRAW ?
Mawg
@Mawg: I could, but I'd feel very bad stealing reputation that rightfully belongs to casablanca, so I'll let him do that! @casablanca: Please include a code snippet!
Andreas Rejbrand
@Ken - Small modification on the sample. Revert/edit as you wish..
Sertac Akyuz
@Ken and Sertac: Thanks, both of you. I haven't used Delphi in about 10 years, so I'm glad someone was able to provide sample code.
casablanca
@Sertac: No revert or re-edit required. I haven't run into a need for Invalidate the few times I've used WM_SETREDRAW, but it might be needed sometimes. I see no problem with the suggestion. :-) Thanks.
Ken White
Thanks! (although it's handle and not hwnd in that second call)
Mawg
@Mawg: Good catch. I pulled the code snippet from a code sample DB I've collected, and it was C code. My quick rewrite into Delphi missed the second HWND. Thanks for pointing it out; I've fixed it for anyone who finds this in the future.
Ken White
+4  A: 

I simply keep Visible = False until everything's nice and ready.

Cosmin Prund
+1 That's also a possibility (I had thought of it, just wanted to see if there wasn't another way)
Mawg
@Mawg, for runtime component creation it seems like keeping Visible = False "feels" better, while using WM_SETREDRAW "feels" better for updating an already existing and already visible control.
Cosmin Prund
Mawg