Solved

How to close all dialogs and messages

Posted on 2006-06-26
13
328 Views
Last Modified: 2010-04-05
How do I close all dialogs and messages generated by my application?










_________________________________________________________________________________________________

http://www.web-hosting-wizard.com/
0
Comment
Question by:tadoox
  • 9
  • 4
13 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16990365
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16990515
As you can see you can not enumerate the message windows opened by your application. To close message window you can.

var
  h : HWND;
begin
  h := FindWindow('TMessageForm', 'Information'); // it can be confirmation,
  if h <> 0 then
    SendMessage(h, WM_CLOSE, 0, 0);
end;

0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16990637
To close the other windows, first you will need to write a call back e.g.

function EnumChildProcCallback(Handle : HWND; Lines : TStrings) : Boolean; stdcall;
var
  ClassName, Caption : array [0..1024] of char;
begin
  Result := True;
  GetClassName(Handle, ClassName, 1024);
  GetWindowText(Handle, Caption, 1024);
  // now check for the class and caption, if it is created by your app then close it. e.g.
  if ClassName = 'TForm1' then
    SendMessage(Handle, WM_CLOSE, 0, 0);
end;

Then call the EnumChildWindows win32 API function as

EnumChildWindows(Self.Handle , @EnumChildProcCallback, 0);
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:tadoox
ID: 16991803
<atul_parmar >, thank you for answers.

But how will i know then to close?
Basically I need to close Messages and Dialogs Before they appear because some could be modal and will freeze application.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16991984
A simple way is to use timer.
0
 

Author Comment

by:tadoox
ID: 16999028
Timer do not work in this case because it is important to close dialogs before apearance.
if you will not close modal dialog it will freeze application. in this case i will have to use another thread for timer but this is not best solution for me. I am searching a way to create method to detect messages and dialogs before apearance.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16999048
it seems that you want to supress the creation of message boxes and modal dialogs right?
0
 

Author Comment

by:tadoox
ID: 16999844
^^^ Yes that would be a best solution for me app. Have any ideas how to do that? thx.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16999862
You can create a hook procedure and then trap the WM_INITDIALOG message.
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 500 total points
ID: 17000068
Hi,

Here is what you want.

var
  MyHook : THandle;

/////// Hook callback procedure

function CallWndProcCallback(nCode : Integer; wParam, lParam : longint): LRESULT; stdcall
var
  Msg : CWPSTRUCT;
  ClassName : array[0..255] of char;
begin
  if nCode = HC_ACTION then
  begin
    CopyMemory(@Msg, Pointer(lParam), SizeOf(CWPSTRUCT));
    case Msg.message of
    WM_INITDIALOG :
      SendMessage(Msg.hwnd, WM_CLOSE, 0, 0);
    WM_CREATE:
      begin
        GetClassName(Msg.hwnd, ClassName, 255);
        if ClassName = 'TMessageForm' then
          SendMessage(Msg.hwnd, WM_CLOSE, 0, 0);
      end;
    end;
  end;
  Result := CallNextHookEx(WH_CALLWNDPROC, nCode, wParam, lParam);
end;

/////// To install the Hook

var
  tpid: DWORD;
begin
  tpid := GetWindowThreadProcessId(Handle, nil);
  if MyHook = 0 then
    MyHook := SetWindowsHookEx(WH_CALLWNDPROC, CallWndProcCallback, 0, tpid);
end;

/// To unintall the hook
  if MyHook <> 0 then
    UnhookWindowsHookEx(MyHook);
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17000208
Here is the updated hook proc.

function CallWndProcCallback(nCode : Integer; wParam, lParam : longint): LRESULT; stdcall
var
  Msg : CWPSTRUCT;
  ClassName : array [0..255] of char;
begin
  if nCode = HC_ACTION then
  begin
    CopyMemory(@Msg, Pointer(lParam), SizeOf(CWPSTRUCT));
    if Msg.message = WM_CREATE then
    begin
      GetClassName(Msg.hwnd, ClassName, 255);
      if Form1.Caption  = '' then
        Form1.caption := Classname;
      if (ClassName = 'TMessageForm') // for message boxes
      or (ClassName = '#32770') then // for dialogs e.g. file open dialog
      begin
        PostMessage(Msg.hwnd, WM_CLOSE, 0, 0); // sendmessage doesn't works all the time
      end;
    end;
  end;
  Result := CallNextHookEx(WH_CALLWNDPROC, nCode, wParam, lParam);
end;
0
 

Author Comment

by:tadoox
ID: 17000302
Thank you  for trying so hard <atul_parmar> this is exactly what i needed
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17000311
Infect, that's my passion! :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

829 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