Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/HtmlRenderer.WinForms/Adapters/WinFormsAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected override RBrush CreateLinearGradientBrush(RRect rect, RColor color1, R
return new BrushAdapter(new LinearGradientBrush(Utils.Convert(rect), Utils.Convert(color1), Utils.Convert(color2), (float)angle), true);
}

protected override RImage ConvertImageInt(object image)
protected override RImage? ConvertImageInt(object? image)
{
return image != null ? new ImageAdapter((Image)image) : null;
}
Expand Down
12 changes: 6 additions & 6 deletions Source/HtmlRenderer.WinForms/HtmlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ public SizeF ActualSize
/// <summary>
/// Get the currently selected text segment in the html.
/// </summary>
public string SelectedText
public string? SelectedText
{
get { return _htmlContainerInt.SelectedText; }
}

/// <summary>
/// Copy the currently selected html segment with style.
/// </summary>
public string SelectedHtml
public string? SelectedHtml
{
get { return _htmlContainerInt.SelectedHtml; }
}
Expand All @@ -312,7 +312,7 @@ public void ClearSelection()
/// </summary>
/// <param name="htmlSource">the html to init with, init empty if not given</param>
/// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
public void SetHtml(string htmlSource, CssData baseCssData = null)
public void SetHtml(string? htmlSource, CssData? baseCssData = null)
{
_htmlContainerInt.SetHtml(htmlSource, baseCssData);
}
Expand All @@ -322,7 +322,7 @@ public void SetHtml(string htmlSource, CssData baseCssData = null)
/// </summary>
/// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
/// <returns>generated html</returns>
public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
public string? GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
{
return _htmlContainerInt.GetHtml(styleGen);
}
Expand All @@ -334,7 +334,7 @@ public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
/// <param name="location">the location to find the attribute at</param>
/// <param name="attribute">the attribute key to get value by</param>
/// <returns>found attribute value or null if not found</returns>
public string GetAttributeAt(Point location, string attribute)
public string? GetAttributeAt(Point location, string attribute)
{
return _htmlContainerInt.GetAttributeAt(Utils.Convert(location), attribute);
}
Expand All @@ -358,7 +358,7 @@ public List<LinkElementData<RectangleF>> GetLinks()
/// </summary>
/// <param name="location">the location to find the link at</param>
/// <returns>css link href if exists or null</returns>
public string GetLinkAt(Point location)
public string? GetLinkAt(Point location)
{
return _htmlContainerInt.GetLinkAt(Utils.Convert(location));
}
Expand Down
24 changes: 10 additions & 14 deletions Source/HtmlRenderer.WinForms/HtmlLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class HtmlLabel : Control
/// <summary>
/// Underline html container instance.
/// </summary>
protected HtmlContainer _htmlContainer;
protected HtmlContainer? _htmlContainer;

/// <summary>
/// The current border style of the control
Expand All @@ -85,17 +85,17 @@ public class HtmlLabel : Control
/// <summary>
/// the raw base stylesheet data used in the control
/// </summary>
protected string _baseRawCssData;
protected string? _baseRawCssData;

/// <summary>
/// the base stylesheet data used in the panel
/// </summary>
protected CssData _baseCssData;
protected CssData? _baseCssData;

/// <summary>
/// the current html text set in the control
/// </summary>
protected string _text;
protected string? _text;

