Solved

How to close all dialogs and messages

Posted on 2006-06-26
13
323 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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