Esempio n. 1
0
        private IntPtr WindowProcedure(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam)
        {
            switch (msg)
            {
            case WindowMessage.EraseBackground:
                User32.SendMessage(hwnd, WindowMessage.Paint, (IntPtr)0, (IntPtr)0);
                break;

            case WindowMessage.Keyup:
            case WindowMessage.Keydown:
            case WindowMessage.Syscommand:
            case WindowMessage.Syskeydown:
            case WindowMessage.Syskeyup:
                return((IntPtr)0);

            case WindowMessage.NcPaint:
            case WindowMessage.Paint:
                return((IntPtr)0);

            case WindowMessage.DwmCompositionChanged:
                WindowHelper.ExtendFrameIntoClientArea(hwnd);
                return((IntPtr)0);

            case WindowMessage.DpiChanged:
                return((IntPtr)0);    // block DPI changed message

            default: break;
            }

            return(User32.DefWindowProc(hwnd, msg, wParam, lParam));
        }
Esempio n. 2
0
        /// <summary>
        /// Makes the window visible.
        /// </summary>
        public void Show()
        {
            if (!_isInitialized)
            {
                throw new InvalidOperationException("OverlayWindow not initialized");
            }

            User32.ShowWindow(_handle, ShowWindowCommand.Show);

            _isVisible = true;

            WindowHelper.ExtendFrameIntoClientArea(_handle);

            OnVisibilityChanged(true);
        }
Esempio n. 3
0
        private void RemoveTopmost()
        {
            if (!_isTopmost)
            {
                return;
            }

            WindowHelper.RemoveTopmost(_handle);

            _isTopmost = false;

            if (_isVisible)
            {
                WindowHelper.ExtendFrameIntoClientArea(_handle);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Changes the size of the window using the given width and height.
        /// </summary>
        /// <param name="width">The new width of the window.</param>
        /// <param name="height">The new height of the window.</param>
        public void Resize(int width, int height)
        {
            if (!_isInitialized)
            {
                throw new InvalidOperationException("OverlayWindow not initialized");
            }

            User32.MoveWindow(_handle, _x, _y, width, height, true);

            _width  = width;
            _height = height;

            WindowHelper.ExtendFrameIntoClientArea(_handle);

            OnSizeChanged(width, height);
        }
Esempio n. 5
0
        /// <summary>
        /// Changes the position of the window using the given coordinates.
        /// </summary>
        /// <param name="x">The new x-coordinate of the window.</param>
        /// <param name="y">The new y-coordinate of the window.</param>
        public void Move(int x, int y)
        {
            if (!_isInitialized)
            {
                throw new InvalidOperationException("OverlayWindow not initialized");
            }

            User32.MoveWindow(_handle, x, y, _width, _height, true);

            _x = x;
            _y = y;

            WindowHelper.ExtendFrameIntoClientArea(_handle);

            OnPositionChanged(x, y);
        }
Esempio n. 6
0
        private void MakeTopmost()
        {
            if (_isTopmost)
            {
                return;
            }

            WindowHelper.MakeTopmost(_handle);

            _isTopmost = true;

            if (!_isVisible)
            {
                WindowHelper.ExtendFrameIntoClientArea(_handle);

                _isVisible = true;
                OnVisibilityChanged(true);
            }
        }
Esempio n. 7
0
        private void SetupWindow()
        {
            // generate a random title if it's null (invalid)
            if (_title == null)
            {
                _title = WindowHelper.GenerateRandomTitle();
            }
            if (string.IsNullOrEmpty(MenuName))
            {
                MenuName = WindowHelper.GenerateRandomTitle();
            }
            // if no class name is given then generate a "unique" one
            if (string.IsNullOrEmpty(_className))
            {
                _className = WindowHelper.GenerateRandomClass();
            }

            // prepare window procedure
            _windowProc        = WindowProcedure;
            _windowProcAddress = Marshal.GetFunctionPointerForDelegate(_windowProc);

            // try to register our class
            while (true)
            {
                var wndClassEx = new WindowClassEx
                {
                    Size        = WindowClassEx.NativeSize(),
                    Style       = 0,
                    WindowProc  = _windowProcAddress,
                    ClsExtra    = 0,
                    WindowExtra = 0,
                    Instance    = IntPtr.Zero,
                    Icon        = IntPtr.Zero,
                    Curser      = IntPtr.Zero,
                    Background  = IntPtr.Zero,
                    MenuName    = MenuName,
                    ClassName   = _className,
                    IconSm      = IntPtr.Zero
                };

                if (User32.RegisterClassEx(ref wndClassEx) != 0)
                {
                    break;
                }
                else
                {
                    // already taken name?
                    _className = WindowHelper.GenerateRandomClass();
                }
            }

            var extendedWindowStyle = ExtendedWindowStyle.Transparent | ExtendedWindowStyle.Layered | ExtendedWindowStyle.NoActivate;

            if (_isTopmost)
            {
                extendedWindowStyle |= ExtendedWindowStyle.Topmost;
            }

            var windowStyle = WindowStyle.Popup;

            if (_isVisible)
            {
                windowStyle |= WindowStyle.Visible;
            }

            _handle = User32.CreateWindowEx(
                extendedWindowStyle,
                _className,
                _title,
                windowStyle,
                _x, _y,
                _width, _height,
                IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            User32.SetLayeredWindowAttributes(_handle, 0, 255, LayeredWindowAttributes.Alpha);
            User32.UpdateWindow(_handle);

            // if the window is incompatible on some platforms then use
            // SetWindowLong and UpdateWindow to set the style again and
            // call SetWindowPos with SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED

            if (_isVisible)
            {
                WindowHelper.ExtendFrameIntoClientArea(_handle);
            }
        }