首页 > 代码库 > winCE/Windows 应用程序消息提示框自动消失功能

winCE/Windows 应用程序消息提示框自动消失功能

      近期在做winCE系统的扫描枪应用程序,遇到了一些问题,其中包括消失提示框在手持终端显示过小,

用户要求提示框提示几秒后自动关闭,Windows平台可以通过调用系统API以定时器的方式进行自动销毁。

不过在winCE上存在不同,由于winCE系统属于精简版的windows系统,所以在API上也是属于精简后的,

Windows平台销毁消息框用user32.dll中的FindWindow和PostMessage完成,而winCE平台并没有

user32.dll,不过对应的API在coredll.dll中。

windows平台示例代码:

技术分享
 1         [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
 2         internal static extern IntPtr FindWindow(string className,string windowName);
 3 
 4         [DllImport("user32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Auto)]
 5         internal static extern int PostMessage(IntPtr hWnd,int msg,IntPtr wParam,IntPtr lParam);
 6 
 7         internal const int WM_CLOSE = 0x10;
 8         Timer timer = new Timer();
 9 
10         private void btnAutoCloseMesaage_Click(object sender, EventArgs e)
11         {
12             runKillTimer();
13             MessageBox.Show("操作已完成,可以关闭!","Message");
14         }
15 
16         /// <summary>
17         /// 运行timer
18         /// </summary>
19         internal void runKillTimer()
20         {
21             timer.Interval = 2000;
22             timer.Tick+=timer_Tick;
23             timer.Start();
24         }
25 
26         /// <summary>
27         /// timer触发关闭消息框
28         /// </summary>
29         /// <param name="sender"></param>
30         /// <param name="e"></param>
31         private void timer_Tick(object sender, EventArgs e)
32         {
33             KillMessageBox();
34             timer.Stop();
35         }
36 
37         /// <summary>
38         /// 调用API查找窗体并关闭
39         /// </summary>
40         internal void KillMessageBox()
41         {
42             IntPtr msgPtr = FindWindow(null, "Message");
43             if (msgPtr != IntPtr.Zero)
44             {
45                 PostMessage(msgPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
46             }
47         }
View Code

winCE平台示例代码:

技术分享
 1 [DllImport("coredll.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
 2         internal static extern IntPtr FindWindow(string className,string windowName);
 3 
 4         [DllImport("coredll.dll", EntryPoint = "PostMessage", CharSet = CharSet.Auto)]
 5         internal static extern int PostMessage(IntPtr hWnd,int msg,IntPtr wParam,IntPtr lParam);
 6 
 7         internal const int WM_CLOSE = 0x10;
 8         Timer timer = new Timer();
 9 
10         private void btnAutoCloseMesaage_Click(object sender, EventArgs e)
11         {
12             runKillTimer();
13             MessageBox.Show("操作已完成,可以关闭!","Message");
14         }
15 
16         /// <summary>
17         /// 运行timer
18         /// </summary>
19         internal void runKillTimer()
20         {
21             timer.Interval = 2000;
22             timer.Tick+=new EventHandler(timer_Tick);
23             timer.Enabled = true;
24         }
25 
26         /// <summary>
27         /// timer触发关闭消息框
28         /// </summary>
29         /// <param name="sender"></param>
30         /// <param name="e"></param>
31         private void timer_Tick(object sender, EventArgs e)
32         {
33             KillMessageBox();
34             timer.Enabled = false;
35         }
36 
37         /// <summary>
38         /// 调用API查找窗体并关闭
39         /// </summary>
40         internal void KillMessageBox()
41         {
42             IntPtr msgPtr = FindWindow(null, "Message");
43             if (msgPtr != IntPtr.Zero)
44             {
45                 PostMessage(msgPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
46             }
47         }
View Code

 

ps:大家有其他好的意见或建议也可以多多交流。

winCE/Windows 应用程序消息提示框自动消失功能