devxlogo

Getting rid of the initial flash of a WS_MAXIMIZE child

Getting rid of the initial flash of a WS_MAXIMIZE child

Question:
How can I open an MDI child form so that it’s initially in a maximized state? Every time I try, it appears in its normal size, then maximizes visibly. I can’t hide it because Delphi won’t let me, and if I trick it by hiding it in CreateParams, it’s maximized for a split second (just after OnShow()), then is reduced to normal size, then is re-maximized. This is all happening somewhere after OnShow() and I can’t seem to stop it … I just need it to open already maximized, and all ready to go. … Help!

Answer:
One thing you might have noticed is that child forms set with thewsMaximized property have a visible flash when they’re first created.First they’re created in a normal state, then they maximize. This is moreannoying than problematic.

For those of you who are experienced inmucking about with form properties, you might think that setting the form’swindow style to WS_MAXIMIZE in the CreateParams method would do the trick.Alas, that doesn’t work either. But don’t worry, there’s a verysimple solution.

One of the ways you can prevent the user from seeing background operationson a window is to prevent it from painting, then having it refresh after thechanges have been made. To the user, it will appear as if the screen wasautomagically changed in the blink of an eye. With respect to opening up amaximized MDI child form in an MDI application, this is exactly the type ofthing we’re going to do.

The specific function that allows us to prevent screen painting is a WinAPIfunction called LockWindowUpdate. LockWindowUpdate takes a singleparameter ? the handle of the window ? and prevents it from painting untilLockWindowUpdate is called again with a parameter of ‘0.’ So, with respectto our particular problem, to prevent a maximized MDI child from flashing atcreate, you enclose its create statement between two LockWindowUpdate callslike so:

LockWindowUpdate(MyMDIMainForm.Handle);MyMDIChild := TMyMDIChild.Create(Application);LockWindowUpdate(0);
Pretty simple, huh? Notice that I locked the screen painting with respectto the MDI form, not the MDI child. That’s important, because if you tried tolock the update for the child, you’d get an error because the handle isinvalid. In any case, use this technique for all your MDI applications toavoid the initial flash.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist