在ppc上实现MessageBox自动消失
之所以有这篇文章,得首先感谢组员zhangjing同学,正是因为有了她的idea,我才会去寻找解决方案,希望我们组的成员能多多提出好的idea。实现MessageBox自动消失的类的源码如下:
?Download MessageBoxTimeOut.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | class MessageBoxTimeOut
{
private string _caption;
public void Show(string text, string caption, int timeout)
{
this._caption = caption;
StartTimer(timeout);
MessageBox.Show(text, caption);
}
private void StartTimer(int interval)
{
Timer timer = new Timer();
timer.Interval = interval;
timer.Tick += new EventHandler(Timer_Tick);
timer.Enabled = true;
}
private void Timer_Tick(object sender, EventArgs e)
{
KillMessageBox();
//停止计时器
((Timer)sender).Enabled = false;
}
[DllImport("coredll.dll", EntryPoint = "FindWindow",
CharSet = CharSet.Auto)]
private extern static IntPtr FindWindow
(string lpClassName, string lpWindowName);
[DllImport("coredll.dll", CharSet = CharSet.Auto)]
public static extern int PostMessage(IntPtr hWnd, int msg,
IntPtr wParam, IntPtr lParam);
public const int WM_CLOSE = 0x10;
private void KillMessageBox()
{
//查找MessageBox的弹出窗口,注意对应标题
IntPtr ptr = FindWindow(null, this._caption);
if (ptr != IntPtr.Zero)
{
//查找到窗口则关闭
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
} |
主要用的就是Timer()消息,是吗,哈哈
[回复]
sunshineyxp 回复:
七月 28th, 2010 at 15:13
恩 你越来越用vc的术语跟我交谈了,我不懂消息机制这些东西,只能模模糊糊知道。这儿就是用了Timer这个类来实现的,嘿嘿。
[回复]
不错哦““““有空来我这看看
[回复]