日期:2014-05-18  浏览次数:20900 次

有谁可以实现自动关闭消息框MessageBox么?
c#:
  ......
  DialogResult PrtYesOrNo = MessageBox.Show("是否打印", "打印提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  ......
   

  不知道是否有自动关闭或定时关闭该消息框的实现方法。要实现的效果是在用户没有点击的情况下自动关闭该提示框以更加人性化。

------解决方案--------------------
http://www.cnblogs.com/eaglet/archive/2009/07/25/1529920.html
------解决方案--------------------
探讨
加个timer


------解决方案--------------------
自己用窗口模拟,再用timer实现
------解决方案--------------------
public void ShowMessageBoxTimeout(string text, string caption, int timeout)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox),
new CloseState(caption, timeout));
MessageBox.Show(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2);
}

private void CloseMessageBox(object state)
{
CloseState closeState = state as CloseState;

Thread.Sleep(closeState.Timeout);
IntPtr dlg = FindWindow(null, closeState.Caption);

if (dlg != IntPtr.Zero)
{
IntPtr result;
EndDialog(dlg, out result);
}
}

[DllImport("coredll.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("coredll.dll")]
static extern bool EndDialog(IntPtr hDlg, out IntPtr nResult);

private class CloseState
{
private int _Timeout;
public int Timeout
{
get
{
return _Timeout;
}
}

private string _Caption;
public string Caption
{
get
{
return _Caption;
}
}

public CloseState(string caption, int timeout)
{
_Timeout = timeout;
_Caption = caption;
}
}

 

使用时:
//提示框显示2秒钟后自动关闭
close_messageBox.ShowMessageBoxTimeout("发送完成!", "提示", 2000);

 

在winform下使用时,只需将coredll.dll改为win32.dll即可