Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264872
  • 博文数量: 757
  • 博客积分: 40040
  • 博客等级: 大将
  • 技术积分: 4935
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:37
文章分类

全部博文(757)

文章存档

2011年(1)

2008年(756)

我的朋友

分类:

2008-09-09 12:44:58

winfrom让弹出的MessageBox在指定时间内自动销毁,代码如下:

private void Button_Click(object sender, System.EventArgs e)
{
StartKiller();
MessageBox.Show("这里是MessageBox弹出的内容","MessageBox");
MessageBox.Show("这里是跟随运行的窗口","窗口");
}
private void StartKiller()
{
Timer timer = new Timer();
timer.Interval = 10000;//10秒启动
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止计时器
((Timer)sender).Stop();
}
private void KillMessageBox()
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null,"MessageBox");
if(ptr != IntPtr.Zero)
{
//查找到窗口则关闭
PostMessage(ptr,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);
}
}
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet=CharSet.Auto)]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10;
【责编:snow】

--------------------next---------------------

阅读(198) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~