In Delphi; what are the differences between Application.MessageBox, Windows.MessageBox or Dialogs.MessageDlg? Or which is more efficient to use computer memory?
6 Answers
Windows.MessageBox is the WinAPI MessageBox, Application.MessageBox is a wrapper around it. Dialogs.MessageDlg however is a VCL form. So if you are concerned about memory or thread safety, the first two might be better suited. MessageDlg OTOH is more flexible and easier to use (IMHO, of course).
Nowadays, I'd consider task dialogs (TaskDialogIndirect, TTaskDialog or another wrapper).

- 13,748
- 1
- 45
- 83
Windows MessageBox is localized by OS (Yes, No, Cancel...), MessageDlg can be localized by hand.

- 2,528
- 18
- 26
If I remember correctly, there is one important distinction bewteen the Delphi VCL message boxes and the Windows ones - you can specifiy flags that stop the Application messages from being serviced (eg MB_SYSTEMMODAL). This can be useful for displaying errors where you need to 'freeze' your application - the Delphi MessageDlg will still fire timer events even whilst on screen. See:

- 13,334
- 11
- 80
- 154
Memory usage shouldn't be such a problem with message boxes. I personally prefer the VCL form (Dialogs.MessageBox) since I can localize it from the Consts.pas unit. I also like it from the fact that I can add custom controls to it, like checkboxes for "don't show this again" and other stuff like this.
Why do you care about the tiny amount of memory used by a message box? There are many other things you should be concerning yourself with when writing a Delphi app. In any case, as far as I'm aware these are all thin wrappers around the Windows MessageBox API.
-
Just I was wondering. I use a MessageBox function hundreds of times in one project. – SimaWB Mar 27 '09 at 11:57
-
the memory is reclaimed each time the message box closes – Mar 27 '09 at 12:03
-
@Neil: We will hope so. Seriously if it wasn't reclaimed it would have been detected years ago. – sharptooth Mar 27 '09 at 12:05
-
*All* wrappers - not really, see my answer. :-) – Uli Gerhardt Mar 27 '09 at 13:10
They all do the same - invoke WinAPI function MessageBox(). The difference in resource consumption if any is minimal. If you care so much you can call MessageBox() directly - just include "uses Windows".

- 167,383
- 100
- 513
- 979