有谁可以实现自动关闭消息框MessageBox么?

china1976 2009-10-25 02:07:40
c#:
......
DialogResult PrtYesOrNo = MessageBox.Show("是否打印", "打印提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
......


不知道是否有自动关闭或定时关闭该消息框的实现方法。要实现的效果是在用户没有点击的情况下自动关闭该提示框以更加人性化。
...全文
715 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
geqinggao 2012-07-19
  • 打赏
  • 举报
回复
无法加载 DLL“coredll.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
jieon 2009-10-28
  • 打赏
  • 举报
回复
用个循环不就行 麻烦。
china1976 2009-10-28
  • 打赏
  • 举报
回复
win32.dll改为user32.dll
可以了
但是现在感觉还是用模式窗口的效果要好一些,兼容性好且易控制,感谢各位,开始结帖散分了。
ping_2009 2009-10-28
  • 打赏
  • 举报
回复
将上面 “使用时:”之前的那段代码单独放到一个类里就可以,然后后面使用的时候就按照后面说的就行了……
ping_2009 2009-10-28
  • 打赏
  • 举报
回复
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即可
lsj_zrp 2009-10-28
  • 打赏
  • 举报
回复
自己用窗口模拟,再用timer实现
w_f_y4422 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ycagri 的回复:]
加个timer

[/Quote]
我觉得只是这样还不行,MessageBox只有静态的Show函数,不能用实例显示,显示之后也只能单击确定才能关闭。
最好自己编一个像MessageBox的窗体,就可以用timer控制了。
sohighthesky 2009-10-28
  • 打赏
  • 举报
回复
sendmesssage点击确定
guyehanxinlei 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 hooverhuang 的回复:]
引用 1 楼 ycagri 的回复:
加个timer


我也赞同这种方法,设置时间限制,超时就自动关闭
[/Quote]
Approve
HooverHuang 2009-10-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ycagri 的回复:]
加个timer

[/Quote]
我也赞同这种方法,设置时间限制,超时就自动关闭
china1976 2009-10-28
  • 打赏
  • 举报
回复
是c#,winform环境下
Sugar_Tiger 2009-10-26
  • 打赏
  • 举报
回复
http://www.cnblogs.com/eaglet/archive/2009/07/25/1529920.html
ping_2009 2009-10-25
  • 打赏
  • 举报
回复
是什么环境下的啊?我这里有wince和winform环境下的
ycagri 2009-10-25
  • 打赏
  • 举报
回复
加个timer

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