如何在C#中自动隐藏消息框

时间:2015-01-01 09:17:09

标签: c# wpf

例如

ScriptManager.RegisterClientScriptBlock
(this.Page, this.GetType(), "MyScript", "alert('Saved Successfully !!');", true);

但是scriptmanager显示为错误

1 个答案:

答案 0 :(得分:0)

在WPF中

你可以像这样显示消息框

MessageBoxResult result = MessageBox.Show("saved successfully");

如果您想在一段时间后关闭它,请使用此

AutoClosingMessageBox.Show("Text", "Caption", 1000);

,类定义就像这样

public class AutoClosingMessageBox {
    System.Threading.Timer _timeoutTimer;
    string _caption;
    AutoClosingMessageBox(string text, string caption, int timeout) {
        _caption = caption;
        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
            null, timeout, System.Threading.Timeout.Infinite);
        MessageBox.Show(text, caption);
    }
    public static void Show(string text, string caption, int timeout) {
        new AutoClosingMessageBox(text, caption, timeout);
    }
    void OnTimerElapsed(object state) {
        IntPtr mbWnd = FindWindow(null, _caption);
        if(mbWnd != IntPtr.Zero)
            SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        _timeoutTimer.Dispose();
    }
    const int WM_CLOSE = 0x0010;
    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}

参考:thanx到@DmitryG