0

Possible Duplicate:
close a message box from another program using c#

There is another program on my computer that displays a message box every once in a while. Is it possible to use .NET to close these dialogs when they pop up?

Community
  • 1
  • 1
Tarang
  • 75,157
  • 39
  • 215
  • 276

1 Answers1

1

Use FindWindow and SendMessage winapi functions like here http://www.codeproject.com/Articles/22257/Find-and-Close-the-Window-using-Win-API

You must create a continous loop wich closes that window, such as while (true). I used a Timer because it's more eficient. Here's my code:

[DllImport ("user32.dll")] public static extern IntPtr FindWindow  (String sClassName, String sAppName);
[DllImport ("user32.dll")] public static extern int    SendMessage (IntPtr hWnd, uint Msg, int wParam, int lParam);
    Timer t;
    public Form1 ()
    {
        InitializeComponent ();
        t=new Timer ();
        t.Interval=100;
        t.Tick+=delegate
        {
            IntPtr w=FindWindow (null, "Message box title");
            if (w!=null) SendMessage (w, 0x0112, 0xF060, 0);
        };
        t.Start ();
    }

Where WM_SYSCOMMAND=0x0112 public const int SC_CLOSE = 0xF060;

If you don't know the window's class name (like above) use null and message box's title as paramers. Of course, that means that the message box has a title.

  • If title of dialog the same with title of App (Ex: Microsoft Excel) it will close app. – D T Mar 21 '19 at 05:05