Skip to content

Commit 8208866

Browse files
author
ArthurHub
committed
* add ScrollToElement to scroll HtmlPanel to specific element by id
1 parent baefd46 commit 8208866

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

‎Source/HtmlRenderer/HtmlPanel.cs‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Windows.Forms;
1717
using HtmlRenderer.Entities;
1818
using HtmlRenderer.Parse;
19+
using HtmlRenderer.Utils;
1920

2021
namespace HtmlRenderer
2122
{
@@ -256,6 +257,26 @@ public string GetHtml()
256257
return _htmlContainer != null ? _htmlContainer.GetHtml() : null;
257258
}
258259

260+
/// <summary>
261+
/// Adjust the scrollbar of the panel on html element by the given id.<br/>
262+
/// The top of the html element rectangle will be at the top of the panel, if there
263+
/// is not enough height to scroll to the top the scroll will be at maximum.<br/>
264+
/// </summary>
265+
/// <param name="elementId">the id of the element to scroll to</param>
266+
public void ScrollToElement(string elementId)
267+
{
268+
ArgChecker.AssertArgNotNullOrEmpty(elementId, "elementId");
269+
270+
if( _htmlContainer != null )
271+
{
272+
var rect = _htmlContainer.GetElementRectangle(elementId);
273+
if (rect.HasValue)
274+
{
275+
UpdateScroll(Point.Round(rect.Value.Location));
276+
_htmlContainer.HandleMouseMove(this, new MouseEventArgs(MouseButtons, 0, MousePosition.X, MousePosition.Y, 0));
277+
}
278+
}
279+
}
259280

260281
#region Private methods
261282

@@ -481,7 +502,16 @@ private void OnRefresh(object sender, HtmlRefreshEventArgs e)
481502
/// </summary>
482503
private void OnScrollChange(object sender, HtmlScrollEventArgs e)
483504
{
484-
AutoScrollPosition = e.Location;
505+
UpdateScroll(e.Location);
506+
}
507+
508+
/// <summary>
509+
/// Adjust the scrolling of the panel to the requested location.
510+
/// </summary>
511+
/// <param name="location">the location to adjust the scroll to</param>
512+
private void UpdateScroll(Point location)
513+
{
514+
AutoScrollPosition = location;
485515
_htmlContainer.ScrollOffset = AutoScrollPosition;
486516
}
487517

0 commit comments

Comments
 (0)