I have HTML-Source string saved in SQL Server table called \"Report\" in HTMLReport field (field type is NTEXT). Now I need to display that stored HTML into WPF Window. HT
I created a control based on this article:
http://blogs.msdn.com/b/ryanvog/archive/2009/01/20/clipping-legacy-content-hosted-inside-a-wpf-scrolling-region.aspx
...
public class ScrollableWebBrowser : WindowsFormsHost
{
[DllImport("GDI32.DLL", EntryPoint = "CreateRectRgn")]
private static extern IntPtr CreateRectRgn(Int32 x1, Int32 y1, Int32 x2, Int32 y2);
[DllImport("User32.dll", SetLastError = true)]
private static extern Int32 SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bRedraw);
private Int32 _topLeftX = -1;
private Int32 _topLeftY = -1;
private Int32 _bottomRightX = -1;
private Int32 _bottomRightY = -1;
bool _disposed = false;
public static readonly DependencyProperty HtmlProperty = DependencyProperty.Register("Html", typeof(string), typeof(ScrollableWebBrowser), new PropertyMetadata(OnHtmlChanged));
public string Html
{
get { return (string)GetValue(HtmlProperty); }
set { SetValue(HtmlProperty, value); }
}
public ScrollableWebBrowser()
{
EventManager.RegisterClassHandler(typeof(ScrollViewer), ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(ScrollHandler));
}
static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ScrollableWebBrowser)d).UpdateWebBrowser();
}
void UpdateWebBrowser()
{
if (this.Child == null)
this.Child = new System.Windows.Forms.WebBrowser();
System.Windows.Forms.WebBrowser browser = this.Child as System.Windows.Forms.WebBrowser;
if (browser != null)
browser.DocumentText = System.Net.WebUtility.HtmlDecode(Html);
}
private void ScrollHandler(Object sender, ScrollChangedEventArgs ea)
{
PresentationSource presentationSource = HwndSource.FromVisual(this);
if (presentationSource == null)
return;
Visual rootVisual = presentationSource.RootVisual;
if (rootVisual == null)
return;
ScrollViewer scrollViewer = (ScrollViewer)sender;
if (!scrollViewer.IsDescendantOf(rootVisual))
return;
Rect hostRect = this.TransformToAncestor(rootVisual).TransformBounds(new Rect(this.Padding.Left, this.Padding.Right, this.RenderSize.Width, this.RenderSize.Height));
Rect intersectRect = Rect.Intersect(scrollViewer.TransformToAncestor(rootVisual).TransformBounds(new Rect(0, 0, scrollViewer.ViewportWidth, scrollViewer.ViewportHeight)), hostRect);
Int32 topLeftX = 0;
Int32 topLeftY = 0;
Int32 bottomRightX = 0;
Int32 bottomRightY = 0;
if (intersectRect != Rect.Empty)
{
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);
}
if (_topLeftX != topLeftX || _topLeftY != topLeftY || _bottomRightX != bottomRightX || _bottomRightY != bottomRightY)
{
_topLeftX = topLeftX;
_topLeftY = topLeftY;
_bottomRightX = bottomRightX;
_bottomRightY = bottomRightY;
SetWindowRgn(this.Handle, CreateRectRgn(_topLeftX, _topLeftY, _bottomRightX, _bottomRightY), true);
}
}
protected override void Dispose(Boolean disposing)
{
if (disposing)
{
try
{
if (!_disposed && this.Child != null)
this.Child.Dispose();
_disposed = true;
}
finally
{
base.Dispose(disposing);
}
}
}
}