/// <summary> /// Setups the instance. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> private void SetupInstance(int x = 0, int y = 0, int width = 800, int height = 600) { IsVisible = true; Topmost = BypassTopmost ? false : true; X = x; Y = y; Width = width; Height = height; WindowClassName = HelperMethods.GenerateRandomString(5, 11); string randomMenuName = HelperMethods.GenerateRandomString(5, 11); if (WindowTitle == null) { WindowTitle = HelperMethods.GenerateRandomString(5, 11); } // prepare method _windowProc = WindowProcedure; RuntimeHelpers.PrepareDelegate(_windowProc); _windowProcPtr = Marshal.GetFunctionPointerForDelegate(_windowProc); WNDCLASSEX wndClassEx = new WNDCLASSEX() { cbSize = WNDCLASSEX.Size(), style = 0, lpfnWndProc = _windowProcPtr, cbClsExtra = 0, cbWndExtra = 0, hInstance = IntPtr.Zero, hIcon = IntPtr.Zero, hCursor = IntPtr.Zero, hbrBackground = IntPtr.Zero, lpszMenuName = randomMenuName, lpszClassName = WindowClassName, hIconSm = IntPtr.Zero }; User32.RegisterClassEx(ref wndClassEx); uint exStyle; if (BypassTopmost) { exStyle = 0x20 | 0x80000 | 0x80 | 0x8000000; } else { exStyle = 0x8 | 0x20 | 0x80000 | 0x80 | 0x8000000; // WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED |WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE } WindowHandle = User32.CreateWindowEx( exStyle, // WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED |WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE WindowClassName, WindowTitle, 0x80000000 | 0x10000000, // WS_POPUP | WS_VISIBLE X, Y, Width, Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); User32.SetLayeredWindowAttributes(WindowHandle, 0, 255, /*0x1 |*/ 0x2); User32.UpdateWindow(WindowHandle); // If window is incompatible on some platforms use SetWindowLong to set the style again // and UpdateWindow If you have changed certain window data using SetWindowLong, you must // call SetWindowPos for the changes to take effect. Use the following combination for // uFlags: SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED. ExtendFrameIntoClientArea(); }
public OverlayWindow(int w, int h, bool vsync = false, IntPtr parent = default(IntPtr)) { X = 0; Y = 0; Width = w; Height = h; if (parent == default(IntPtr)) { Width = User32.GetSystemMetrics(0); Height = User32.GetSystemMetrics(1); } string className = GenerateRandomString(5, 11); string menuName = GenerateRandomString(5, 11); string windowName = GenerateRandomString(5, 11); WNDCLASSEX wndClassEx = new WNDCLASSEX() { cbSize = WNDCLASSEX.Size(), style = 0, lpfnWndProc = getWindowProcPointer(), cbClsExtra = 0, cbWndExtra = 0, hInstance = IntPtr.Zero, hIcon = IntPtr.Zero, hCursor = IntPtr.Zero, hbrBackground = IntPtr.Zero, lpszMenuName = menuName, lpszClassName = className, hIconSm = IntPtr.Zero }; if (User32.RegisterClassEx(ref wndClassEx) == 0) { throw new Exception("RegisterClassExA failed with error code: " + Marshal.GetLastWin32Error()); } Handle = User32.CreateWindowEx( 0x8 | 0x20 | 0x80000 | 0x80 | 0x8000000, // WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED |WS_EX_TOOLWINDOW className, windowName, 0x80000000 | 0x10000000, X, Y, Width, Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); if (Handle == IntPtr.Zero) { throw new Exception("CreateWindowEx failed with error code: " + Marshal.GetLastWin32Error()); } User32.SetLayeredWindowAttributes(Handle, 0, 255, 0x1 | 0x2); Graphics = new Direct2DRenderer(Handle, vsync); extendFrameIntoClientArea(); User32.UpdateWindow(Handle); IsVisible = true; Topmost = true; if (parent == default(IntPtr)) { return; } ParentWindowHandle = parent; new Task(_parentServiceThread).Start(); }
public OverlayWindow(bool vsync = false, IntPtr parent = default(IntPtr)) { this.X = 0; this.Y = 0; this.Width = 800; this.Height = 600; if (parent == default(IntPtr)) { this.Width = User32.GetSystemMetrics(0); this.Height = User32.GetSystemMetrics(1); } var className = GenerateRandomString(5, 11); var menuName = GenerateRandomString(5, 11); var windowName = GenerateRandomString(5, 11); var wndClassEx = new WNDCLASSEX { cbSize = WNDCLASSEX.Size(), style = 0, lpfnWndProc = this.GetWindowProcPointer(), cbClsExtra = 0, cbWndExtra = 0, hInstance = IntPtr.Zero, hIcon = IntPtr.Zero, hCursor = IntPtr.Zero, hbrBackground = IntPtr.Zero, lpszMenuName = menuName, lpszClassName = className, hIconSm = IntPtr.Zero }; if (User32.RegisterClassEx(ref wndClassEx) == 0) { throw new Exception("RegisterClassExA failed with error code: " + Marshal.GetLastWin32Error()); } this.Handle = User32.CreateWindowEx( 0x8 | 0x20 | 0x80000 | 0x80 | 0x8000000, // WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOOLWINDOW className, windowName, 0x80000000 | 0x10000000, this.X, this.Y, this.Width, this.Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (this.Handle == IntPtr.Zero) { throw new Exception("CreateWindowEx failed with error code: " + Marshal.GetLastWin32Error()); } User32.SetLayeredWindowAttributes(this.Handle, 0, 255, 0x2); // | 0x01 this.Graphics = new Direct2DRenderer(this.Handle, vsync); this.extendFrameIntoClientArea(); User32.UpdateWindow(this.Handle); this.IsVisible = true; this.Topmost = true; if (parent == default(IntPtr)) { return; } this.ParentWindowHandle = parent; }
/// <summary> /// Setups the instance. /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> private void SetupInstance(int x = 0, int y = 0, int width = 800, int height = 600) { IsVisible = true; Topmost = BypassTopmost ? false : true; X = x; Y = y; Width = width; Height = height; WindowClassName = HelperMethods.GenerateRandomString(5, 11); string randomMenuName = HelperMethods.GenerateRandomString(5, 11); if (WindowTitle == null) { WindowTitle = HelperMethods.GenerateRandomString(5, 11); } // prepare method _windowProc = WindowProcedure; RuntimeHelpers.PrepareDelegate(_windowProc); _windowProcPtr = Marshal.GetFunctionPointerForDelegate(_windowProc); WNDCLASSEX wndClassEx = new WNDCLASSEX() { cbSize = WNDCLASSEX.Size(), style = 0, lpfnWndProc = _windowProcPtr, cbClsExtra = 0, cbWndExtra = 0, hInstance = IntPtr.Zero, hIcon = IntPtr.Zero, hCursor = IntPtr.Zero, hbrBackground = IntPtr.Zero, lpszMenuName = randomMenuName, lpszClassName = WindowClassName, hIconSm = IntPtr.Zero }; if (User32.RegisterClassEx(ref wndClassEx) == 0) { WinApi.ThrowWin32Exception("Failed to register window class!"); } uint exStyle; if (BypassTopmost) { exStyle = (uint)(ExtendedWindowStyles.Transparent | ExtendedWindowStyles.Layered | ExtendedWindowStyles.NoActivate); } else { exStyle = (uint)(ExtendedWindowStyles.TopMost | ExtendedWindowStyles.Transparent | ExtendedWindowStyles.Layered | ExtendedWindowStyles.NoActivate); } WindowHandle = User32.CreateWindowEx( exStyle, WindowClassName, WindowTitle, (uint)(WindowStyles.Popup | WindowStyles.Visible), X, Y, Width, Height, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero ); if (WindowHandle == IntPtr.Zero) { WinApi.ThrowWin32Exception("Failed to create window!"); } User32.SetLayeredWindowAttributes(WindowHandle, 0, 255, (uint)(LayeredWindowAttribute.Alpha)); User32.UpdateWindow(WindowHandle); IsInitialized = true; // If window is incompatible on some platforms use SetWindowLong to set the style again // and UpdateWindow If you have changed certain window data using SetWindowLong, you must // call SetWindowPos for the changes to take effect. Use the following combination for // uFlags: SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED. ExtendFrameIntoClientArea(); }