diff --git a/VAR.Toolbox/Code/Mouse.cs b/VAR.Toolbox/Code/Mouse.cs new file mode 100644 index 0000000..da03ae8 --- /dev/null +++ b/VAR.Toolbox/Code/Mouse.cs @@ -0,0 +1,70 @@ +using System; +using System.Runtime.InteropServices; + +namespace VAR.Toolbox.Code +{ + public class Mouse + { + public static void Move(int dx, int dy) + { + User32.INPUT input = new User32.INPUT(); + input.Type = User32.INPUT_MOUSE; + input.Data.Mouse.X = dx; + input.Data.Mouse.Y = dy; + input.Data.Mouse.Flags = User32.MOUSEEVENTF_MOVE; + User32.INPUT[] inputs = new User32.INPUT[] { input }; + if (User32.SendInput(1, inputs, Marshal.SizeOf(typeof(User32.INPUT))) == 0) + throw new Exception(); + } + + public enum MouseButtons + { + Left, + Middle, + Right + } + + public static void SetButton(MouseButtons button, bool down) + { + User32.INPUT input = new User32.INPUT(); + input.Type = User32.INPUT_MOUSE; + input.Data.Mouse.X = 0; + input.Data.Mouse.Y = 0; + if (button == MouseButtons.Left) + { + input.Data.Mouse.Flags = down ? User32.MOUSEEVENTF_LEFTDOWN : User32.MOUSEEVENTF_LEFTUP; + } + if (button == MouseButtons.Middle) + { + input.Data.Mouse.Flags = down ? User32.MOUSEEVENTF_MIDDLEDOWN : User32.MOUSEEVENTF_MIDDLEUP; + } + if (button == MouseButtons.Right) + { + input.Data.Mouse.Flags = down ? User32.MOUSEEVENTF_RIGHTDOWN : User32.MOUSEEVENTF_RIGHTUP; + } + User32.INPUT[] inputs = new User32.INPUT[] { input }; + if (User32.SendInput(1, inputs, Marshal.SizeOf(typeof(User32.INPUT))) == 0) + throw new Exception(); + } + + public static void Click(MouseButtons button) + { + SetButton(button, true); + System.Threading.Thread.Sleep(500); + SetButton(button, false); + } + + public static void GetPosition(out UInt32 x, out UInt32 y) + { + User32.POINT lpPoint; + User32.GetCursorPos(out lpPoint); + x = lpPoint.X; + y = lpPoint.Y; + } + + public static void SetPosition(UInt32 x, UInt32 y) + { + User32.SetCursorPos(x, y); + } + } +} \ No newline at end of file diff --git a/VAR.Toolbox/Code/User32.cs b/VAR.Toolbox/Code/User32.cs new file mode 100644 index 0000000..29b04f3 --- /dev/null +++ b/VAR.Toolbox/Code/User32.cs @@ -0,0 +1,137 @@ +using System; +using System.Runtime.InteropServices; + +namespace VAR.Toolbox.Code +{ + public class User32 + { + [StructLayout(LayoutKind.Sequential)] + public struct INPUT + { + public uint Type; + public MOUSEKEYBDHARDWAREINPUT Data; + } + + public const int INPUT_MOUSE = 0; + public const int INPUT_KEYBOARD = 1; + public const int INPUT_HARDWARE = 2; + + /// + /// http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/f0e82d6e-4999-4d22-b3d3-32b25f61fb2a + /// + [StructLayout(LayoutKind.Explicit)] + public struct MOUSEKEYBDHARDWAREINPUT + { + [FieldOffset(0)] + public HARDWAREINPUT Hardware; + + [FieldOffset(0)] + public KEYBDINPUT Keyboard; + + [FieldOffset(0)] + public MOUSEINPUT Mouse; + } + + /// + /// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx + /// + [StructLayout(LayoutKind.Sequential)] + public struct HARDWAREINPUT + { + public uint Msg; + public ushort ParamL; + public ushort ParamH; + } + + /// + /// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx + /// + [StructLayout(LayoutKind.Sequential)] + public struct KEYBDINPUT + { + public ushort Vk; + public ushort Scan; + public uint Flags; + public uint Time; + public IntPtr ExtraInfo; + } + + /// + /// http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/2abc6be8-c593-4686-93d2-89785232dacd + /// https://msdn.microsoft.com/es-es/library/windows/desktop/ms646273%28v=vs.85%29.aspx + /// + [StructLayout(LayoutKind.Sequential)] + public struct MOUSEINPUT + { + public int X; + public int Y; + public uint MouseData; + public uint Flags; + public uint Time; + public IntPtr ExtraInfo; + } + + public const int MOUSEEVENTD_XBUTTON1 = 0x0001; + public const int MOUSEEVENTD_XBUTTON2 = 0x0002; + + public const uint MOUSEEVENTF_ABSOLUTE = 0x8000; + public const uint MOUSEEVENTF_HWHEEL = 0x01000; + public const uint MOUSEEVENTF_MOVE = 0x0001; + public const uint MOUSEEVENTF_MOVE_NOCOALESCE = 0x2000; + public const uint MOUSEEVENTF_LEFTDOWN = 0x0002; + public const uint MOUSEEVENTF_LEFTUP = 0x0004; + public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008; + public const uint MOUSEEVENTF_RIGHTUP = 0x0010; + public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020; + public const uint MOUSEEVENTF_MIDDLEUP = 0x0040; + public const uint MOUSEEVENTF_VIRTUALDESK = 0x4000; + public const uint MOUSEEVENTF_WHEEL = 0x0800; + public const uint MOUSEEVENTF_XDOWN = 0x0080; + public const uint MOUSEEVENTF_XUP = 0x0100; + + [DllImport("User32.dll")] + public static extern int SendInput(int nInputs, INPUT[] pInputs, int cbSize); + + // + /// Struct representing a point. + /// + [StructLayout(LayoutKind.Sequential)] + public struct POINT + { + public UInt32 X; + public UInt32 Y; + } + + /// + /// Retrieves the cursor's position, in screen coordinates. + /// + /// See MSDN documentation for further information. + [DllImport("user32.dll")] + public static extern bool GetCursorPos(out POINT lpPoint); + + [DllImport("User32.dll")] + public static extern Boolean SetCursorPos(UInt32 X, UInt32 Y); + + [StructLayout(LayoutKind.Sequential)] + public struct LASTINPUTINFO + { + public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO)); + + [MarshalAs(UnmanagedType.U4)] + public UInt32 cbSize; + + [MarshalAs(UnmanagedType.U4)] + public UInt32 dwTime; + } + + [DllImport("user32.dll")] + public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + [DllImport("user32.dll")] + public static extern short GetAsyncKeyState(int vKey); + + public const int VK_MBUTTON = 0x04; + public const int VK_LBUTTON = 0x01; + public const int VK_RBUTTON = 0x02; + } +} \ No newline at end of file diff --git a/VAR.Toolbox/Code/Win32API.cs b/VAR.Toolbox/Code/Win32API.cs new file mode 100644 index 0000000..14f6a95 --- /dev/null +++ b/VAR.Toolbox/Code/Win32API.cs @@ -0,0 +1,26 @@ +using System; +using System.Runtime.InteropServices; + +namespace VAR.Toolbox.Code +{ + public class Win32API + { + [DllImport("PowrProf.dll")] + public static extern Boolean SetSuspendState(Boolean Hibernate, Boolean ForceCritical, Boolean DisableWakeEvent); + + public static uint GetLastInputTime() + { + uint idleTime = 0; + User32.LASTINPUTINFO lastInputInfo = new User32.LASTINPUTINFO(); + lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo); + lastInputInfo.dwTime = 0; + uint envTicks = (uint)Environment.TickCount; + if (User32.GetLastInputInfo(ref lastInputInfo)) + { + uint lastInputTick = lastInputInfo.dwTime; + idleTime = envTicks - lastInputTick; + } + return ((idleTime > 0) ? (idleTime / 1000) : 0); + } + } +} \ No newline at end of file diff --git a/VAR.Toolbox/UI/FrmCover.cs b/VAR.Toolbox/UI/FrmCover.cs new file mode 100644 index 0000000..9eb61fb --- /dev/null +++ b/VAR.Toolbox/UI/FrmCover.cs @@ -0,0 +1,79 @@ +using System; +using System.Drawing; +using System.Windows.Forms; +using VAR.Toolbox.Code; + +namespace VAR.Toolbox.UI +{ + internal class FrmCover : Form + { + #region Declarations + + private Random rnd = new Random(); + private Timer _timer = new Timer(); + + private UInt32 _mouseX = 0; + private UInt32 _mouseY = 0; + + #endregion Declarations + + #region Form life cycle + + public FrmCover() + { + Mouse.GetPosition(out _mouseX, out _mouseY); + + TopMost = true; + FormBorderStyle = FormBorderStyle.None; + BackColor = Color.Black; + + Load += FrmCover_Load; + Click += FrmCover_Click; + + _timer.Interval = 1000; + _timer.Enabled = true; + _timer.Tick += timer_Tick; + } + + private void FrmCover_Load(object sender, EventArgs e) + { + Rectangle r = new Rectangle(); + foreach (Screen s in Screen.AllScreens) + { + r = Rectangle.Union(r, s.Bounds); + } + Top = r.Top; + Left = r.Left; + Width = r.Width; + Height = r.Height; + Cursor.Hide(); + _timer.Start(); + } + + #endregion Form life cycle + + #region UI events + + private void FrmCover_Click(object sender, EventArgs e) + { + Cursor.Show(); + _timer.Stop(); + _timer.Enabled = false; + Mouse.SetPosition(_mouseX, _mouseY); + + Close(); + } + + private void timer_Tick(object sender, EventArgs e) + { + Mouse.Move( + (rnd.Next() % 11) - 5, + (rnd.Next() % 11) - 5); + + _timer.Stop(); + _timer.Start(); + } + + #endregion UI events + } +} \ No newline at end of file diff --git a/VAR.Toolbox/UI/FrmToolbox.Designer.cs b/VAR.Toolbox/UI/FrmToolbox.Designer.cs index 1dae48c..7260df0 100644 --- a/VAR.Toolbox/UI/FrmToolbox.Designer.cs +++ b/VAR.Toolbox/UI/FrmToolbox.Designer.cs @@ -31,13 +31,17 @@ this.btnBase64 = new System.Windows.Forms.Button(); this.btnProxyCmd = new System.Windows.Forms.Button(); this.btnWebcam = new System.Windows.Forms.Button(); + this.pnlCover1 = new VAR.Toolbox.UI.PnlCover(); this.SuspendLayout(); // // btnBase64 // - this.btnBase64.Location = new System.Drawing.Point(12, 12); + this.btnBase64.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.btnBase64.Location = new System.Drawing.Point(18, 18); + this.btnBase64.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.btnBase64.Name = "btnBase64"; - this.btnBase64.Size = new System.Drawing.Size(209, 34); + this.btnBase64.Size = new System.Drawing.Size(293, 52); this.btnBase64.TabIndex = 0; this.btnBase64.Text = "Base64"; this.btnBase64.UseVisualStyleBackColor = true; @@ -45,9 +49,12 @@ // // btnProxyCmd // - this.btnProxyCmd.Location = new System.Drawing.Point(12, 52); + this.btnProxyCmd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.btnProxyCmd.Location = new System.Drawing.Point(18, 80); + this.btnProxyCmd.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.btnProxyCmd.Name = "btnProxyCmd"; - this.btnProxyCmd.Size = new System.Drawing.Size(209, 36); + this.btnProxyCmd.Size = new System.Drawing.Size(293, 55); this.btnProxyCmd.TabIndex = 1; this.btnProxyCmd.Text = "ProxyCmd"; this.btnProxyCmd.UseVisualStyleBackColor = true; @@ -55,23 +62,37 @@ // // btnWebcam // - this.btnWebcam.Location = new System.Drawing.Point(12, 94); + this.btnWebcam.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.btnWebcam.Location = new System.Drawing.Point(18, 145); + this.btnWebcam.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.btnWebcam.Name = "btnWebcam"; - this.btnWebcam.Size = new System.Drawing.Size(209, 36); + this.btnWebcam.Size = new System.Drawing.Size(293, 55); this.btnWebcam.TabIndex = 2; this.btnWebcam.Text = "Webcam"; this.btnWebcam.UseVisualStyleBackColor = true; this.btnWebcam.Click += new System.EventHandler(this.btnWebcam_Click); // + // pnlCover1 + // + this.pnlCover1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.pnlCover1.Location = new System.Drawing.Point(12, 246); + this.pnlCover1.Name = "pnlCover1"; + this.pnlCover1.Size = new System.Drawing.Size(300, 126); + this.pnlCover1.TabIndex = 3; + // // FrmToolbox // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(233, 391); + this.ClientSize = new System.Drawing.Size(324, 384); + this.Controls.Add(this.pnlCover1); this.Controls.Add(this.btnWebcam); this.Controls.Add(this.btnProxyCmd); this.Controls.Add(this.btnBase64); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; + this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.MaximizeBox = false; this.Name = "FrmToolbox"; this.Text = "Toolbox"; @@ -86,6 +107,7 @@ private System.Windows.Forms.Button btnBase64; private System.Windows.Forms.Button btnProxyCmd; private System.Windows.Forms.Button btnWebcam; + private PnlCover pnlCover1; } } diff --git a/VAR.Toolbox/UI/FrmToolbox.cs b/VAR.Toolbox/UI/FrmToolbox.cs index 9d287bf..33dae1c 100644 --- a/VAR.Toolbox/UI/FrmToolbox.cs +++ b/VAR.Toolbox/UI/FrmToolbox.cs @@ -79,5 +79,6 @@ namespace VAR.Toolbox.UI } #endregion Window handling + } } diff --git a/VAR.Toolbox/UI/PnlCover.Designer.cs b/VAR.Toolbox/UI/PnlCover.Designer.cs new file mode 100644 index 0000000..445ae43 --- /dev/null +++ b/VAR.Toolbox/UI/PnlCover.Designer.cs @@ -0,0 +1,143 @@ +namespace VAR.Toolbox.UI +{ + partial class PnlCover + { + /// + /// Variable del diseñador necesaria. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Limpiar los recursos que se estén usando. + /// + /// true si los recursos administrados se deben desechar; false en caso contrario. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Código generado por el Diseñador de componentes + + /// + /// Método necesario para admitir el Diseñador. No se puede modificar + /// el contenido de este método con el editor de código. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.grpCover = new System.Windows.Forms.GroupBox(); + this.btnCover = new System.Windows.Forms.Button(); + this.numInactive = new System.Windows.Forms.NumericUpDown(); + this.lblInactive = new System.Windows.Forms.Label(); + this.chkAutoCover = new System.Windows.Forms.CheckBox(); + this.timTicker = new System.Windows.Forms.Timer(this.components); + this.grpCover.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numInactive)).BeginInit(); + this.SuspendLayout(); + // + // grpCover + // + this.grpCover.Controls.Add(this.btnCover); + this.grpCover.Controls.Add(this.numInactive); + this.grpCover.Controls.Add(this.lblInactive); + this.grpCover.Controls.Add(this.chkAutoCover); + this.grpCover.Dock = System.Windows.Forms.DockStyle.Fill; + this.grpCover.Location = new System.Drawing.Point(0, 0); + this.grpCover.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.grpCover.Name = "grpCover"; + this.grpCover.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.grpCover.Size = new System.Drawing.Size(262, 160); + this.grpCover.TabIndex = 14; + this.grpCover.TabStop = false; + this.grpCover.Text = "Cover"; + // + // btnCover + // + this.btnCover.Location = new System.Drawing.Point(8, 83); + this.btnCover.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.btnCover.Name = "btnCover"; + this.btnCover.Size = new System.Drawing.Size(181, 35); + this.btnCover.TabIndex = 7; + this.btnCover.Text = "Cover"; + this.btnCover.UseVisualStyleBackColor = true; + this.btnCover.Click += new System.EventHandler(this.btnCover_Click); + // + // numInactive + // + this.numInactive.Location = new System.Drawing.Point(131, 27); + this.numInactive.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.numInactive.Maximum = new decimal(new int[] { + 300, + 0, + 0, + 0}); + this.numInactive.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.numInactive.Name = "numInactive"; + this.numInactive.Size = new System.Drawing.Size(58, 26); + this.numInactive.TabIndex = 10; + this.numInactive.Value = new decimal(new int[] { + 180, + 0, + 0, + 0}); + // + // lblInactive + // + this.lblInactive.AutoSize = true; + this.lblInactive.Location = new System.Drawing.Point(8, 58); + this.lblInactive.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.lblInactive.Name = "lblInactive"; + this.lblInactive.Size = new System.Drawing.Size(79, 20); + this.lblInactive.TabIndex = 8; + this.lblInactive.Text = "lblInactive"; + // + // chkAutoCover + // + this.chkAutoCover.AutoSize = true; + this.chkAutoCover.Checked = true; + this.chkAutoCover.CheckState = System.Windows.Forms.CheckState.Checked; + this.chkAutoCover.Location = new System.Drawing.Point(8, 29); + this.chkAutoCover.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.chkAutoCover.Name = "chkAutoCover"; + this.chkAutoCover.Size = new System.Drawing.Size(110, 24); + this.chkAutoCover.TabIndex = 9; + this.chkAutoCover.Text = "AutoCover"; + this.chkAutoCover.UseVisualStyleBackColor = true; + // + // timTicker + // + this.timTicker.Enabled = true; + this.timTicker.Tick += new System.EventHandler(this.timTicker_Tick); + // + // PnlCover + // + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.grpCover); + this.Name = "PnlCover"; + this.Size = new System.Drawing.Size(262, 160); + this.grpCover.ResumeLayout(false); + this.grpCover.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numInactive)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.GroupBox grpCover; + private System.Windows.Forms.Button btnCover; + private System.Windows.Forms.NumericUpDown numInactive; + private System.Windows.Forms.Label lblInactive; + private System.Windows.Forms.CheckBox chkAutoCover; + private System.Windows.Forms.Timer timTicker; + } +} diff --git a/VAR.Toolbox/UI/PnlCover.cs b/VAR.Toolbox/UI/PnlCover.cs new file mode 100644 index 0000000..1d87ec6 --- /dev/null +++ b/VAR.Toolbox/UI/PnlCover.cs @@ -0,0 +1,64 @@ +using System; +using System.Windows.Forms; +using VAR.Toolbox.Code; + +namespace VAR.Toolbox.UI +{ + public partial class PnlCover : UserControl + { + public PnlCover() + { + InitializeComponent(); + } + + private void timTicker_Tick(object sender, EventArgs e) + { + if (DesignMode) { return; } + bool userInactive = false; + uint inactiveTime = Win32API.GetLastInputTime(); + + lblInactive.Text = String.Format("Inactive by {0} seconds", inactiveTime); + + if (chkAutoCover.Checked) + { + if (!userInactive && inactiveTime > numInactive.Value) + { + userInactive = true; + CoverScreen(); + } + if (inactiveTime < 1) + { + userInactive = false; + } + } + timTicker.Stop(); + timTicker.Start(); + } + + private void btnCover_Click(object sender, EventArgs e) + { + CoverScreen(); + } + + private FrmCover _frmCover = null; + + private void CoverScreen() + { + if (DesignMode) { return; } + if(_frmCover != null) + { + _frmCover.Show(); + return; + } + _frmCover = new FrmCover(); + _frmCover.FormClosing += (sender, e) => { _frmCover = null; }; + _frmCover.Show(); + + Form frmParent = Parent as Form; + if (frmParent != null) + { + frmParent.WindowState = FormWindowState.Minimized; + } + } + } +} diff --git a/VAR.Toolbox/UI/PnlCover.resx b/VAR.Toolbox/UI/PnlCover.resx new file mode 100644 index 0000000..4c972f7 --- /dev/null +++ b/VAR.Toolbox/UI/PnlCover.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/VAR.Toolbox/VAR.Toolbox.csproj b/VAR.Toolbox/VAR.Toolbox.csproj index 652b0a3..f34bc48 100644 --- a/VAR.Toolbox/VAR.Toolbox.csproj +++ b/VAR.Toolbox/VAR.Toolbox.csproj @@ -80,7 +80,10 @@ + + + Component @@ -90,6 +93,9 @@ FrmBase64.cs + + Form + Form @@ -115,6 +121,12 @@ FrmWebcam.cs + + UserControl + + + PnlCover.cs + @@ -129,6 +141,9 @@ FrmWebcam.cs + + PnlCover.cs +