Teardown a MessageBox programmatically without user input

我是研究僧i 提交于 2019-12-01 05:21:05

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.

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.

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);

You might want to consider a LogFile for your messages, along with a richtextbox(or multiline Textbox) embeded in your main form, you could then post your messages in there(1 per line, along with a timestamp). As for your messagebox problem, im not sure there is a (nice) way to programmatically close them.(Aborting the thread won't work).

xeLin xel

Make an exception for your criteria to know when to start msgbox or no. Example:

if (criteria)
{
    new Thread(() => MessageBox.Show("Some text", "Some caption")).Start();
}
else
{
    //do nothing
}

if you use DevExpress, then you can do following: Application have property OpenForms which contains all open forms of application. You steel can`t find specific messagebox but you can close all XtraMessageBox. Or if you run MessageBox in some Task/Thread check it before closing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!