Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>Top left x position of window region</summary>
- private Int32 _topLeftX = -1;
- /// <summary>Top left y position of window region</summary>
- private Int32 _topLeftY = -1;
- /// <summary>Bottom right x position of window region</summary>
- private Int32 _bottomRightX = -1;
- /// <summary>Bottom right x position of window region</summary>
- private Int32 _bottomRightY = -1;
- // ...
- /// <summary>Creates a handle to a region specified through upper left and bottom right corner.</summary>
- /// <param name="x1">Upper left X</param>
- /// <param name="y1">Upper left Y</param>
- /// <param name="x2">Bottom right X</param>
- /// <param name="y2">Bottom right Y</param>
- /// <returns>Returns a handle to the created region.</returns>
- [DllImport("GDI32.DLL", EntryPoint = "CreateRectRgn")]
- private static extern IntPtr CreateRectRgn(Int32 x1, Int32 y1, Int32 x2, Int32 y2);
- /// <summary>Sets the specified region to the specified window. That means the specified window can paint
- /// itself only in the specified region.</summary>
- /// <param name="hWnd">Handle to the window</param>
- /// <param name="hRgn">Hanlde to the region</param>
- /// <param name="bRedraw">Boolean indicating whether the specified region should be redrawn.</param>
- /// <returns>Returns nonzero if success, otherwise zero.</returns>
- [DllImport("User32.dll", SetLastError = true)]
- private static extern Int32 SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bRedraw);
- /// <summary>
- /// Gets the handle of the parent
- /// </summary>
- /// <param name="hWnd">The Childs hWnd</param>
- /// <returns></returns>
- [DllImport("User32.dll")]
- private static extern IntPtr GetParent(IntPtr hWnd);
- private void OnScrollChanged(object sender, ScrollChangedEventArgs scrollChangedEventArgs)
- {
- PresentationSource presentationSource = PresentationSource.FromVisual(this);
- Visual rootVisual = presentationSource?.RootVisual;
- if (rootVisual == null)
- {
- return;
- }
- var scrollViewer = sender as ScrollViewer;
- if(scrollViewer == null || !scrollViewer.IsDescendantOf(rootVisual)) return;
- GeneralTransform transform = scrollViewer.TransformToAncestor(rootVisual);
- Rect scrollRect = transform.TransformBounds(new Rect(0, 0, scrollViewer.ViewportWidth, scrollViewer.ViewportHeight));
- // calculate the rect of the scrollable windows forms host instance with 0/0 at upper left corner of root visual
- transform = TransformToAncestor(rootVisual);
- Rect hostRect = transform.TransformBounds(new Rect(Padding.Left, Padding.Right,
- RenderSize.Width, RenderSize.Height));
- // calculate the intersection of the two rect
- Rect intersectRect = Rect.Intersect(scrollRect, hostRect);
- Int32 topLeftX = 0;
- Int32 topLeftY = 0;
- Int32 bottomRightX = 0;
- Int32 bottomRightY = 0;
- if (intersectRect != Rect.Empty)
- {
- // calculate the HRGN points with 0/0 at upper left corner of scrollable windows forms host instance
- topLeftX = (Int32)(intersectRect.TopLeft.X - hostRect.TopLeft.X);
- topLeftY = (Int32)(intersectRect.TopLeft.Y - hostRect.TopLeft.Y);
- bottomRightX = (Int32)(intersectRect.BottomRight.X - hostRect.TopLeft.X);
- bottomRightY = (Int32)(intersectRect.BottomRight.Y - hostRect.TopLeft.Y);
- }
- // because the CreateRectRgn / SetWindowRgn api calls are slow we call them only if it has a visual effect
- if (_topLeftX != topLeftX || _topLeftY != topLeftY || _bottomRightX != bottomRightX || _bottomRightY != bottomRightY)
- {
- _topLeftX = topLeftX;
- _topLeftY = topLeftY;
- _bottomRightX = bottomRightX;
- _bottomRightY = bottomRightY;
- // create HRGN object and set it to the windows forms host instance
- IntPtr hrgn = CreateRectRgn(_topLeftX, _topLeftY, _bottomRightX, _bottomRightY);
- SetWindowRgn(GetParent(_webView.Handle), hrgn, true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement