How to close dialogs open on main form - *from* the main form

Posted on 2005-04-26
Last Modified: 2010-04-05
I have searched EE and have not found a suitable solution. I have also googled with no luck yet.

When my application receives a certain wm_Timer message,
I want to close all the dialogs over the main form of my application
Important: There may be 2 levels of dialogs (e.g. dialog1 which has an opendialog showing).
The solution needs to be abstract, I will _not_ have variable names (fdialog1) nor classnames (tfdialog1) to use

I already have the wm_timer part

    procedure WMTimer( var Msg : TMessage ); message wm_timer;
procedure TForm1.WMTimer(var Msg: TMessage);
        if msg.wParam = 101 then
// close modal dialog boxes here
Question by:TheRealLoki
    1 Comment
    LVL 26

    Accepted Solution

    This following code should do as requested, just call it in your routine:

        if msg.wParam = 101 then
               // close modal dialog boxes here


    function CloseModalWindowsProc(Handle: HWND; Param: HWND): BOOL; stdcall;
      // Don't touch the application handle
      if (Handle <> Application.Handle) then
         // Check the owner window handle
         if (GetWindow(Handle, GW_OWNER) = Application.Handle) then
            // Don't close the caller window
            if (Handle <> Param) then SendMessage(Handle, WM_CLOSE, 0, 0);
      // Keep going

    procedure CloseModalWindows(Caller: TForm);
      EnumWindows(@CloseModalWindowsProc, Caller.Handle);

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now