9

I am using the code below to create a messagebox in Delphi 7. However I also have another form on screen who's FormStyle is set to fsStayOnTop and the messagebox appears behind this form.

Is there any way to force the messagebox to appear in front?

    if Application.MessageBox('Amessage here','Title', +MB_APPLMODAL + MB_ICONQUESTION + MB_YESNO) = IDNO then
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
colin
  • 2,983
  • 6
  • 41
  • 49
  • Related: http://stackoverflow.com/questions/2997079/delphi-how-to-prevent-forms-msgboxes-to-move-under-prior-form – Jens Mühlenhoff Nov 04 '11 at 11:03
  • Avoid using `Application.MessageBox`. Instead use WinAPI `MessageBox` function and pass it a `HWND` of your topmost form. Additionally, you can use `MB_TASKMODAL` flag to make sure the dialog will popup on top of every other form. – LightBulb Nov 05 '11 at 12:12

3 Answers3

8

Call NormalizeTopMosts prior to showing the message box.

Use NormalizeTopMosts to allow a message box or dialog box that is displayed using the Windows API functions (such as MessageBox and MessageDlg) directly, appear on top of a topmost form. Otherwise the topmost form remains on top, and may obscure the message box.

(Hope it's available in Delphi 7.)

Edit: Not sure about the downvote. If it hints in the direction that OP should use the native MessageBox function and set its parent HWND to the topmost window - I would agree. But maybe this is not possible for some reason.

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • 9
    Hmm. There are downvotes on all 3 answers so far, with no explanation/justification. Not sure what's up with that. SO doesn't have a "hit-and-run" badge. – Chris Thornton Nov 04 '11 at 13:48
  • Without a hint these downvotes are so pointless. +1 for the badge :) – Heinrich Ulbricht Nov 04 '11 at 20:16
  • I'm not seeing down votes, however as indicated, it may have been because the question specifically asks about using application.messagebox - there is a lot of code in that method before the windows.messagebox is called - mostly dealing with multiple monitors and task windows. Replacing the current call as specified in the question may cause undesirable results in the environment. – SilverKnight Nov 04 '11 at 22:25
2

In Windows.pas you can find more flags to MessageBox():

MB_APPLMODAL = $00000000;
MB_SYSTEMMODAL = $00001000;
MB_TASKMODAL = $00002000;

Read about them in MessageBox documentation

You can even use MB_TOPMOST flag.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
0

Try MB_TASKMODAL flag instead of MB_APPLMODAL.

Andrej Kirejeŭ
  • 5,381
  • 2
  • 26
  • 31