/// <summary>
/// is to handle auto size of the control height only
Expand Down Expand Up @@ -296,7 +296,7 @@ public virtual bool IsContextMenuEnabled
[Description("Set base stylesheet to be used by html rendered in the control.")]
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Windows.Forms.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Drawing.Design.UITypeEditor, System.Windows.Forms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public virtual string BaseStylesheet
{
get { return _baseRawCssData; }
Expand Down Expand Up @@ -488,21 +488,17 @@ protected override void OnLayout(LayoutEventArgs levent)
{
if (_htmlContainer != null)
{
Graphics g = CreateGraphics();
if (g != null)
using (var g = CreateGraphics())
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
{
using (g)
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
{
var newSize = HtmlRendererUtils.Layout(ig,
var newSize = HtmlRendererUtils.Layout(ig,
_htmlContainer.HtmlContainerInt,
new RSize(ClientSize.Width - Padding.Horizontal, ClientSize.Height - Padding.Vertical),
new RSize(MinimumSize.Width - Padding.Horizontal, MinimumSize.Height - Padding.Vertical),
new RSize(MaximumSize.Width - Padding.Horizontal, MaximumSize.Height - Padding.Vertical),
AutoSize,
AutoSizeHeightOnly);
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
}
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
}
}
base.OnLayout(levent);
Expand Down Expand Up @@ -659,7 +655,7 @@ protected override void WndProc(ref Message m)
try
{
// Replace .NET's hand cursor with the OS cursor
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
Win32Utils.SetCursor(Win32Utils.LoadCursor(IntPtr.Zero, Win32Utils.IdcHand));
m.Result = IntPtr.Zero;
return;
}
Expand Down
18 changes: 7 additions & 11 deletions Source/HtmlRenderer.WinForms/HtmlPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ public class HtmlPanel : ScrollableControl
/// <summary>
/// the raw base stylesheet data used in the control
/// </summary>
protected string _baseRawCssData;
protected string? _baseRawCssData;

/// <summary>
/// the base stylesheet data used in the control
/// </summary>
protected CssData _baseCssData;
protected CssData? _baseCssData;

/// <summary>
/// the current html text set in the control
/// </summary>
protected string _text;
protected string? _text;

/// <summary>
/// If to use cursors defined by the operating system or .NET cursors
Expand Down Expand Up @@ -304,7 +304,7 @@ public virtual bool IsContextMenuEnabled
[Category("Appearance")]
[Description("Set base stylesheet to be used by html rendered in the control.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Windows.Forms.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Drawing.Design.UITypeEditor, System.Windows.Forms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public virtual string BaseStylesheet
{
get { return _baseRawCssData; }
Expand Down Expand Up @@ -471,13 +471,9 @@ protected void PerformHtmlLayout()
{
_htmlContainer.MaxSize = new SizeF(ClientSize.Width - Padding.Horizontal, 0);

Graphics g = CreateGraphics();
if (g != null)
using (var g = CreateGraphics())
{
using (g)
{
_htmlContainer.PerformLayout(g);
}
_htmlContainer.PerformLayout(g);
}

AutoScrollMinSize = Size.Round(new SizeF(_htmlContainer.ActualSize.Width + Padding.Horizontal, _htmlContainer.ActualSize.Height));
Expand Down Expand Up @@ -747,7 +743,7 @@ protected override void WndProc(ref Message m)
try
{
// Replace .NET's hand cursor with the OS cursor
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
Win32Utils.SetCursor(Win32Utils.LoadCursor(IntPtr.Zero, Win32Utils.IdcHand));
m.Result = IntPtr.Zero;
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/HtmlRenderer.WinForms/HtmlRenderer.WinForms.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net10.0-windows</TargetFramework>
<OutputType>Library</OutputType>
<RootNamespace>TheArtOfDev.HtmlRenderer.WinForms</RootNamespace>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
</PropertyGroup>
<!-- NuGet Package Properties -->
<PropertyGroup>
Expand Down
54 changes: 30 additions & 24 deletions Source/HtmlRenderer.WinForms/HtmlToolTip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public class HtmlToolTip : ToolTip
/// <summary>
/// the container to render and handle the html shown in the tooltip
/// </summary>
protected HtmlContainer _htmlContainer;
protected HtmlContainer? _htmlContainer;

/// <summary>
/// the raw base stylesheet data used in the control
/// </summary>
protected string _baseRawCssData;
protected string? _baseRawCssData;

/// <summary>
/// the base stylesheet data used in the panel
/// </summary>
protected CssData _baseCssData;
protected CssData? _baseCssData;

/// <summary>
/// The text rendering hint to be used for text rendering.
Expand All @@ -57,13 +57,13 @@ public class HtmlToolTip : ToolTip
/// the control that the tooltip is currently showing on.<br/>
/// Used for link handling.
/// </summary>
private Control _associatedControl;
private Control? _associatedControl;

/// <summary>
/// timer used to handle mouse move events when mouse is over the tooltip.<br/>
/// Used for link handling.
/// </summary>
private Timer _linkHandlingTimer;
private Timer? _linkHandlingTimer;

/// <summary>
/// the handle of the actual tooltip window used to know when the tooltip is hidden<br/>
Expand Down Expand Up @@ -175,7 +175,7 @@ public TextRenderingHint TextRenderingHint
[Description("Set base stylesheet to be used by html rendered in the tooltip.")]
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Windows.Forms.Design, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Drawing.Design.UITypeEditor, System.Windows.Forms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public virtual string BaseStylesheet
{
get { return _baseRawCssData; }
Expand Down Expand Up @@ -238,6 +238,9 @@ public virtual Size MaximumSize
/// </summary>
protected virtual void OnToolTipPopup(PopupEventArgs e)
{
if (e.AssociatedControl == null)
return;

//Create fragment container
var cssClass = string.IsNullOrEmpty(_tooltipCssClass) ? null : string.Format(" class=\"{0}\"", _tooltipCssClass);
var toolipHtml = string.Format("<div{0}>{1}</div>", cssClass, GetToolTip(e.AssociatedControl));
Expand Down Expand Up @@ -276,7 +279,8 @@ protected virtual void OnToolTipDraw(DrawToolTipEventArgs e)
_tooltipHandle = Win32Utils.WindowFromDC(hdc);
e.Graphics.ReleaseHdc(hdc);

AdjustTooltipPosition(e.AssociatedControl, e.Bounds.Size);
if (e.AssociatedControl != null)
AdjustTooltipPosition(e.AssociatedControl, e.Bounds.Size);
}

e.Graphics.Clear(Color.White);
Expand Down Expand Up @@ -373,16 +377,19 @@ protected virtual void OnLinkHandlingTimerTick(EventArgs e)
_linkHandlingTimer.Stop();
_tooltipHandle = IntPtr.Zero;

var mPos = Control.MousePosition;
var mButtons = Control.MouseButtons;
var rect = Win32Utils.GetWindowRectangle(handle);
if (rect.Contains(mPos))
if (handle != IntPtr.Zero && _associatedControl != null)
{
if (mButtons == MouseButtons.Left)
var mPos = Control.MousePosition;
var mButtons = Control.MouseButtons;
var rect = Win32Utils.GetWindowRectangle(handle);
if (rect.Contains(mPos))
{
var args = new MouseEventArgs(mButtons, 1, mPos.X - rect.X, mPos.Y - rect.Y, 0);
_htmlContainer.HandleMouseDown(_associatedControl, args);
_htmlContainer.HandleMouseUp(_associatedControl, args);
if (mButtons == MouseButtons.Left)
{
var args = new MouseEventArgs(mButtons, 1, mPos.X - rect.X, mPos.Y - rect.Y, 0);
_htmlContainer.HandleMouseDown(_associatedControl, args);
_htmlContainer.HandleMouseUp(_associatedControl, args);
}
}
}
}
Expand All @@ -402,23 +409,22 @@ protected virtual void OnToolTipDisposed(EventArgs e)
Draw -= OnToolTipDraw;
Disposed -= OnToolTipDisposed;

if (_linkHandlingTimer != null)
{
_linkHandlingTimer.Tick -= OnLinkHandlingTimerTick;
_linkHandlingTimer.Dispose();
_linkHandlingTimer = null;
}

if (_htmlContainer != null)
{
_htmlContainer.LinkClicked -= OnLinkClicked;
_htmlContainer.RenderError -= OnRenderError;
_htmlContainer.StylesheetLoad -= OnStylesheetLoad;
_htmlContainer.ImageLoad -= OnImageLoad;
_htmlContainer.Dispose();
_htmlContainer = null;
}

if (_linkHandlingTimer != null)
{
_linkHandlingTimer.Dispose();
_linkHandlingTimer = null;

if (_htmlContainer != null)
_htmlContainer.LinkClicked -= OnLinkClicked;
}
}


Expand Down
19 changes: 10 additions & 9 deletions Source/HtmlRenderer.WinForms/MetafileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ public static void SaveAsEmf(Metafile me, string fileName)
by : SWAT Team member _1
Date : Friday, February 01, 2008 1:38 PM
*/
int enfMetafileHandle = me.GetHenhmetafile().ToInt32();
IntPtr enfMetafileHandle = me.GetHenhmetafile();
int bufferSize = GetEnhMetaFileBits(enfMetafileHandle, 0, null); // Get required buffer size.
byte[] buffer = new byte[bufferSize]; // Allocate sufficient buffer
if (GetEnhMetaFileBits(enfMetafileHandle, bufferSize, buffer) <= 0) // Get raw metafile data.
throw new SystemException("Fail");

FileStream ms = File.Open(fileName, FileMode.Create);
ms.Write(buffer, 0, bufferSize);
ms.Close();
ms.Dispose();
using (var ms = File.Open(fileName, FileMode.Create))
{
ms.Write(buffer, 0, bufferSize);
}

if (!DeleteEnhMetaFile(enfMetafileHandle)) //free handle
throw new SystemException("Fail Free");
}

[DllImport("gdi32")]
public static extern int GetEnhMetaFileBits(int hemf, int cbBuffer, byte[] lpbBuffer);
[DllImport("gdi32.dll")]
public static extern int GetEnhMetaFileBits(IntPtr hemf, int cbBuffer, byte[] lpbBuffer);

[DllImport("gdi32")]
public static extern bool DeleteEnhMetaFile(int hemfbitHandle);
[DllImport("gdi32.dll")]
public static extern bool DeleteEnhMetaFile(IntPtr hemfbitHandle);
}
}
2 changes: 2 additions & 0 deletions Source/HtmlRenderer.WinForms/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System;
using System.Drawing;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.Core.Utils;

namespace TheArtOfDev.HtmlRenderer.WinForms.Utilities
{
Expand All @@ -34,6 +35,7 @@ public static RPoint Convert(PointF p)
/// </summary>
public static PointF[] Convert(RPoint[] points)
{
ArgChecker.AssertArgNotNull(points, nameof(points));
PointF[] myPoints = new PointF[points.Length];
for (int i = 0; i < points.Length; i++)
myPoints[i] = Convert(points[i]);
Expand Down
8 changes: 4 additions & 4 deletions Source/HtmlRenderer.WinForms/Utilities/Win32Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ internal static class Win32Utils

public const int TextAlignBaselineRtl = 256 + 24;

[DllImport("user32.dll")]
public static extern int SetCursor(int hCursor);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetCursor(IntPtr hCursor);

[DllImport("user32.dll")]
public static extern int LoadCursor(int hInstance, int lpCursorName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);

/// <summary>
/// Create a compatible memory HDC from the given HDC.<br/>
Expand Down
Loading