diff --git a/ClickableTransparentOverlay/ClickableTransparentOverlay.csproj b/ClickableTransparentOverlay/ClickableTransparentOverlay.csproj index ef2ab7a..5898980 100644 --- a/ClickableTransparentOverlay/ClickableTransparentOverlay.csproj +++ b/ClickableTransparentOverlay/ClickableTransparentOverlay.csproj @@ -53,6 +53,8 @@ prompt MinimumRecommendedRules.ruleset true + + @@ -258,7 +260,7 @@ - + diff --git a/ClickableTransparentOverlay/HookController.cs b/ClickableTransparentOverlay/HookController.cs index bb54d7c..9481726 100644 --- a/ClickableTransparentOverlay/HookController.cs +++ b/ClickableTransparentOverlay/HookController.cs @@ -166,15 +166,21 @@ { return; } - // TODO: + + var io = ImGui.GetIO(); + + if (io.KeyAlt) // Ignoring ALT key so we can do ALT+TAB or ALT+F4 etc. Not sure if ImGUI uses ALT key in anyway. + return; + + if (io.WantTextInput || io.WantCaptureKeyboard) + { + io.AddInputCharacter(e.KeyChar); + e.Handled = true; + } } private void _hook_KeyUp(object sender, KeyEventArgs e) { - if (!Enable) - { - return; - } // TODO: } diff --git a/ClickableTransparentOverlay/ImGuiController.cs b/ClickableTransparentOverlay/ImGuiController.cs index 2bbc090..681224b 100644 --- a/ClickableTransparentOverlay/ImGuiController.cs +++ b/ClickableTransparentOverlay/ImGuiController.cs @@ -13,7 +13,7 @@ /// A modified version of ImGui.NET.SampleProgram's ImGuiController. /// Manages input for ImGui and handles rendering ImGui's DrawLists with Veldrid. /// - public class ImGuiController : IDisposable + public sealed class ImGuiController : IDisposable { private GraphicsDevice _gd; private bool _frameBegun; diff --git a/ClickableTransparentOverlay/WinApi.cs b/ClickableTransparentOverlay/NativeMethods.cs similarity index 53% rename from ClickableTransparentOverlay/WinApi.cs rename to ClickableTransparentOverlay/NativeMethods.cs index 517a701..f82697b 100644 --- a/ClickableTransparentOverlay/WinApi.cs +++ b/ClickableTransparentOverlay/NativeMethods.cs @@ -3,27 +3,35 @@ using System; using System.Runtime.InteropServices; - public static class WinApi + public static class NativeMethods { private const int GWL_EXSTYLE = -20; private const int WS_EX_LAYERED = 0x80000; private const int WS_EX_TRANSPARENT = 0x20; - private const int LWA_ALPHA = 0x02; - private const int LWA_COLORKEY = 0x01; private const int SW_HIDE = 0x00; private const int SW_SHOW = 0x05; public static void EnableTransparent(IntPtr handle, System.Drawing.Rectangle size) { - int windowLong = GetWindowLong(handle, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT; - SetWindowLong(handle, GWL_EXSTYLE, new IntPtr(windowLong)); - //SetLayeredWindowAttributes(handle, 0, 255, LWA_ALPHA /*| LWA_COLORKEY*/ ); + IntPtr windowLong = GetWindowLongPtr(handle, GWL_EXSTYLE); + windowLong = new IntPtr(windowLong.ToInt64() | WS_EX_LAYERED | WS_EX_TRANSPARENT); + SetWindowLongPtr(handle, GWL_EXSTYLE, windowLong); Margins margins = Margins.FromRectangle(size); DwmExtendFrameIntoClientArea(handle, ref margins); } - #region Structures + public static void HideConsoleWindow() + { + var handle = GetConsoleWindow(); + ShowWindow(handle, SW_HIDE); + } + + public static void ShowConsoleWindow() + { + var handle = GetConsoleWindow(); + ShowWindow(handle, SW_SHOW); + } [StructLayout(LayoutKind.Sequential)] private struct Margins @@ -43,47 +51,19 @@ } } - [StructLayout(LayoutKind.Sequential)] - private struct Rect - { - private readonly int left, top, right, bottom; - - public System.Drawing.Rectangle ToRectangle(System.Drawing.Point point) - { - return new System.Drawing.Rectangle(point.X, point.Y, right - left, bottom - top); - } - } - - #endregion Structures - - public static void HideConsoleWindow() - { - var handle = GetConsoleWindow(); - ShowWindow(handle, SW_HIDE); - } - - public static void ShowConsoleWindow() - { - var handle = GetConsoleWindow(); - ShowWindow(handle, SW_SHOW); - } - [DllImport("dwmapi.dll")] - private static extern IntPtr DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset); - - [DllImport("user32.dll", SetLastError = true)] - private static extern int GetWindowLong(IntPtr hWnd, int nIndex); + private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset); - [DllImport("user32.dll", SetLastError = true)] - private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); + [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")] + private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); - [DllImport("user32.dll", SetLastError = true)] - private static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, uint dwFlags); + [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] + private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow(); - [DllImport("user32.dll")] + [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); } -} +} \ No newline at end of file diff --git a/ClickableTransparentOverlay/Overlay.cs b/ClickableTransparentOverlay/Overlay.cs index 4e952d3..1595eeb 100644 --- a/ClickableTransparentOverlay/Overlay.cs +++ b/ClickableTransparentOverlay/Overlay.cs @@ -24,6 +24,7 @@ private static Vector2 _future_size; private static int _fps; private static bool _is_visible; + private static bool _is_closed; private static bool _require_resize; private static bool _start_resizing; private static object _resize_thread_lock; @@ -33,6 +34,7 @@ _clearColor = new Vector4(0.00f, 0.00f, 0.00f, 0.00f); _fps = fps; _is_visible = true; + _is_closed = false; // Stuff related to (thread safe) resizing of SDL2Window _require_resize = false; _start_resizing = false; @@ -43,7 +45,7 @@ _window = new Sdl2Window("Overlay", x, x, width, height, SDL_WindowFlags.Borderless | SDL_WindowFlags.AlwaysOnTop | SDL_WindowFlags.SkipTaskbar, true); // TODO: Create a new branch for Non-Veldrid dependent version. Ideally, we can directly use SDL2Window. _gd = VeldridStartup.CreateGraphicsDevice(_window, new GraphicsDeviceOptions(true, null, true), GraphicsBackend.Direct3D11); - WinApi.EnableTransparent(_window.Handle, new System.Drawing.Rectangle(_window.X , _window.Y, _window.Width, _window.Height)); + NativeMethods.EnableTransparent(_window.Handle, new System.Drawing.Rectangle(_window.X , _window.Y, _window.Width, _window.Height)); _window.Resized += () => { _gd.MainSwapchain.Resize((uint)_window.Width, (uint)_window.Height); @@ -54,6 +56,10 @@ _start_resizing = false; } }; + _window.Closed += () => + { + _is_closed = true; + }; _cl = _gd.ResourceFactory.CreateCommandList(); _im_controller = new ImGuiController(_gd, _gd.MainSwapchain.Framebuffer.OutputDescription, _window.Width, _window.Height, _fps); @@ -65,7 +71,7 @@ { _ui_thread.Start(); _hook_controller.EnableHooks(); - WinApi.HideConsoleWindow(); + NativeMethods.HideConsoleWindow(); Application.Run(new ApplicationContext()); } @@ -73,13 +79,20 @@ { _is_visible = false; _window.Close(); + while (!_is_closed) + { + Thread.Sleep(10); + } + _ui_thread.Join(); _gd.WaitForIdle(); _im_controller.Dispose(); _cl.Dispose(); _gd.Dispose(); _hook_controller.Dispose(); - WinApi.ShowConsoleWindow(); + NativeMethods.ShowConsoleWindow(); + _resize_thread_lock = null; + SubmitUI = null; Console.WriteLine("All Overlay resources are cleared."); } @@ -91,7 +104,7 @@ _future_size.Y = height; // TODO: move following two lines to _window.Moved _hook_controller.UpdateWindowPosition(x, y); - WinApi.EnableTransparent(_window.Handle, new System.Drawing.Rectangle(x, y, width, height)); + NativeMethods.EnableTransparent(_window.Handle, new System.Drawing.Rectangle(x, y, width, height)); _require_resize = true; } diff --git a/DriverProgram/DriverProgram.csproj b/DriverProgram/DriverProgram.csproj index 93ccb88..24bb5e0 100644 --- a/DriverProgram/DriverProgram.csproj +++ b/DriverProgram/DriverProgram.csproj @@ -53,6 +53,8 @@ MinimumRecommendedRules.ruleset true true + + diff --git a/DriverProgram/Program.cs b/DriverProgram/Program.cs index 37d98b6..f367618 100644 --- a/DriverProgram/Program.cs +++ b/DriverProgram/Program.cs @@ -7,14 +7,14 @@ class Program { private static Overlay demo; - private static int Width; - private static int Height; - private static int Fps; + private static int Width = 2560; + private static int Height = 1440; + private static int Fps = 144; static void Main(string[] args) { - Width = int.Parse(System.IO.File.ReadAllText("config/width.txt")); - Height = int.Parse(System.IO.File.ReadAllText("config/height.txt")); - Fps = int.Parse(System.IO.File.ReadAllText("config/fps.txt")); + //Width = int.Parse(System.IO.File.ReadAllText("config/width.txt")); + //Height = int.Parse(System.IO.File.ReadAllText("config/height.txt")); + //Fps = int.Parse(System.IO.File.ReadAllText("config/fps.txt")); var EndDemo = new Thread(DistroyDemo); EndDemo.Start(); StartDemo(); @@ -30,7 +30,7 @@ public static void DistroyDemo() { Thread.Sleep(10000); - demo.ResizeWindow(100, 100, 1024, 1024); + demo.ResizeWindow(0, 0, 2560, 1440); Thread.Sleep(10000); demo.HideWindow(); Thread.Sleep(10000); @@ -39,4 +39,4 @@ demo.Dispose(); } } -} +} \ No newline at end of file