From dcb9c65267c05df2a12a386695a3e5b6321450e1 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Thu, 23 May 2019 08:31:43 +0200 Subject: [PATCH] FrmCover: Uncover with keypress. FrmCover: Copy active window title, before covering. --- VAR.Toolbox/Code/Windows/User32.cs | 27 +++++++++++++++++++++++++++ VAR.Toolbox/UI/FrmCover.cs | 20 ++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/VAR.Toolbox/Code/Windows/User32.cs b/VAR.Toolbox/Code/Windows/User32.cs index f4ec416..70b1b04 100644 --- a/VAR.Toolbox/Code/Windows/User32.cs +++ b/VAR.Toolbox/Code/Windows/User32.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using System.Text; namespace VAR.Toolbox.Code.Windows { @@ -154,11 +155,37 @@ namespace VAR.Toolbox.Code.Windows [DllImport("user32.dll")] public static extern IntPtr GetDesktopWindow(); + [DllImport("user32.dll")] public static extern IntPtr GetWindowDC(IntPtr hWnd); + [DllImport("user32.dll")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); + [DllImport("user32.dll")] public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll")] + public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); + + public static string GetActiveWindowTitle() + { + const int nChars = 256; + StringBuilder Buff = new StringBuilder(nChars); + IntPtr handle = GetForegroundWindow(); + + if (GetWindowText(handle, Buff, nChars) > 0) + { + return Buff.ToString(); + } + return null; + } } } \ No newline at end of file diff --git a/VAR.Toolbox/UI/FrmCover.cs b/VAR.Toolbox/UI/FrmCover.cs index fede8be..352a01e 100644 --- a/VAR.Toolbox/UI/FrmCover.cs +++ b/VAR.Toolbox/UI/FrmCover.cs @@ -2,6 +2,7 @@ using System.Drawing; using System.Windows.Forms; using VAR.Toolbox.Code; +using VAR.Toolbox.Code.Windows; namespace VAR.Toolbox.UI { @@ -12,8 +13,8 @@ namespace VAR.Toolbox.UI private Random rnd = new Random(); private Timer _timer = new Timer(); - private UInt32 _mouseX = 0; - private UInt32 _mouseY = 0; + private uint _mouseX = 0; + private uint _mouseY = 0; #endregion Declarations @@ -23,12 +24,15 @@ namespace VAR.Toolbox.UI { Mouse.GetPosition(out _mouseX, out _mouseY); + Text = User32.GetActiveWindowTitle(); + TopMost = true; FormBorderStyle = FormBorderStyle.None; BackColor = Color.Black; Load += FrmCover_Load; Click += FrmCover_Click; + KeyPress += FrmCover_KeyPress; _timer.Interval = 1000; _timer.Enabled = true; @@ -48,6 +52,7 @@ namespace VAR.Toolbox.UI Height = r.Height; Cursor.Hide(); _timer.Start(); + User32.SetForegroundWindow(Handle); } #endregion Form life cycle @@ -64,8 +69,19 @@ namespace VAR.Toolbox.UI Close(); } + private void FrmCover_KeyPress(object sender, KeyPressEventArgs e) + { + Cursor.Show(); + _timer.Stop(); + _timer.Enabled = false; + Mouse.SetPosition(_mouseX, _mouseY); + + Close(); + } + private void timer_Tick(object sender, EventArgs e) { + User32.SetForegroundWindow(Handle); try { Mouse.Move(