none
Capture Popup-Window from other program RRS feed

  • Question

  • Hey,

    I'm running an *.exe on my Windows Server in the background. Sometimes the program prompts some kind of MessageBox, which prevents my program from running.

    Now it came in my mind, to write a Little program, which checks, if there that messageBox is shown up and Closes that window.

    Does anybody have an idea, how to handle such a situation in c#.net?

    Wednesday, April 25, 2018 6:47 AM

All replies

  • Hi ed912211,

    Thank you for posting here.

    For your question, please try the code below.

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsForms { public partial class capture_popup_window : Form { public capture_popup_window() { InitializeComponent(); } [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); public const int WM_CLOSE = 0x10; private void StartKiller() { timer1.Interval = 1000;

    timer1.Tick += new EventHandler(timer1_Tick); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { KillMessageBox(); //((Timer)sender).Stop(); } private void KillMessageBox() { //It used to search the pop up MessageBox, please note the Message is the MessageBox caption. IntPtr ptr = FindWindow(null, "Message"); if (ptr != IntPtr.Zero) { //find the window and close it PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } private void button1_Click(object sender, EventArgs e) { StartKiller(); } } }

    I create a winform application to show the MessageBox to test the example to close the MessageBox. I use a gif to show the result.

    Please note that, the code below, you need to input the caption of MessageBox which pop up.

     //It used to search the pop up MessageBox, please note the Message is the MessageBox caption. 
                IntPtr ptr = FindWindow(null, "Message"); //The Message if the caption of my MessageBox.
              

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, April 26, 2018 7:30 AM
    Moderator