private void AdjustWindow(bool updateScroll)
        {
            bool hasFocus = false;
            if (_originalControl != null)
                hasFocus = _originalControl.Focused;
            try
            {
                if (_targetHandle != IntPtr.Zero)
                {
                    if (_referenceControl is ComboBox)
                    {
                        RECT clientRectangle = new RECT();
                        GetClientRect(_targetHandle, out clientRectangle);
                        Point p1 = new Point(clientRectangle.Left, clientRectangle.Top);
                        Point p2 = new Point(clientRectangle.Right, clientRectangle.Bottom);
                        ClientToScreen(_targetHandle, ref p1);
                        ClientToScreen(_targetHandle, ref p2);
                        clientRectangle.Left = p1.X;
                        clientRectangle.Top = p1.Y;
                        clientRectangle.Right = p2.X;
                        clientRectangle.Bottom = p2.Y;

                        int systemScrollWidth = GetSystemMetrics(SystemMetric.SM_CXVSCROLL);
                        int nBorder = GetSystemMetrics(SystemMetric.SM_CXBORDER);
                        int wndStyle = GetWindowLong(_targetHandle, GWL_STYLE);
                        int wndStyleEx = GetWindowLong(_targetHandle, GWL_EXSTYLE);
                        bool leftScroll = ((wndStyleEx & WS_EX_LEFTSCROLLBAR) != 0);
                        bool vScroll = ((wndStyle & WS_VSCROLL) != 0);

                        if (vScroll)
                        {
                            if (_vScrollBar == null)
                            {
                                if (UpdateParent())
                                {
                                    _vScrollBar = new ScrollBarEx();
                                    long targetStyleEx = GetWindowLong(_vScrollBar.Handle, GWL_EXSTYLE);
                                    targetStyleEx |= WS_EX_TOOLWINDOW;
                                    //ShowWindow(_vScrollBar.Handle, SW_HIDE);
                                    SetWindowLong(_vScrollBar.Handle, GWL_EXSTYLE, (IntPtr)targetStyleEx);
                                    //ShowWindow(_vScrollBar.Handle, SW_SHOW);
                                    _vScrollBar.Orientation = ScrollOrientation.VerticalScroll;
                                    SetParent(_vScrollBar.Handle, GetParent(_targetHandle));
                                }
                            }
                        }
                        if (_vScrollBar != null)
                        {
                            if (vScroll && IsWindowVisible(_targetHandle))
                            {
                                _vScrollBar.Visible = true;
                                SetWindowPos(_vScrollBar.Handle, HWND_TOPMOST, clientRectangle.Right, clientRectangle.Top, systemScrollWidth, clientRectangle.Bottom - clientRectangle.Top, SetWindowPosFlags.SHOWWINDOW);
                                SetColors(_vScrollBar);
                            }
                            else
                                _vScrollBar.Visible = false;
                        }
                    }
                    else if (_referenceControl is DataGridView)
                    {
                        foreach (Control c in _referenceControl.Controls)
                        {
                            if (c is VScrollBar)
                                _targetVScrollBar = (ScrollBar)c;
                            else if (c is HScrollBar)
                                _targetHScrollBar = (ScrollBar)c;
                        }
                        if ((_targetVScrollBar != null) && (_targetVScrollBar.Visible))
                        {
                            if (UpdateParent())
                            {
                                if (_vScrollBar == null)
                                {
                                    _vScrollBar = new ScrollBarEx();
                                    _vScrollBar.Orientation = ScrollOrientation.VerticalScroll;
                                    _vScrollBar.Parent = _parentControl;
                                    _vScrollBar.Scroll += new ScrollEventHandler(OnScroll);
                                }
                                Rectangle rect = _targetVScrollBar.Parent.RectangleToScreen(_targetVScrollBar.Bounds);
                                rect = _parentControl.RectangleToClient(rect);
                                _vScrollBar.Location = rect.Location;
                                _vScrollBar.Size = rect.Size;
                            }
                        }
                        if (_vScrollBar != null)
                        {
                            if ((_targetVScrollBar != null) && (_targetVScrollBar.Visible))
                            {
                                _vScrollBar.Visible = true;
                                _vScrollBar.BringToFront();
                                SetColors(_vScrollBar);
                            }
                            else
                                _vScrollBar.Visible = false;
                        }
                        if ((_targetHScrollBar != null) && (_targetHScrollBar.Visible))
                        {
                            if (UpdateParent())
                            {
                                if (_hScrollBar == null)
                                {
                                    _hScrollBar = new ScrollBarEx();
                                    _hScrollBar.Orientation = ScrollOrientation.HorizontalScroll;
                                    _hScrollBar.Parent = _parentControl;
                                    _hScrollBar.Scroll += new ScrollEventHandler(OnScroll);
                                }
                                Rectangle rect = _targetVScrollBar.Parent.RectangleToScreen(_targetHScrollBar.Bounds);
                                rect = _parentControl.RectangleToClient(rect);
                                _hScrollBar.Location = rect.Location;
                                _hScrollBar.Size = rect.Size;
                            }
                        }
                        if (_hScrollBar != null)
                        {
                            if ((_targetHScrollBar != null) && (_targetHScrollBar.Visible))
                            {
                                _hScrollBar.Visible = true;
                                _hScrollBar.BringToFront();
                                SetColors(_hScrollBar);
                            }
                            else
                                _hScrollBar.Visible = false;
                        }
                    }
                    else
                    {
                        int systemScrollWidth = GetSystemMetrics(SystemMetric.SM_CXVSCROLL);
                        int nBorder = GetSystemMetrics(SystemMetric.SM_CXBORDER);
                        int wndStyle = GetWindowLong(_targetHandle, GWL_STYLE);
                        int wndStyleEx = GetWindowLong(_targetHandle, GWL_EXSTYLE);
                        bool leftScroll = ((wndStyleEx & WS_EX_LEFTSCROLLBAR) != 0);
                        bool vScroll = ((wndStyle & WS_VSCROLL) != 0) || (_referenceControl is PropertyGrid);
                        bool hScroll = ((wndStyle & WS_HSCROLL) != 0) || (_referenceControl is PropertyGrid);

                        if (vScroll || hScroll)
                        {
                            if (UpdateParent())
                            {
                                RECT clientRectangle = new RECT();
                                GetClientRect(_targetHandle, out clientRectangle);
                                Point p1 = new Point(clientRectangle.Left, clientRectangle.Top);
                                Point p2 = new Point(clientRectangle.Right, clientRectangle.Bottom);
                                ClientToScreen(_targetHandle, ref p1);
                                ClientToScreen(_targetHandle, ref p2);
                                clientRectangle.Left = p1.X;
                                clientRectangle.Top = p1.Y;
                                clientRectangle.Right = p2.X;
                                clientRectangle.Bottom = p2.Y;

                                RECT fullRectangle = new RECT();
                                GetClientRect(_parentControl.Handle, out fullRectangle);
                                p1 = new Point(fullRectangle.Left, fullRectangle.Top);
                                p2 = new Point(fullRectangle.Right, fullRectangle.Bottom);
                                ClientToScreen(_parentControl.Handle, ref p1);
                                ClientToScreen(_parentControl.Handle, ref p2);
                                fullRectangle.Left = p1.X;
                                fullRectangle.Top = p1.Y;
                                fullRectangle.Right = p2.X;
                                fullRectangle.Bottom = p2.Y;

                                if (vScroll)
                                {
                                    if (_vScrollBar == null)
                                    {
                                        _vScrollBar = new ScrollBarEx();
                                        _vScrollBar.Orientation = ScrollOrientation.VerticalScroll;
                                        _vScrollBar.Parent = _parentControl;
                                        _vScrollBar.Scroll += new ScrollEventHandler(OnScroll);
                                    }
                                    _vScrollBar.Size = new Size(systemScrollWidth, (clientRectangle.Bottom - clientRectangle.Top));
                                    _vScrollBar.Location = new Point(clientRectangle.Left - fullRectangle.Left + (clientRectangle.Right - clientRectangle.Left) - 1 + nBorder, clientRectangle.Top - fullRectangle.Top);
                                }
                                if (hScroll)
                                {
                                    if (_hScrollBar == null)
                                    {
                                        _hScrollBar = new ScrollBarEx();
                                        _hScrollBar.Orientation = ScrollOrientation.HorizontalScroll;
                                        _hScrollBar.Parent = _parentControl;
                                        _hScrollBar.Scroll += new ScrollEventHandler(OnScroll);
                                    }

                                    _hScrollBar.Size = new Size(clientRectangle.Right - clientRectangle.Left, systemScrollWidth);
                                    _hScrollBar.Location = new Point(clientRectangle.Left - fullRectangle.Left, clientRectangle.Top - fullRectangle.Top + (clientRectangle.Bottom - clientRectangle.Top) - 1 + nBorder);
                                }
                                if (leftScroll)
                                {
                                    if (vScroll)
                                    {
                                        _vScrollBar.Location = new Point(clientRectangle.Left - fullRectangle.Left - _vScrollBar.Width, clientRectangle.Top - fullRectangle.Top);
                                        if (_hScrollBar != null)
                                            _hScrollBar.Left = _vScrollBar.Right;
                                    }
                                }
                            }
                        }
                        if (_vScrollBar != null)
                        {
                            if (vScroll && _referenceControl.Visible)
                            {
                                _vScrollBar.Visible = true;
                                _vScrollBar.BringToFront();
                                SetColors(_vScrollBar);
                            }
                            else
                                _vScrollBar.Visible = false;
                        }
                        if (_hScrollBar != null)
                        {
                            if (hScroll && _referenceControl.Visible)
                            {
                                _hScrollBar.Visible = true;
                                _hScrollBar.BringToFront();
                                SetColors(_hScrollBar);
                            }
                            else
                                _hScrollBar.Visible = false;
                        }
                    }
                    if (updateScroll)
                        UpdateScrollBars();
                }
            }
            catch { }
            if (hasFocus)
            {
                if (!_originalControl.Focused)
                    _originalControl.Focus();
            }
        }
 private void OriginalControlParentChanged(object sender, EventArgs e)
 {
     if ((((Control)sender).Parent != null) && (((Control)sender).Parent != _parentControl))
     {
         _vScrollBar = null;
         _hScrollBar = null;
         _originalParentControl = ((Control)sender).Parent;
         _parentControl = null;
         AdjustWindow(true);
     }
 }
 private void ReferenceControlHandleCreated(object sender, EventArgs e)
 {
     if (InitializeHandle(_originalControl))
     {
         if ((_originalControl.Parent != null) && (_originalControl.Parent != _parentControl))
         {
             _vScrollBar = null;
             _hScrollBar = null;
             _originalParentControl = _originalControl.Parent;
             _parentControl = null;
             AdjustWindow(true);
         }
     }
 }
 private void SetColors(ScrollBarEx scrollBar)
 {
     if ((scrollBar.NormalColor != ScrollBarSkinner.NormalColor) && (!ScrollBarSkinner.NormalColor.IsEmpty))
         scrollBar.NormalColor = ScrollBarSkinner.NormalColor;
     if ((scrollBar.HoverColor != ScrollBarSkinner.HoverColor) && (!ScrollBarSkinner.HoverColor.IsEmpty))
         scrollBar.HoverColor = ScrollBarSkinner.HoverColor;
     if ((scrollBar.PressedColor != ScrollBarSkinner.PressedColor) && (!ScrollBarSkinner.PressedColor.IsEmpty))
         scrollBar.PressedColor = ScrollBarSkinner.PressedColor;
     if ((scrollBar.BackColor != ScrollBarSkinner.BackColor) && (!ScrollBarSkinner.BackColor.IsEmpty))
         scrollBar.BackColor = ScrollBarSkinner.BackColor;
     if ((scrollBar.TrackColor != ScrollBarSkinner.TrackColor) && (!ScrollBarSkinner.TrackColor.IsEmpty))
         scrollBar.TrackColor = ScrollBarSkinner.TrackColor;
     if ((scrollBar.TrackPressedColor != ScrollBarSkinner.TrackPressedColor) && (!ScrollBarSkinner.TrackPressedColor.IsEmpty))
         scrollBar.TrackPressedColor = ScrollBarSkinner.TrackPressedColor;
 }