zoukankan      html  css  js  c++  java
  • 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:大家有其他好的意见或建议也可以多多交流。

  • 相关阅读:
    Java实现 LeetCode 69 x的平方根
    Java实现 LeetCode 68 文本左右对齐
    Java实现 LeetCode 68 文本左右对齐
    Java实现 LeetCode 68 文本左右对齐
    Java实现 LeetCode 67 二进制求和
    Java实现 LeetCode 67 二进制求和
    Java实现 LeetCode 67 二进制求和
    Java实现 LeetCode 66 加一
    Java实现 LeetCode 66 加一
    CxSkinButton按钮皮肤类
  • 原文地址:https://www.cnblogs.com/ultimateWorld/p/6183922.html
Copyright © 2011-2022 走看看