?
Solved

How to close all dialogs and messages

Posted on 2006-06-26
13
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
13 Comments
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

649 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