private void setupInstance(int x = 0, int y = 0, int width = 800, int height = 600)
        {
            IsVisible = true;
            Topmost   = true;

            X      = x;
            Y      = y;
            Width  = width;
            Height = height;

            randomClassName = generateRandomString(5, 11);
            string randomMenuName   = generateRandomString(5, 11);
            string randomWindowName = generateRandomString(5, 11);

            // prepare method
            wndProc = windowProcedure;
            RuntimeHelpers.PrepareDelegate(wndProc);
            wndProcPointer = Marshal.GetFunctionPointerForDelegate(wndProc);

            PInvoke.WNDCLASSEX wndClassEx = new PInvoke.WNDCLASSEX()
            {
                cbSize        = PInvoke.WNDCLASSEX.Size(),
                style         = 0,
                lpfnWndProc   = wndProcPointer,
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = IntPtr.Zero,
                hIcon         = IntPtr.Zero,
                hCursor       = IntPtr.Zero,
                hbrBackground = IntPtr.Zero,
                lpszMenuName  = randomMenuName,
                lpszClassName = randomClassName,
                hIconSm       = IntPtr.Zero
            };

            PInvoke.RegisterClassEx(ref wndClassEx);

            WindowHandle = PInvoke.CreateWindowEx(
                0x8 | 0x20 | 0x80000 | 0x80 | 0x8000000, // WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED |WS_EX_TOOLWINDOW
                randomClassName,
                randomWindowName,
                0x80000000 | 0x10000000,
                X, Y,
                Width, Height,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero
                );

            PInvoke.SetLayeredWindowAttributes(WindowHandle, 0, 255, /*0x1 |*/ 0x2);
            extendFrameIntoClientArea();
            PInvoke.UpdateWindow(WindowHandle);
        }
Exemple #2
0
        private void setupInstance(int x = 0, int y = 0, int width = 800, int height = 600)
        {
            IsVisible = true;
            Topmost   = BypassTopmost ? true : false;

            X      = x;
            Y      = y;
            Width  = width;
            Height = height;

            randomClassName = generateRandomString(5, 11);
            string randomMenuName = generateRandomString(5, 11);

            string randomWindowName = string.Empty;//generateRandomString(5, 11);

            switch (WindowNameGenerator)
            {
            case OverlayWindowNameGenerator.None:
                randomWindowName = string.Empty;
                break;

            case OverlayWindowNameGenerator.Random:
                randomWindowName = generateRandomString(5, 11);
                break;

            case OverlayWindowNameGenerator.Legit:
                randomWindowName = getLegitWindowName();
                break;

            case OverlayWindowNameGenerator.Executable:
                randomWindowName = getExecutableName();
                break;

            case OverlayWindowNameGenerator.Custom:
                randomWindowName = CustomWindowName;
                break;

            default:
                randomWindowName = string.Empty;
                break;
            }

            // prepare method
            wndProc = windowProcedure;
            RuntimeHelpers.PrepareDelegate(wndProc);
            wndProcPointer = Marshal.GetFunctionPointerForDelegate(wndProc);

            PInvoke.WNDCLASSEX wndClassEx = new PInvoke.WNDCLASSEX()
            {
                cbSize        = PInvoke.WNDCLASSEX.Size(),
                style         = 0,
                lpfnWndProc   = wndProcPointer,
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = IntPtr.Zero,
                hIcon         = IntPtr.Zero,
                hCursor       = IntPtr.Zero,
                hbrBackground = IntPtr.Zero,
                lpszMenuName  = randomMenuName,
                lpszClassName = randomClassName,
                hIconSm       = IntPtr.Zero
            };

            PInvoke.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 = PInvoke.CreateWindowEx(
                exStyle, // WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED |WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE
                randomClassName,
                randomWindowName,
                0x80000000 | 0x10000000, // WS_POPUP | WS_VISIBLE
                X, Y,
                Width, Height,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero
                );

            PInvoke.SetLayeredWindowAttributes(WindowHandle, 0, 255, /*0x1 |*/ 0x2);
            PInvoke.UpdateWindow(WindowHandle);

            // TODO: 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();
        }