?
Solved

How to close all dialogs and messages

Posted on 2006-06-26
13
Medium Priority
?
337 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

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!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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