zoukankan      html  css  js  c++  java
  • WPF窗口长时间无人操作鼠标自动隐藏

    在软件开发中有时会有等待一段时间无人操作后隐藏鼠标,可能原因大致如下:

    1.为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面;

    2.软件为了更好的播放效果,需要隐藏鼠标。

    这里写的是第二种情况,wpf做播放时,需要隐藏鼠标。

    思路是:假如3s鼠标不动则隐藏,设计了计时器的间隔时间为1s,并添加鼠标没移动的计数器,计数器达到3才执行程序。实现是这样的:每隔1s检测鼠标是否移动,如果不移动则计数器加1,如果中途鼠标移动,则计数器清零,要达到计数器计数为3,则要3次鼠标检测中鼠标都不移动,这样从鼠标停止移动,到计数器达到3,刚好是3s,能够达到3s鼠标不动则隐藏程序执行;

    一、封装好的监视鼠标移动的类文件:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Runtime.InteropServices;
    
    namespace EBS.Common {
        public class MouseMonitorHelper {
            private static Point mousePosition;    //鼠标的位置
            public static int CheckCount;   //检测鼠标位置的次数
    
            //判断鼠标是否移动        
            public static bool HaveUsedTo() {
                Point point = GetMousePoint();
                if (point == mousePosition) return false;
                mousePosition = point; return true;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MPoint {
                public int X;
                public int Y;
                public MPoint(int x, int y) {
                    this.X = x;
                    this.Y = y;
                }
            }
    
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern bool GetCursorPos(out MPoint mpt);
    
            // 获取当前屏幕鼠标位置           
            public static Point GetMousePoint() {
                MPoint mpt = new MPoint();
                GetCursorPos(out mpt);
                Point p = new Point(mpt.X, mpt.Y);
                return p;
            }
        }
    }
    View Code


     

    二、程序中调用Timer事件:

    private DispatcherTimer Timer_MouseMove;
    
    private void Window_Loaded(object sender, RoutedEventArgs e) {
                // 设置鼠标隐藏、显示
                this.Timer_MouseMove = new DispatcherTimer();
                this.Timer_MouseMove.Tick +=new EventHandler(Timer_MouseMove_Tick);
                this.Timer_MouseMove.Interval = new TimeSpan(0, 0, 1);
                this.Timer_MouseMove.Start();
            }
    
    
    private void Timer_MouseMove_Tick(object sender, EventArgs e) {
                try {
                    if (!MouseMonitorHelper.HaveUsedTo()) {
                        MouseMonitorHelper.CheckCount++;
                        if (MouseMonitorHelper.CheckCount == 3) {
                            MouseMonitorHelper.CheckCount = 0;
                            // 关闭按钮隐藏、鼠标隐藏
                            this.cnsExist.Visibility = Visibility.Hidden;
                            Mouse.OverrideCursor = Cursors.None;
                        }
                    } else MouseMonitorHelper.CheckCount = 0;
                } catch {
                    throw new NotImplementedException();
                }
            }
    
    
    private void DockPanel_MouseMove(object sender, MouseEventArgs e) {
                this.cnsExist.Visibility = Visibility.Visible;
                Mouse.OverrideCursor = Cursors.Arrow;
    View Code


    另外如果有设计到鼠标焦点的情况,请参考WPF程序长时间无人操作

  • 相关阅读:
    分布式锁设计方案
    ACID/CAP/BASE 理论知识
    分布式ID设计方案
    perl代码调试
    文档型数据库设计模式-如何存储树形数据 [转]
    树形结构的数据库表Schema设计
    http协议相关
    HTTPS加密原理(转)
    VBA读写XML文件
    VBA 刷新数据透视表
  • 原文地址:https://www.cnblogs.com/alvinyue/p/3600867.html
Copyright © 2011-2022 走看看