zoukankan      html  css  js  c++  java
  • WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z

    操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作。那么对于WinForm程序怎么实现呢?

    实现原理:拦截Application响应操作系统发送到消息,如果是比如KeyDown、Mouse Move等输入设备相关的Message ID则表示程序是在有人操作的状态,反之则使用一个计数器累积到某值,也就是相当于多长时间无人操作则关闭功能界面回到主界面。使用 Application.AddMessageFilter方法添加消息的过滤机制。

    IMessageFilter接口:
    消息筛选器接口。命名空间: System.Windows.Forms
    程序集: System.Windows.Forms(在 system.windows.forms.dll 中)。AddMessageFilter方法传递的参数为实现IMessageFilter的一个类,那么必须实现PreFilterMessage方法,这就是实现消息筛选的方法。

    代码实现:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace WinFormApp
    {
        public class MyMessageFilter : IMessageFilter
        {
            public int iOperCount
            {
                get;
                set;
            }
     
            #region IMessageFilter 成员
            public bool PreFilterMessage(ref Message m)
            {
                //如果检测到有鼠标或则键盘的消息 可添加其他消息ID如触摸屏的点击事件ID
                if (m.Msg == (int)WindowsMessages.WM_KEYDOWN || m.Msg == 513 || m.Msg == 515 || m.Msg == 516 || m.Msg == 522
                    //|| m.Msg == (int)WindowsMessages.WM_MOUSEMOVE
                    //|| m.Msg == (int)WindowsMessages.WM_LBUTTONDOWN
                    //|| m.Msg == (int)WindowsMessages.WM_RBUTTONDOWN
                    || m.Msg == (int)WindowsMessages.WM_MBUTTONDOWN
                    )
                {
                    iOperCount = 0;
                }
                return false;
            }
            #endregion
        }
    }

     应用程序入口添加消息监控、过滤:

    public static MyMessageFilter msgFilter = new MyMessageFilter();      
    [STAThread]
    static void Main()
     {
          Application.AddMessageFilter(msgFilter);
          Application.Run( new frmMain());
     

     程序主界面开启一个定时器:

    /// <summary>
            /// 定时器设置为每10秒触发一次
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void timer1_Tick(object sender, EventArgs e)
            {
                Program.msgFilter.iOperCount++;
                if (Program.msgFilter.iOperCount > CLOSE_TIME)
                {
                    if (Application.OpenForms.Count > 1)
                        foreach (Form mdiFrm in Application.OpenForms)
                            if (mdiFrm.Name.ToLower() != "frmmain")
                                mdiFrm.Close();
                }
           }
  • 相关阅读:
    基于typora编写Markdown文档
    VMware Workstation常见的故障处理
    VMware Workstation产品常用的快捷键
    2
    1
    9
    8
    7
    6
    5
  • 原文地址:https://www.cnblogs.com/zeroone/p/3640305.html
Copyright © 2011-2022 走看看