I created an application with two forms. First one is the main form and second one is hidden. I placed a button on Form1 and I made it ShowModal the second form. On Win7 the form appears with an animation. Then I close the appeared form (Form2) and I click the button once again. Form2 appears without the animation. I want the animation every time. What should I do?
-
1+1 just because I have actually never thought about this before. – Andreas Rejbrand Dec 17 '10 at 12:51
-
By "What should I do", I assume you mean "I want the animation every time. What should I do to have it displayed every time". – Andreas Rejbrand Dec 17 '10 at 12:52
-
@Andreas: really? Well I know how to solve the problem. We need to destroy the form and create it again but the method that I'm using is wrong. I use application.CreateForm that I think is not a good way. – Javid Dec 17 '10 at 12:57
-
The only thing that `Application.CreateForm` does in addition to calling the constructor directly is setting `Application.MainForm` if it is `nil`. So it does no harm but it's also useless. – jpfollenius Dec 17 '10 at 13:24
-
@Smasher, it *can* do harm. It's not type safe, and if you're not careful, it can make your main form be something you didn't expect. Details: http://www.cs.wisc.edu/~rkennedy/createform – Rob Kennedy Dec 17 '10 at 16:27
-
@Rob Kennedy: Didn't know that, thanks for the link. Interesting read. – jpfollenius Dec 20 '10 at 07:48
3 Answers
The only thing I can think of right now is to create the form manually each time you want to display it modally. To do this, go to the project options and make sure that the form isn't automatically created. Then do
procedure TForm1.Button1Click(Sender: TObject);
begin
with TForm2.Create(self) do
try
ShowModal;
finally
Free;
end;
end;
In my opinion, most often modal forms should in fact be created manually.

- 105,602
- 8
- 282
- 384
-
2+1 for creating modal forms manually. That way application start up can be a lot faster too and often enough a form isn't needed at all. – jpfollenius Dec 17 '10 at 13:27
Well, you could just elect not to worry about it! Alternatively a very quick hack would be to free the form each time it closes since the animation appears to run only on the first time the form is shown.
EDIT: Another approach would be to call DestroyHandle on your form whenever it closes. I'm guessing now, but I imagine that Windows records somewhere in the window a flag indicating that the animation has been shown. Once this flag has been set the animation is never shown again.

- 601,492
- 42
- 1,072
- 1,490
-
1Although I agree this isn't much of a problem (the lack of the animation), it might also be considered an effect of another problem: In my opinion modal forms should be created manually, and then the animation is shown every time. For one thing, I often dislike when a modal form is displayed at its "old" position (the position it had the last time it was displayed) and with the last tab displayed, and with the focus still at the old control, and with the previous edit box selection still present, and so on. It feels wrong. – Andreas Rejbrand Dec 17 '10 at 13:01
-
@Andreas Ha! Funny you got your answer accepted when I said the same thing! – David Heffernan Dec 17 '10 at 13:17
-
Yeah. I dislike when people downvote without giving a reason. I give you +1 just to compensate. If I have to guess the downvoter didn't like the attitude ("Well, you could just elect not to worry about it!") or possibly the fact that you called it a "hack" to create the form manually. As I noted previously, it might even be better to create modal forms manually. Why the OP accepted my answer and not yours, I do not know. But given the OP's comment above, it might be the case that he didn't know *how* to create modal form manually (you shouldn't use `CreateForm`). – Andreas Rejbrand Dec 17 '10 at 13:21
-
3I didn't downvote either, but your first sentence is really not helpful. If the OP decided not to worry about it he wouldn't have posted a question. – jpfollenius Dec 17 '10 at 13:26
-
1@Smasher sometimes it is helpful to challenge the OP to look at a problem a different way. Often I solve problems by changing the question so that the previously hard question dissolves. – David Heffernan Dec 17 '10 at 13:29
-
@Andreas I take charge of positioning all forms myself because I don't like the VCL options. So the issue of position is not a problem for me when re-using forms. Sometimes you want to re-use if you want the state from a previous instantiation to persist but you don't happen to have a persistence mechanism available. – David Heffernan Dec 17 '10 at 13:30
-
1@David: "Well, you could just elect not to worry about it!"... I don't think that that qualifies as "challenging to look at a problem a different way". Just my opinion. – jpfollenius Dec 17 '10 at 13:32
-
1
As an alternative way it's possible to fool windows by sending a notification that form's style has been changed, that will make windows reset "secret flag" for current form's handle. Thus, on showing already created form the cool show effect animation will be applied again. However, I can't say what negative effects can be caused by doing this way.
uses
Winapi.Windows, Vcl.Controls;
type
TFormHelper = class helper for TForm
public
procedure Show;
end;
implementation
procedure TFormHelper.Show;
begin
SendMessage(Handle,CM_CUSTOMSTYLECHANGED,0,0);
inherited Show;
end;
Note: code featured with a class helper, this feature/keyword might be not available in older IDEs.

- 1
- 1
-
Note that the handler for the message of TCustomForm performs a CM_RECREATEWND on itself. In effect, as far as *windows* is concerned, you have a new window. – Sertac Akyuz Nov 27 '14 at 20:46