当前位置:   article > 正文

【原创】C#浏览器监控程序核心代码_c# 监听所有浏览器url

c# 监听所有浏览器url

在一些公共场合,需要为公众提供一些公共查询的计算机,其实就是一些Windows计算机。我们希望使用计算机的人只使用浏览器打开某个指定的网页。

在使用过程中,用户查询完毕之后,往往不主动关闭自己浏览过的网页,对于新来的用户来说造成一定的困扰。各个屏幕上花花绿绿,乱七八糟的信息,对于整个环境的整洁性来说,很不和谐。

我们的解决把法就是,为浏览器设置默认主页。在用户新打开浏览器的时候,就会直接打开我们希望用户访问的页面。但是新打开浏览器这个操作需要用户自己点击鼠标,而且还有一个问题,“关闭浏览器”,也需要用户主动操作。

接下来,我们用C#代码,制作一个软件,自动关闭浏览器,再自动开启浏览器。

实现流程如下:

  1. 计算机检测浏览器是否开启,如果没有开启,则开启浏览器。
  2. 计算机检测用户是否正在操作计算机,比如2分钟无键盘鼠标输入,则认为用户已离开,则关闭浏览器。

难点1

鼠标键盘hook
还好有人已经制作好代码,MouseKeyboardLibrary
源码如下
GlobalHook.cs

