Solved

How to close all dialogs and messages

Posted on 2006-06-26
13
316 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
Comment Utility
0
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
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
Comment Utility
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
 

Author Comment

by:tadoox
Comment Utility
<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
Comment Utility
A simple way is to use timer.
0
 

Author Comment

by:tadoox
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

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

Author Comment

by:tadoox
Comment Utility
^^^ 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you  for trying so hard <atul_parmar> this is exactly what i needed
0
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
Infect, that's my passion! :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

15 Experts available now in Live!

Get 1:1 Help Now