zoukankan      html  css  js  c++  java
  • 页面开机自启动,页面置顶显示,页面持续获得焦点,鼠标点击器源码

    首先讲一下本文的使用背景,现有以下需求:

    将一个展示页面开机后自动全屏显示,要求运行期间不允许有任何弹窗弹出遮挡住页面,且页面应保持焦点以保证键盘可以操作页面。

    开机自动启动我们可以将需要打开的页面的快捷方式或其他自启动的软件放在系统启动文件夹中,Windows操作系统中有两个启动文件夹

    路径如下所示:

    C:ProgramDataMicrosoftWindowsStart MenuProgramsStartUp


    C:UsersWindowsUserNameAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup

    其中"WindowsUserName"为使用电脑的用户名。

    实现全屏显示,浏览器拥有记忆功能,快捷方式打开页面实现全屏显示,关闭后再打开浏览器仍会记住上次打开的全屏显示。

     

    因为是设置为页面单一显示,不能操作其他,所以这也算是一种比较极端的页面使用方式,下面就介绍一下为实现以上效果而使用的方法以及一些相关的个人感觉比较有用的方法。

     实现预期目标制作了一个鼠标定时点击器,使用了Winform设计界面如下:

    代码及相关解释如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Threading;
    using Microsoft.Win32;
    using System.IO;
    
    namespace AutoClick
    {
        /// <summary>
        /// 鼠标按键的ASCLL码
        /// </summary>
        public enum MouseEventFlags
        {
            Move = 0x0001,          //移动
            LeftDown = 0x0002,      //左键被按下
            LeftUp = 0x0004,        //左键被按放开
            RightDown = 0x0008,     //右键被按下
            RightUp = 0x0010,       //右键被按放开
            MiddleDown = 0x0020,    //中间键被按下
            MiddleUp = 0x0040,      //中间键被按放开
            Wheel = 0x0800,         //滑轮转动
            Absolute = 0x8000
        }
        public partial class AutoClick : Form
        {
            /// <summary>
            /// 鼠标位置X坐标
            /// </summary>
            private int positionX = 400;
            /// <summary>
            /// 鼠标位置Y坐标
            /// </summary>
            private int positionY = 300;
            /// <summary>
            /// 定义定期事件
            /// </summary>
            private System.Timers.Timer mTimer;
            /// <summary>
            /// 鼠标移动事件
            /// </summary>
            /// <param name="dwFlags"></param>
            /// <param name="dx"></param>
            /// <param name="dy"></param>
            /// <param name="dwData"></param>
            /// <param name="dwExtraInfo"></param>
            [DllImport("User32")]
            public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
            /// <summary>
            /// 设置鼠标位置
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <returns></returns>
            [DllImport("user32.dll")]
            static extern int SetCursorPos(int x, int y);
    
            /// <summary>
            /// 构造函数
            /// </summary>
            public AutoClick()
            {
                InitializeComponent();
    
                //获取坐标和时间信息
                string xAndYAndTime = ReadText();
                string[] boxText = xAndYAndTime.Split(',');
    
                //
                mTimer = new System.Timers.Timer();
                mTimer.Interval = int.Parse(boxText[2]);//设置时间间隔
                mTimer.Elapsed += new System.Timers.ElapsedEventHandler(mTimer_Elapsed);//设置达到间隔时发生的事件
    
                //设置鼠标坐标值
                positionX = int.Parse(boxText[0]);
                positionY = int.Parse(boxText[1]);
    
                //设置页面显示信息
                txtX.Text = boxText[0];
                txtY.Text = boxText[1];
                txtTime.Text = boxText[2];
    
                //
                mTimer.Start();
    
                //更新页面控件状态
                UpdateEnabled();
            }
    
            /// <summary>
            /// 达到时间间隔时触发
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void mTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                SetCursorPos(positionX, positionY);
                mouse_event((int)(2 | 32768), positionX, positionY, 0, IntPtr.Zero);
                Thread.Sleep(10);
                mouse_event((int)(4 | 32768), positionX, positionY, 0, IntPtr.Zero);
            }
    
            /// <summary>
            /// 启动/停止按钮点击事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnDo_Click(object sender, EventArgs e)
            {
                if (btnDo.Text == "启动")//如果按钮为启动时,开启定时事件,并设置按钮为停止
                {
                    mTimer.Start();
                    positionX = int.Parse(txtX.Text.Trim());
                    positionY = int.Parse(txtY.Text.Trim()); 
                    mTimer.Interval = int.Parse(txtTime.Text.Trim());
                    btnDo.Text = "停止";
                }
                else//如果按钮为停止时,关闭定时事件,并设置按钮为启动
                {
                    mTimer.Stop();
                    btnDo.Text = "启动";
                }
                UpdateEnabled();
                //将鼠标坐标和时间信息写入txt文件
                WriteText();
            }
    
            /// <summary>
            /// 设置开机自启动  
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnAutoStart_Click(object sender, EventArgs e)
            {
                MessageBox.Show("设置开机自启动,需要修改注册表", "提示");
                string path = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
                rk2.SetValue("JcShutdown", path);
                rk2.Close();
                rk.Close();
            }
    
            /// <summary>
            ///取消开机自启动 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnAutoClose_Click(object sender, EventArgs e)
            {
                MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
                string path = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
                rk2.DeleteValue("JcShutdown", false);
                rk2.Close();
                rk.Close();
            }
    
            /// <summary>
            /// 禁用/开启输入框
            /// </summary>
            private void UpdateEnabled()
            {
                if (btnDo.Text == "启动")
                {
                    txtX.Enabled = true;
                    txtY.Enabled = true;
                    txtTime.Enabled = true;
                }
                else
                {
                    txtX.Enabled = false;
                    txtY.Enabled = false;
                    txtTime.Enabled = false;
                }
            }
    
            /// <summary>
            /// 读取txt文档
            /// </summary>
            private string ReadText()
            {
                string xAndYAndTime = File.ReadAllText(@".AutoClick.txt");
                return xAndYAndTime;
            }
    
            /// <summary>
            /// 写入txt文档
            /// </summary>
            private void WriteText()
            {
                string xAndYAndTime = txtX.Text.Trim() + "," + txtY.Text.Trim() + "," + txtTime.Text.Trim();
                File.WriteAllText(@".AutoClick.txt", xAndYAndTime);
            }
        }
    }

    下面介绍一下一些其他方法

    一、使用软件的方法实现页面置顶和获取焦点

    使用了两张软件:AutoHotKey   +    鼠大侠 鼠标连点器

    思路: 鼠大侠 鼠标连点器可实现鼠标自动点击,一般弹窗都会是比较小的窗口,所以可以将鼠标点击位置设置在页面边缘区域实现点击我们需要的页面确保页面置顶和获取焦点。

     

        但问题是鼠大侠 鼠标连点器不能开机自启动,且必须通过操作热键或鼠标才能启动或关闭。热键,就是按键盘按键或组合键。

    *针对鼠大侠 鼠标连点器不能开机自启动的问题我们可以将该软件放在上面所述的启动文件夹中实现开机自启。

    *对于必须通过操作热键或鼠标才能启动或关闭则需要我们的另一款软件“AutoHotKey”。

    AutoHotKeyWINDOWS出得一个类似按键精灵的软件,下载好之后安装上,然后在D盘下面建一个文本文件,在里面写上

    Send,{a}
    
    Return

    a那个地方就是你要按的键,然后保存,把文档的名字改为1.ahk 这个一定要保存在d盘下,

    然后再建立一个文本文件在里面写上

    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun]
    
    "1.ahk"="D:\1.ahk"

    保存,随便取个名字,但是要以.reg结尾。

     双击那个.reg的文件,会提示你是否添加注册表项,选择是

    然后杀毒软件会报有软件想修改你得注册表键值,选择允许,完成!

    最后将1.ahk文件新建个快捷方式放到启动文件夹中即可,为了实现我们的效果,文件启动需要相应的顺序,分别是 页面,鼠标点击器,热键操作。排好相应的顺序,例如通过名字,我们的任务算完成了!重启电脑后发现实现了想要的效果。

    *********************************对于鼠标点击器网上有很多种,如需要的话可以自行挑选,大都是使用热键的方式实现开启与关闭*************************************

     

    二、使用代码的方法实现页面置顶和获取焦点

    使用代码的方法很简单,只需要在前台页面中设置定时获取页面焦点即可

        <script>
            setInterval(function () {
                window.focus();
            }, 5000);
        </script>

    这样可以实现无论操作什么其他界面,页面都会自动跳回我们需要的界面并获取到焦点,但是在使用过程中发现ie浏览器全屏后再操作其他界面就无法跳转回目标界面,原因尚未找到,希望有了解知晓的前辈可以指导解惑一下。

     

    三、间接打开界面

    该方法只是想出的一个感觉可行的方法并未亲自去实现查看效果,在此也记录一下,那就是开机自启一个页面,该页面使用window.showModalDialog方法自动跳转到我们需要的目标界面,也可实现画面前置不可操作其他页面。

     

    四、单纯实现页面置顶的软件方法

    单纯实现页面置顶的话可以使用软件“DeskPins”.

              

    DeskPins可以自动设置规则,将某些或某类窗口置顶显示,可以实现开机自启动设置。

    *************还有许多其他窗口置顶显示的软件有需要的可以了自行再解一下*****************

     五、Windows.Topmost属性

    Gets or sets a value that indicates whether a window appears in the topmost z-order.

    (获取或设置一个值,该值指示是否窗口显示在最顶层。)

    命名空间:   System.Windows
    程序集:  PresentationFramework(位于 PresentationFramework.dll)

    public bool Topmost { get; set; }
    

    属性值

    Type: System.Boolean

    true 如果窗口为最顶层元素否则为 false

    备注
     一个窗口,其 Topmost 属性设置为 true 所有窗口的上面显示其 Topmost 属性设置为 false

    具有 windows 组中 Topmost 属性设置为 true, ,目前处于激活状态的窗口是最顶层窗口。 同样为具有 windows 组 Topmost 属性设置为 false

    System_CAPS_note说明

    当窗口承载在浏览器中时,不能设置或获取该属性。

     
     
     
     

    标识符字段,

    TopmostProperty

    元数据属性设置为 true

     

  • 相关阅读:
    codevs1735 方程的解数(meet in the middle)
    cf280C. Game on Tree(期望线性性)
    使用ASP.NET上传多个文件到服务器
    Oracle DB 数据库维护
    poj 3237(树链剖分+线段树)
    undefined reference to 'pthread_create'
    ios开发-调用系统自带手势
    Mysql创建、删除用户、查询所有用户等教程,提升您的MYSQL安全度!
    Number Sequence_hdu_1005(规律)
    SCU 4313 把一棵树切成每段K个点 (n%k)剩下的点不管
  • 原文地址:https://www.cnblogs.com/ingvner/p/7407207.html
Copyright © 2011-2022 走看看