/// <summary>
/// Abstract base class for Mouse and Keyboard hooks
/// </summary>
public abstract class GlobalHook
{
    #region Windows API Code
    [StructLayout(LayoutKind.Sequential)]
    protected class POINT
    {
        public int x;
        public int y;
    }
    [StructLayout(LayoutKind.Sequential)]
    protected class MouseHookStruct
    {
        public POINT pt;
        public int hwnd;
        public int wHitTestCode;
        public int dwExtraInfo;
    }
    [StructLayout(LayoutKind.Sequential)]
    protected class MouseLLHookStruct
    {
        public POINT pt;
        public int mouseData;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }
    [StructLayout(LayoutKind.Sequential)]
    protected class KeyboardHookStruct
    {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }
    [DllImport("user32.dll", CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    protected static extern int SetWindowsHookEx(
      int idHook,
      HookProc lpfn,
      IntPtr hMod,
      int dwThreadId);
    [DllImport("user32.dll", CharSet = CharSet.Auto,
      CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    protected static extern int UnhookWindowsHookEx(int idHook);
    [DllImport("user32.dll", CharSet = CharSet.Auto,
       CallingConvention = CallingConvention.StdCall)]
    protected static extern int CallNextHookEx(
      int idHook,
      int nCode,
      int wParam,
      IntPtr lParam);
    [DllImport("user32")]
    protected static extern int ToAscii(
      int uVirtKey,
      int uScanCode,
      byte[] lpbKeyState,
      byte[] lpwTransKey,
      int fuState);
    [DllImport("user32")]
    protected static extern int GetKeyboardState(byte[] pbKeyState);
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    protected static extern short GetKeyState(int vKey);
    protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);
    protected const int WH_MOUSE_LL = 14;
    protected const int WH_KEYBOARD_LL = 13;
    protected const int WH_MOUSE = 7;
    protected const int WH_KEYBOARD = 2;
    protected const int WM_MOUSEMOVE = 0x200;
    protected const int WM_LBUTTONDOWN = 0x201;
    protected const int WM_RBUTTONDOWN = 0x204;
    protected const int WM_MBUTTONDOWN = 0x207;
    protected const int WM_LBUTTONUP = 0x202;
    protected const int WM_RBUTTONUP = 0x205;
    protected const int WM_MBUTTONUP = 0x208;
    protected const int WM_LBUTTONDBLCLK = 0x203;
    protected const int WM_RBUTTONDBLCLK = 0x206;
    protected const int WM_MBUTTONDBLCLK = 0x209;
    protected const int WM_MOUSEWHEEL = 0x020A;
    protected const int WM_KEYDOWN = 0x100;
    protected const int WM_KEYUP = 0x101;
    protected const int WM_SYSKEYDOWN = 0x104;
    protected const int WM_SYSKEYUP = 0x105;
    protected const byte VK_SHIFT = 0x10;
    protected const byte VK_CAPITAL = 0x14;
    protected const byte VK_NUMLOCK = 0x90;
    protected const byte VK_LSHIFT = 0xA0;
    protected const byte VK_RSHIFT = 0xA1;
    protected const byte VK_LCONTROL = 0xA2;
    protected const byte VK_RCONTROL = 0x3;
    protected const byte VK_LALT = 0xA4;
    protected const byte VK_RALT = 0xA5;
    protected const byte LLKHF_ALTDOWN = 0x20;
    #endregion
    #region Private Variables
    protected int _hookType;
    protected int _handleToHook;
    protected bool _isStarted;
    protected HookProc _hookCallback;
    #endregion
    #region Properties
    public bool IsStarted
    {
        get
        {
            return _isStarted;
        }
    }
    #endregion
    #region Constructor
    public GlobalHook()
    {
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    }
    #endregion
    #region Methods
    public void Start()
    {
        if (!_isStarted &&
          _hookType != 0)
        {
            // Make sure we keep a reference to this delegate!
            // If not, GC randomly collects it, and a NullReference exception is thrown
            _hookCallback = new HookProc(HookCallbackProcedure);
            _handleToHook = SetWindowsHookEx(
              _hookType,
              _hookCallback,
              Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
              0);
            //IntPtr.Zero;
            // Were we able to sucessfully start hook?
            if (_handleToHook != 0)
            {
                _isStarted = true;
            }
        }
    }
    public void Stop()
    {
        if (_isStarted)
        {
            UnhookWindowsHookEx(_handleToHook);
            _isStarted = false;
        }
    }
    protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)
    {
        // This method must be overriden by each extending hook
        return 0;
    }
    protected void Application_ApplicationExit(object sender, EventArgs e)
    {
        if (_isStarted)
        {
            Stop();
        }
    }
    #endregion
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162

KeyboardHook.cs

 /// <summary>
 /// Captures global keyboard events
 /// </summary>
 public class KeyboardHook : GlobalHook
 {
     #region Events
     public event KeyEventHandler KeyDown;
     public event KeyEventHandler KeyUp;
     public event KeyPressEventHandler KeyPress;
     #endregion
     #region Constructor
     public KeyboardHook()
     {
         _hookType = WH_KEYBOARD_LL;
     }
     #endregion
     #region Methods
     protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
     {
         bool handled = false;
         if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
         {
             KeyboardHookStruct keyboardHookStruct =
               (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
             // Is Control being held down?
             bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
                     ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
             // Is Shift being held down?
             bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
                    ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
             // Is Alt being held down?
             bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
                   ((GetKeyState(VK_RALT) & 0x80) != 0);
             // Is CapsLock on?
             bool capslock = (GetKeyState(VK_CAPITAL) != 0);
             // Create event using keycode and control/shift/alt values found above
             KeyEventArgs e = new KeyEventArgs(
               (Keys)(
                 keyboardHookStruct.vkCode |
                 (control ? (int)Keys.Control : 0) |
                 (shift ? (int)Keys.Shift : 0) |
                 (alt ? (int)Keys.Alt : 0)
                 ));
             // Handle KeyDown and KeyUp events
             switch (wParam)
             {
                 case WM_KEYDOWN:
                 case WM_SYSKEYDOWN:
                     if (KeyDown != null)
                     {
                         KeyDown(this, e);
                         handled = handled || e.Handled;
                     }
                     break;
                 case WM_KEYUP:
                 case WM_SYSKEYUP:
                     if (KeyUp != null)
                     {
                         KeyUp(this, e);
                         handled = handled || e.Handled;
                     }
                     break;
             }
             // Handle KeyPress event
             if (wParam == WM_KEYDOWN &&
               !handled &&
               !e.SuppressKeyPress &&
               KeyPress != null)
             {
                 byte[] keyState = new byte[256];
                 byte[] inBuffer = new byte[2];
                 GetKeyboardState(keyState);
                 if (ToAscii(keyboardHookStruct.vkCode,
                      keyboardHookStruct.scanCode,
                      keyState,
                      inBuffer,
                      keyboardHookStruct.flags) == 1)
                 {
                     char key = (char)inBuffer[0];
                     if ((capslock ^ shift) && Char.IsLetter(key))
                         key = Char.ToUpper(key);
                     KeyPressEventArgs e2 = new KeyPressEventArgs(key);
                     KeyPress(this, e2);
                     handled = handled || e.Handled;
                 }
             }
         }
         if (handled)
         {
             return 1;
         }
         else
         {
             return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
         }
     }
     #endregion
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

KeyboardSimulator.cs

/// <summary>
/// Simulate keyboard key presses
/// </summary>
public static class KeyboardSimulator
{
    #region Windows API Code
    const int KEYEVENTF_EXTENDEDKEY = 0x1;
    const int KEYEVENTF_KEYUP = 0x2;
    [DllImport("user32.dll")]
    static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
    #endregion
    #region Methods
    public static void KeyDown(Keys key)
    {
        keybd_event(ParseKey(key), 0, 0, 0);
    }
    public static void KeyUp(Keys key)
    {
        keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
    }
    public static void KeyPress(Keys key)
    {
        KeyDown(key);
        KeyUp(key);
    }
    public static void SimulateStandardShortcut(StandardShortcut shortcut)
    {
        switch (shortcut)
        {
            case StandardShortcut.Copy:
                KeyDown(Keys.Control);
                KeyPress(Keys.C);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.Cut:
                KeyDown(Keys.Control);
                KeyPress(Keys.X);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.Paste:
                KeyDown(Keys.Control);
                KeyPress(Keys.V);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.SelectAll:
                KeyDown(Keys.Control);
                KeyPress(Keys.A);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.Save:
                KeyDown(Keys.Control);
                KeyPress(Keys.S);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.Open:
                KeyDown(Keys.Control);
                KeyPress(Keys.O);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.New:
                KeyDown(Keys.Control);
                KeyPress(Keys.N);
                KeyUp(Keys.Control);
                break;
            case StandardShortcut.Close:
                KeyDown(Keys.Alt);
                KeyPress(Keys.F4);
                KeyUp(Keys.Alt);
                break;
            case StandardShortcut.Print:
                KeyDown(Keys.Control);
                KeyPress(Keys.P);
                KeyUp(Keys.Control);
                break;
        }
    }
    static byte ParseKey(Keys key)
    {
        // Alt, Shift, and Control need to be changed for API function to work with them
        switch (key)
        {
            case Keys.Alt:
                return (byte)18;
            case Keys.Control:
                return (byte)17;
            case Keys.Shift:
                return (byte)16;
            default:
                return (byte)key;
        }
    }
    #endregion
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

MouseButton.cs

/// <summary>
/// Mouse buttons that can be pressed
/// </summary>
public enum MouseButton
{
    Left = 0x2,
    Right = 0x8,
    Middle = 0x20
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

MouseHook.cs

 /// <summary>
 /// Captures global mouse events
 /// </summary>
 public class MouseHook : GlobalHook
 {
     #region MouseEventType Enum
     private enum MouseEventType
     {
         None,
         MouseDown,
         MouseUp,
         DoubleClick,
         MouseWheel,
         MouseMove
     }
     #endregion
     #region Events
     public event MouseEventHandler MouseDown;
     public event MouseEventHandler MouseUp;
     public event MouseEventHandler MouseMove;
     public event MouseEventHandler MouseWheel;
     public event EventHandler Click;
     public event EventHandler DoubleClick;
     #endregion
     #region Constructor
     public MouseHook()
     {
         _hookType = WH_MOUSE_LL;
     }
     #endregion
     #region Methods
     protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
     {
         if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
         {
             MouseLLHookStruct mouseHookStruct =
               (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
             MouseButtons button = GetButton(wParam);
             MouseEventType eventType = GetEventType(wParam);
             MouseEventArgs e = new MouseEventArgs(
               button,
               (eventType == MouseEventType.DoubleClick ? 2 : 1),
               mouseHookStruct.pt.x,
               mouseHookStruct.pt.y,
               (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));
             // Prevent multiple Right Click events (this probably happens for popup menus)
             if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
             {
                 eventType = MouseEventType.None;
             }
             switch (eventType)
             {
                 case MouseEventType.MouseDown:
                     if (MouseDown != null)
                     {
                         MouseDown(this, e);
                     }
                     break;
                 case MouseEventType.MouseUp:
                     if (Click != null)
                     {
                         Click(this, new EventArgs());
                     }
                     if (MouseUp != null)
                     {
                         MouseUp(this, e);
                     }
                     break;
                 case MouseEventType.DoubleClick:
                     if (DoubleClick != null)
                     {
                         DoubleClick(this, new EventArgs());
                     }
                     break;
                 case MouseEventType.MouseWheel:
                     if (MouseWheel != null)
                     {
                         MouseWheel(this, e);
                     }
                     break;
                 case MouseEventType.MouseMove:
                     if (MouseMove != null)
                     {
                         MouseMove(this, e);
                     }
                     break;
                 default:
                     break;
             }
         }
         return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
     }
     private MouseButtons GetButton(Int32 wParam)
     {
         switch (wParam)
         {
             case WM_LBUTTONDOWN:
             case WM_LBUTTONUP:
             case WM_LBUTTONDBLCLK:
                 return MouseButtons.Left;
             case WM_RBUTTONDOWN:
             case WM_RBUTTONUP:
             case WM_RBUTTONDBLCLK:
                 return MouseButtons.Right;
             case WM_MBUTTONDOWN:
             case WM_MBUTTONUP:
             case WM_MBUTTONDBLCLK:
                 return MouseButtons.Middle;
             default:
                 return MouseButtons.None;
         }
     }
     private MouseEventType GetEventType(Int32 wParam)
     {
         switch (wParam)
         {
             case WM_LBUTTONDOWN:
             case WM_RBUTTONDOWN:
             case WM_MBUTTONDOWN:
                 return MouseEventType.MouseDown;
             case WM_LBUTTONUP:
             case WM_RBUTTONUP:
             case WM_MBUTTONUP:
                 return MouseEventType.MouseUp;
             case WM_LBUTTONDBLCLK:
             case WM_RBUTTONDBLCLK:
             case WM_MBUTTONDBLCLK:
                 return MouseEventType.DoubleClick;
             case WM_MOUSEWHEEL:
                 return MouseEventType.MouseWheel;
             case WM_MOUSEMOVE:
                 return MouseEventType.MouseMove;
             default:
                 return MouseEventType.None;
         }
     }
     #endregion
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138

MousePoint.cs

 /// <summary>
 /// And X, Y point on the screen
 /// </summary>
 public struct MousePoint
 {
     public MousePoint(Point p)
     {
         X = p.X;
         Y = p.Y;
     }
     public int X;
     public int Y;
     public static implicit operator Point(MousePoint p)
     {
         return new Point(p.X, p.Y);
     }
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

MouseSimulator.cs

/// <summary>
/// Operations that simulate mouse events
/// </summary>
public static class MouseSimulator
{
    #region Windows API Code
    [DllImport("user32.dll")]
    static extern int ShowCursor(bool show);
    [DllImport("user32.dll")]
    static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
    const int MOUSEEVENTF_MOVE = 0x1;
    const int MOUSEEVENTF_LEFTDOWN = 0x2;
    const int MOUSEEVENTF_LEFTUP = 0x4;
    const int MOUSEEVENTF_RIGHTDOWN = 0x8;
    const int MOUSEEVENTF_RIGHTUP = 0x10;
    const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
    const int MOUSEEVENTF_MIDDLEUP = 0x40;
    const int MOUSEEVENTF_WHEEL = 0x800;
    const int MOUSEEVENTF_ABSOLUTE = 0x8000;
    #endregion
    #region Properties
    /// <summary>
    /// Gets or sets a structure that represents both X and Y mouse coordinates
    /// </summary>
    public static MousePoint Position
    {
        get
        {
            return new MousePoint(Cursor.Position);
        }
        set
        {
            Cursor.Position = value;
        }
    }
    /// <summary>
    /// Gets or sets only the mouse's x coordinate
    /// </summary>
    public static int X
    {
        get
        {
            return Cursor.Position.X;
        }
        set
        {
            Cursor.Position = new Point(value, Y);
        }
    }
    /// <summary>
    /// Gets or sets only the mouse's y coordinate
    /// </summary>
    public static int Y
    {
        get
        {
            return Cursor.Position.Y;
        }
        set
        {
            Cursor.Position = new Point(X, value);
        }
    }
    #endregion
    #region Methods
    /// <summary>
    /// Press a mouse button down
    /// </summary>
    /// <param name="button"></param>
    public static void MouseDown(MouseButton button)
    {
        mouse_event(((int)button), 0, 0, 0, 0);
    }
    public static void MouseDown(MouseButtons button)
    {
        switch (button)
        {
            case MouseButtons.Left:
                MouseDown(MouseButton.Left);
                break;
            case MouseButtons.Middle:
                MouseDown(MouseButton.Middle);
                break;
            case MouseButtons.Right:
                MouseDown(MouseButton.Right);
                break;
        }
    }
    /// <summary>
    /// Let a mouse button up
    /// </summary>
    /// <param name="button"></param>
    public static void MouseUp(MouseButton button)
    {
        mouse_event(((int)button) * 2, 0, 0, 0, 0);
    }
    public static void MouseUp(MouseButtons button)
    {
        switch (button)
        {
            case MouseButtons.Left:
                MouseUp(MouseButton.Left);
                break;
            case MouseButtons.Middle:
                MouseUp(MouseButton.Middle);
                break;
            case MouseButtons.Right:
                MouseUp(MouseButton.Right);
                break;
        }
    }
    /// <summary>
    /// Click a mouse button (down then up)
    /// </summary>
    /// <param name="button"></param>
    public static void Click(MouseButton button)
    {
        MouseDown(button);
        MouseUp(button);
    }
    public static void Click(MouseButtons button)
    {
        switch (button)
        {
            case MouseButtons.Left:
                Click(MouseButton.Left);
                break;
            case MouseButtons.Middle:
                Click(MouseButton.Middle);
                break;
            case MouseButtons.Right:
                Click(MouseButton.Right);
                break;
        }
    }
    /// <summary>
    /// Double click a mouse button (down then up twice)
    /// </summary>
    /// <param name="button"></param>
    public static void DoubleClick(MouseButton button)
    {
        Click(button);
        Click(button);
    }
    public static void DoubleClick(MouseButtons button)
    {
        switch (button)
        {
            case MouseButtons.Left:
                DoubleClick(MouseButton.Left);
                break;
            case MouseButtons.Middle:
                DoubleClick(MouseButton.Middle);
                break;
            case MouseButtons.Right:
                DoubleClick(MouseButton.Right);
                break;
        }
    }
    /// <summary>
    /// Show a hidden current on currently application
    /// </summary>
    public static void Show()
    {
        ShowCursor(true);
    }
    /// <summary>
    /// Hide mouse cursor only on current application's forms
    /// </summary>
    public static void Hide()
    {
        ShowCursor(false);
    }
    #endregion
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175

StandardShortcut.cs

 /// <summary>
 /// Standard Keyboard Shortcuts used by most applications
 /// </summary>
 public enum StandardShortcut
 {
     Copy,
     Cut,
     Paste,
     SelectAll,
     Save,
     Open,
     New,
     Close,
     Print
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Utility.cs

 public class Utility
 {
     [DllImport("user32.dll")]
     public static extern bool SetForegroundWindow(IntPtr hWnd);

     [DllImport("user32.dll")]
     public static extern int ShowWindowAsync(IntPtr hwnd, int nCmdShow);
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

启动参数配置

AppConfig.cs

 public class AppConfig
 {
     /// <summary>
     /// 监控的应用程序
     /// </summary>
     public string Application { set; get; }
     /// <summary>
     /// 启动参数
     /// </summary>
     public string StartParam { set; get; } = "http://202.115.142.156/";
     /// <summary>
     /// 空闲时间
     /// </summary>        
     public int IdleTime { set; get; } = 120;
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在程序启动的时候,加载配置

/// <summary>
/// 加载配置文件
/// </summary>
private void LoadConfigFile()
{
    //确定路径存在
    if (String.IsNullOrEmpty(AppDataPath))
    {
        Assembly assembly = Assembly.GetExecutingAssembly();
        AssemblyProductAttribute product = (AssemblyProductAttribute)assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];
        AppDataPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                "Zmrbak", product.Product);

        //如果路径不存在,则创建路径,获取AccessToken
        if (Directory.Exists(AppDataPath) == false)
        {
            Directory.CreateDirectory(AppDataPath);
        }

        //配置文件
        appConfigJsonFile = Path.Combine(AppDataPath, "AppConfig.Json");
    }


    //读取配置
    if (File.Exists(appConfigJsonFile))
    {
        //从配置文件中获取参数
        appConfig = new JavaScriptSerializer().Deserialize<AppConfig>(File.ReadAllText(appConfigJsonFile));
    }
    else
    {
        //选择默认浏览器
        RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
        string appRegistryKeyValue = registryKey.GetValue("").ToString().Trim();

        //第一个双引号,到第二个双引号之间的字符串
        int startIndex = appRegistryKeyValue.IndexOf("\"");
        string appString = appRegistryKeyValue.Substring(startIndex + 1, appRegistryKeyValue.IndexOf("\"", startIndex + 2) - 1);

        if (File.Exists(appString))
        {
            appConfig = new AppConfig
            {
                Application = appString
            };
        }
        else
        {
            //默认配置文件
            appConfig = new AppConfig
            {
                Application = @"请替换为要监视的应用程序的绝对路径",
            };
        }

        //将默认配置写入磁盘
        File.WriteAllText(appConfigJsonFile, new JavaScriptSerializer().Serialize(appConfig));

        //调用记事本,打开配置文件,准备修改
        Process.Start("notepad.exe", appConfigJsonFile);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

启动键盘鼠标hook

//启动鼠标HooK
mouseHook = new MouseHook();
mouseHook.MouseMove += new MouseEventHandler(KeyboardHook_Action);
mouseHook.MouseDown += new MouseEventHandler(KeyboardHook_Action);
mouseHook.MouseUp += new MouseEventHandler(KeyboardHook_Action);
mouseHook.MouseWheel += new MouseEventHandler(KeyboardHook_Action);
mouseHook.Start();

//启动键盘HooK
keyboardHook = new KeyboardHook();
keyboardHook.KeyDown += new KeyEventHandler(KeyboardHook_Action);
keyboardHook.KeyUp += new KeyEventHandler(KeyboardHook_Action);
keyboardHook.KeyPress += new KeyPressEventHandler(KeyboardHook_Action);
keyboardHook.Start();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

判断浏览器进程是否已经启动

/// <summary>
/// 指定进程是否已经启动
/// </summary>
/// <returns></returns>
private bool IsProcessRunning()
{
    var processes = Process.GetProcesses();
    foreach (var item in processes)
    {
        try
        {
            if (item.MainModule.FileName == appConfig.Application)
            {
                if (item.MainWindowHandle != IntPtr.Zero)
                {
                    return true;
                }
            }
        }
        catch { }
    }
    return false;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

结束进程

/// <summary>
/// 结束进程
/// </summary>
private void ShutdownProcess()
{
    if (processName != null)
    {
        var processes1 = Process.GetProcessesByName(processName);
        foreach (var item in processes1)
        {
            if (item.MainWindowHandle != IntPtr.Zero)
            {
                try
                {
                    item.CloseMainWindow();
                }
                catch { };
            }
        }
    }

    //二次清理
    var processes = Process.GetProcesses();
    foreach (var item in processes)
    {
        try
        {
            if (item.MainModule.FileName == appConfig.Application)
            {
                if (item.MainWindowHandle == IntPtr.Zero)
                {
                    try
                    {
                        item.Kill();
                    }
                    catch { };
                }
            }
        }
        catch { }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

启动进程

/// <summary>
/// 启动进程
/// </summary>
private void StartProcess()
{
    Process process = new Process();
    process.StartInfo.FileName = appConfig.Application;
    process.StartInfo.Arguments = appConfig.StartParam;
    process.Start();

    if (processName == null)
    {
        processName = process.ProcessName;
    }

    int breakLoop = 0;
    while (true)
    {
        if (breakLoop++ > 10) break;
        Process[] processes = Process.GetProcessesByName(processName);
        foreach (var item in processes)
        {
            if (item.MainWindowHandle != IntPtr.Zero)
            {
                //设置前置
                Utility.SetForegroundWindow(item.MainWindowHandle);
                //设置最大化
                Utility.ShowWindowAsync(item.MainWindowHandle, 3);
                return;
            }
        }
        Thread.Sleep(200);
    };
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

定时器设置

设置一个定时器,定时执行检查
如果浏览器未启动,则启动浏览器
如果浏览器已经启动,则检查这段时间是否有鼠标键盘操作,若没有操作,则判断是否超时,若超时,则关闭浏览器。
如果从上次自动重启后,一直未检测到鼠标键盘活动,则不需要再重启浏览器。

键盘鼠标事件

只做一件事情,重置上次操作的时间。让定时器从此刻开始计算超时时间。

遇到的坑

如果对浏览器直接杀进程,那么下次浏览器启动起来之后,会有“上次未正常关闭”的错误提示信息。使用CloseMainWindow()方法关闭浏览器,再重启浏览器则不会出现该错误提示信息。

FireFox浏览器启动起来之后,发现会有多个进程(我的计算机上8进程),但只有一个进程有窗口,其他没窗口。因此,使用MainWindowHandle != IntPtr.Zero来找到该窗口,然后关掉它。对于其他没窗口的进程,直接杀掉。在啥的时候,会出现异常,程序直接吞掉异常即可。

启动进程,需要时间。
启动之后,再找到有窗口的进程,将其设置为前置,最大化。这里需要使用Windows Api。不过,时灵时不灵,暂未找到好办法。

将程序设置为管理员身份运行,再创建快捷方式,将快捷方式放到“启动”里面就可以了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/309757
推荐阅读
相关标签
  

闽ICP备14008679号