Teardown a MessageBox programmatically without user input

12,282

Solution 1

Why not make a custom message box? You could have it display for a fixed amount of time or until your app closes it through code.

Create an instance of your custom message box (child of Form class) and save it as a variable (ex. MyMessageBox), then show it with MyMessageBox.Show();.When you want to take it down, call MyMessageBox.Close();

If you have problems closing it if you opened it in another thread, try calling MyMessageBox.Invoke(new Action(() => {MyMessageBox.Close();})); That will run the command MyMessageBox.Close(); on the same thread MyMessageBox was created in, as to not cause issues.

Solution 2

This worked for me

public partial class Form1 : Form
{
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.Dll")]
    static extern int PostMessage(IntPtr hWnd, UInt32 msg, int wParam, int lParam);

    const UInt32 WM_CLOSE = 0x0010;

    Thread thread;

    public Form1()
    {
        InitializeComponent();

        thread = new Thread(ShowMessageBox);
        thread.Start();
    }

    void CloseMessageBox()
    {
        IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, "Caption");
        if (hWnd != IntPtr.Zero)
            PostMessage(hWnd, WM_CLOSE, 0, 0);

        if (thread.IsAlive)
            thread.Abort();
    }

    static void ShowMessageBox()
    {
        MessageBox.Show("Message", "Caption");
    }
}

Now you can use CloseMessageBox() to close the message box.

But have in mind, the captions must be the same in CloseMessageBox() and ShowMessageBox()!

Maybe through a global variable but that's up to you.

Solution 3

you can use below class to create a MessageBox easily:

using System;
using System.Runtime.InteropServices;

public class MsgBox
{
    [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    static extern bool EndDialog(IntPtr hDlg, int nResult);

    [DllImport("user32.dll")]
    static extern int MessageBoxTimeout(IntPtr hwnd, string txt, string caption,
        int wtype, int wlange, int dwtimeout);

    const int WM_CLOSE = 0x10;

    public static int Show(string text, string caption, int milliseconds, MsgBoxStyle style)
    {
        return MessageBoxTimeout(IntPtr.Zero, text, caption, (int)style, 0, milliseconds);
    }

    public static int Show(string text, string caption, int milliseconds, int style)
    {
        return MessageBoxTimeout(IntPtr.Zero, text, caption, style, 0, milliseconds);
    }
    public static int Show(string text, string caption, int milliseconds)
    {
        return MessageBoxTimeout(IntPtr.Zero, text, caption, 0, 0, milliseconds);
    }
}

public enum MsgBoxStyle
{
    OK = 0, OKCancel = 1, AbortRetryIgnore = 2, YesNoCancel = 3, YesNo = 4,
    RetryCancel = 5, CancelRetryContinue = 6,

    RedCritical_OK = 16, RedCritical_OKCancel = 17, RedCritical_AbortRetryIgnore = 18,
    RedCritical_YesNoCancel = 19, RedCritical_YesNo = 20,
    RedCritical_RetryCancel = 21, RedCritical_CancelRetryContinue = 22,

    BlueQuestion_OK = 32, BlueQuestion_OKCancel = 33, BlueQuestion_AbortRetryIgnore = 34,
    BlueQuestion_YesNoCancel = 35, BlueQuestion_YesNo = 36,
    BlueQuestion_RetryCancel = 37, BlueQuestion_CancelRetryContinue = 38,

    YellowAlert_OK = 48, YellowAlert_OKCancel = 49, YellowAlert_AbortRetryIgnore = 50,
    YellowAlert_YesNoCancel = 51, YellowAlert_YesNo = 52,
    YellowAlert_RetryCancel = 53, YellowAlert_CancelRetryContinue = 54,

    BlueInfo_OK = 64, BlueInfo_OKCancel = 65, BlueInfo_AbortRetryIgnore = 66,
    BlueInfo_YesNoCancel = 67, BlueInfo_YesNo = 68,
    BlueInfo_RetryCancel = 69, BlueInfo_CancelRetryContinue = 70,
}

Usage:

MsgBox.Show("this is content", "this is caption", 3000);
Share:
12,282
Michael Mankus
Author by

Michael Mankus

I've been programming since I was 16 years old. The majority of my experience is in writing non-commercial software for both the private and government sectors. My specialty is writing testbed applications and data collection / playback applications. Familiar languages include C#, C++, C, and Java.

Updated on June 23, 2022

Comments

  • Michael Mankus
    Michael Mankus almost 2 years

    I'm using a MessageBox from time to time to pop up alert messages for the user. My particular application can be setup to run remotely so there are times where the user is in front of the computer and other times where the user may not be in front of the computer for hours or even days. Sometimes I popup an alert message using MessageBox but after some period of the time the alert is no longer relevant. For example, I popup an alert that a task can't be completed because of some criteria not being met. A few minutes later that criteria is met and the task begins. That MessageBox is no longer relevant.

    I want to be able to programmatically close the MessageBox in these cases where the message is no longer relevant. Is this possible? Currently I create my MessageBox objects in a thread using:

    new Thread(() => MessageBox.Show("Some text", "Some caption")).Start();
    

    I do this so that the application can continue to work in the background without being halted by the MessageBox. Any suggestions?

  • Tafari
    Tafari over 10 years
    But as he stated it is possible that the cirteria is not met at the current time, but later it will be, so the Message will not be relevant but will be still on. And the question is how to Remove a MessageBox not how to Start it.
  • King King
    King King over 10 years
    I doubt if there was any downvoter jumping in here, this answer would receive at least 2 downvotes.
  • Alex
    Alex about 8 years
    In my tests (Windows 7) it works with MessageBox.Show(this, "Abort?", AlertCaption, MessageBoxButtons.OKCancel) but not with MessageBox.Show(this, "Abort?", AlertCaption, MessageBoxButtons.YesNo). Nothing happens with the latter (the alert remains open). Any idea?
  • Alex
    Alex about 8 years
    I noticed that hitting Esc doesn't work in YesNo dialogs (and therefore WM_CLOSE message does nothing). Any idea what to send to the window to make it respond as if No was pressed?
  • Alex
    Alex about 8 years
    Well, it looks very complicated and someone mentioned that it wouldn't work on non-English Windows. I think I'll have to refactor my UI so that it would utilize OK/Cancel rather than Yes/No.
  • Yoda
    Yoda almost 8 years
    Andy is there a C++ method to move the MessageBox to the specified location?