Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adapts to another window in the postion and size.
        /// </summary>
        /// <param name="windowHandle">The target window handle.</param>
        /// <param name="attachToClientArea">A Boolean determining whether to fit to the client area of the target window.</param>
        public void FitToWindow(IntPtr windowHandle, bool attachToClientArea = false)
        {
            WindowBounds rect;
            bool         result = attachToClientArea ? WindowHelper.GetWindowClientBounds(windowHandle, out rect) : WindowHelper.GetWindowBounds(windowHandle, out rect);

            if (result)
            {
                int x      = rect.Left;
                int y      = rect.Top;
                int width  = rect.Right - rect.Left;
                int height = rect.Bottom - rect.Top;

                if (X != x ||
                    Y != y ||
                    Width != width ||
                    Height != height)
                {
                    Resize(x, y, width, height);
                }
            }
        }