Skip to content

Commit 8b3b781

Browse files
committed
feat: Modernize HtmlRenderer.Core and HtmlRenderer.WinForms for .NET 10
1 parent 2283fc5 commit 8b3b781

19 files changed

Lines changed: 111 additions & 97 deletions

‎Source/HtmlRenderer.WinForms/Adapters/WinFormsAdapter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected override RBrush CreateLinearGradientBrush(RRect rect, RColor color1, R
8888
return new BrushAdapter(new LinearGradientBrush(Utils.Convert(rect), Utils.Convert(color1), Utils.Convert(color2), (float)angle), true);
8989
}
9090

91-
protected override RImage ConvertImageInt(object image)
91+
protected override RImage? ConvertImageInt(object? image)
9292
{
9393
return image != null ? new ImageAdapter((Image)image) : null;
9494
}

‎Source/HtmlRenderer.WinForms/HtmlContainer.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,15 @@ public SizeF ActualSize
286286
/// <summary>
287287
/// Get the currently selected text segment in the html.
288288
/// </summary>
289-
public string SelectedText
289+
public string? SelectedText
290290
{
291291
get { return _htmlContainerInt.SelectedText; }
292292
}
293293

294294
/// <summary>
295295
/// Copy the currently selected html segment with style.
296296
/// </summary>
297-
public string SelectedHtml
297+
public string? SelectedHtml
298298
{
299299
get { return _htmlContainerInt.SelectedHtml; }
300300
}
@@ -312,7 +312,7 @@ public void ClearSelection()
312312
/// </summary>
313313
/// <param name="htmlSource">the html to init with, init empty if not given</param>
314314
/// <param name="baseCssData">optional: the stylesheet to init with, init default if not given</param>
315-
public void SetHtml(string htmlSource, CssData baseCssData = null)
315+
public void SetHtml(string? htmlSource, CssData? baseCssData = null)
316316
{
317317
_htmlContainerInt.SetHtml(htmlSource, baseCssData);
318318
}
@@ -322,7 +322,7 @@ public void SetHtml(string htmlSource, CssData baseCssData = null)
322322
/// </summary>
323323
/// <param name="styleGen">Optional: controls the way styles are generated when html is generated (default: <see cref="HtmlGenerationStyle.Inline"/>)</param>
324324
/// <returns>generated html</returns>
325-
public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
325+
public string? GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
326326
{
327327
return _htmlContainerInt.GetHtml(styleGen);
328328
}
@@ -334,7 +334,7 @@ public string GetHtml(HtmlGenerationStyle styleGen = HtmlGenerationStyle.Inline)
334334
/// <param name="location">the location to find the attribute at</param>
335335
/// <param name="attribute">the attribute key to get value by</param>
336336
/// <returns>found attribute value or null if not found</returns>
337-
public string GetAttributeAt(Point location, string attribute)
337+
public string? GetAttributeAt(Point location, string attribute)
338338
{
339339
return _htmlContainerInt.GetAttributeAt(Utils.Convert(location), attribute);
340340
}
@@ -358,7 +358,7 @@ public List<LinkElementData<RectangleF>> GetLinks()
358358
/// </summary>
359359
/// <param name="location">the location to find the link at</param>
360360
/// <returns>css link href if exists or null</returns>
361-
public string GetLinkAt(Point location)
361+
public string? GetLinkAt(Point location)
362362
{
363363
return _htmlContainerInt.GetLinkAt(Utils.Convert(location));
364364
}

‎Source/HtmlRenderer.WinForms/HtmlLabel.cs‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class HtmlLabel : Control
7575
/// <summary>
7676
/// Underline html container instance.
7777
/// </summary>
78-
protected HtmlContainer _htmlContainer;
78+
protected HtmlContainer? _htmlContainer;
7979

8080
/// <summary>
8181
/// The current border style of the control
@@ -85,17 +85,17 @@ public class HtmlLabel : Control
8585
/// <summary>
8686
/// the raw base stylesheet data used in the control
8787
/// </summary>
88-
protected string _baseRawCssData;
88+
protected string? _baseRawCssData;
8989

9090
/// <summary>
9191
/// the base stylesheet data used in the panel
9292
/// </summary>
93-
protected CssData _baseCssData;
93+
protected CssData? _baseCssData;
9494

9595
/// <summary>
9696
/// the current html text set in the control
9797
/// </summary>
98-
protected string _text;
98+
protected string? _text;
9999

100100
/// <summary>
101101
/// is to handle auto size of the control height only
@@ -296,7 +296,7 @@ public virtual bool IsContextMenuEnabled
296296
[Description("Set base stylesheet to be used by html rendered in the control.")]
297297
[Category("Appearance")]
298298
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
299-
[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")]
299+
[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")]
300300
public virtual string BaseStylesheet
301301
{
302302
get { return _baseRawCssData; }
@@ -488,21 +488,17 @@ protected override void OnLayout(LayoutEventArgs levent)
488488
{
489489
if (_htmlContainer != null)
490490
{
491-
Graphics g = CreateGraphics();
492-
if (g != null)
491+
using (var g = CreateGraphics())
492+
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
493493
{
494-
using (g)
495-
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
496-
{
497-
var newSize = HtmlRendererUtils.Layout(ig,
494+
var newSize = HtmlRendererUtils.Layout(ig,
498495
_htmlContainer.HtmlContainerInt,
499496
new RSize(ClientSize.Width - Padding.Horizontal, ClientSize.Height - Padding.Vertical),
500497
new RSize(MinimumSize.Width - Padding.Horizontal, MinimumSize.Height - Padding.Vertical),
501498
new RSize(MaximumSize.Width - Padding.Horizontal, MaximumSize.Height - Padding.Vertical),
502499
AutoSize,
503500
AutoSizeHeightOnly);
504-
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
505-
}
501+
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
506502
}
507503
}
508504
base.OnLayout(levent);
@@ -659,7 +655,7 @@ protected override void WndProc(ref Message m)
659655
try
660656
{
661657
// Replace .NET's hand cursor with the OS cursor
662-
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
658+
Win32Utils.SetCursor(Win32Utils.LoadCursor(IntPtr.Zero, Win32Utils.IdcHand));
663659
m.Result = IntPtr.Zero;
664660
return;
665661
}

‎Source/HtmlRenderer.WinForms/HtmlPanel.cs‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ public class HtmlPanel : ScrollableControl
7777
/// <summary>
7878
/// the raw base stylesheet data used in the control
7979
/// </summary>
80-
protected string _baseRawCssData;
80+
protected string? _baseRawCssData;
8181

8282
/// <summary>
8383
/// the base stylesheet data used in the control
8484
/// </summary>
85-
protected CssData _baseCssData;
85+
protected CssData? _baseCssData;
8686

8787
/// <summary>
8888
/// the current html text set in the control
8989
/// </summary>
90-
protected string _text;
90+
protected string? _text;
9191

9292
/// <summary>
9393
/// If to use cursors defined by the operating system or .NET cursors
@@ -304,7 +304,7 @@ public virtual bool IsContextMenuEnabled
304304
[Category("Appearance")]
305305
[Description("Set base stylesheet to be used by html rendered in the control.")]
306306
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
307-
[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")]
307+
[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")]
308308
public virtual string BaseStylesheet
309309
{
310310
get { return _baseRawCssData; }
@@ -471,13 +471,9 @@ protected void PerformHtmlLayout()
471471
{
472472
_htmlContainer.MaxSize = new SizeF(ClientSize.Width - Padding.Horizontal, 0);
473473

474-
Graphics g = CreateGraphics();
475-
if (g != null)
474+
using (var g = CreateGraphics())
476475
{
477-
using (g)
478-
{
479-
_htmlContainer.PerformLayout(g);
480-
}
476+
_htmlContainer.PerformLayout(g);
481477
}
482478

483479
AutoScrollMinSize = Size.Round(new SizeF(_htmlContainer.ActualSize.Width + Padding.Horizontal, _htmlContainer.ActualSize.Height));
@@ -747,7 +743,7 @@ protected override void WndProc(ref Message m)
747743
try
748744
{
749745
// Replace .NET's hand cursor with the OS cursor
750-
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
746+
Win32Utils.SetCursor(Win32Utils.LoadCursor(IntPtr.Zero, Win32Utils.IdcHand));
751747
m.Result = IntPtr.Zero;
752748
return;
753749
}

‎Source/HtmlRenderer.WinForms/HtmlRenderer.WinForms.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net8.0-windows</TargetFramework>
3+
<TargetFramework>net10.0-windows</TargetFramework>
44
<OutputType>Library</OutputType>
55
<RootNamespace>TheArtOfDev.HtmlRenderer.WinForms</RootNamespace>
66
<UseWindowsForms>true</UseWindowsForms>
7+
<Nullable>enable</Nullable>
78
<GenerateResourceUsePreserializedResources>true</GenerateResourceUsePreserializedResources>
8-
<EnableWindowsTargeting>true</EnableWindowsTargeting>
99
</PropertyGroup>
1010
<!-- NuGet Package Properties -->
1111
<PropertyGroup>

‎Source/HtmlRenderer.WinForms/HtmlToolTip.cs‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ public class HtmlToolTip : ToolTip
3131
/// <summary>
3232
/// the container to render and handle the html shown in the tooltip
3333
/// </summary>
34-
protected HtmlContainer _htmlContainer;
34+
protected HtmlContainer? _htmlContainer;
3535

3636
/// <summary>
3737
/// the raw base stylesheet data used in the control
3838
/// </summary>
39-
protected string _baseRawCssData;
39+
protected string? _baseRawCssData;
4040

4141
/// <summary>
4242
/// the base stylesheet data used in the panel
4343
/// </summary>
44-
protected CssData _baseCssData;
44+
protected CssData? _baseCssData;
4545

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

6262
/// <summary>
6363
/// timer used to handle mouse move events when mouse is over the tooltip.<br/>
6464
/// Used for link handling.
6565
/// </summary>
66-
private Timer _linkHandlingTimer;
66+
private Timer? _linkHandlingTimer;
6767

6868
/// <summary>
6969
/// the handle of the actual tooltip window used to know when the tooltip is hidden<br/>
@@ -175,7 +175,7 @@ public TextRenderingHint TextRenderingHint
175175
[Description("Set base stylesheet to be used by html rendered in the tooltip.")]
176176
[Category("Appearance")]
177177
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
178-
[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")]
178+
[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")]
179179
public virtual string BaseStylesheet
180180
{
181181
get { return _baseRawCssData; }
@@ -238,6 +238,9 @@ public virtual Size MaximumSize
238238
/// </summary>
239239
protected virtual void OnToolTipPopup(PopupEventArgs e)
240240
{
241+
if (e.AssociatedControl == null)
242+
return;
243+
241244
//Create fragment container
242245
var cssClass = string.IsNullOrEmpty(_tooltipCssClass) ? null : string.Format(" class=\"{0}\"", _tooltipCssClass);
243246
var toolipHtml = string.Format("<div{0}>{1}</div>", cssClass, GetToolTip(e.AssociatedControl));
@@ -276,7 +279,8 @@ protected virtual void OnToolTipDraw(DrawToolTipEventArgs e)
276279
_tooltipHandle = Win32Utils.WindowFromDC(hdc);
277280
e.Graphics.ReleaseHdc(hdc);
278281

279-
AdjustTooltipPosition(e.AssociatedControl, e.Bounds.Size);
282+
if (e.AssociatedControl != null)
283+
AdjustTooltipPosition(e.AssociatedControl, e.Bounds.Size);
280284
}
281285

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

376-
var mPos = Control.MousePosition;
377-
var mButtons = Control.MouseButtons;
378-
var rect = Win32Utils.GetWindowRectangle(handle);
379-
if (rect.Contains(mPos))
380+
if (handle != IntPtr.Zero && _associatedControl != null)
380381
{
381-
if (mButtons == MouseButtons.Left)
382+
var mPos = Control.MousePosition;
383+
var mButtons = Control.MouseButtons;
384+
var rect = Win32Utils.GetWindowRectangle(handle);
385+
if (rect.Contains(mPos))
382386
{
383-
var args = new MouseEventArgs(mButtons, 1, mPos.X - rect.X, mPos.Y - rect.Y, 0);
384-
_htmlContainer.HandleMouseDown(_associatedControl, args);
385-
_htmlContainer.HandleMouseUp(_associatedControl, args);
387+
if (mButtons == MouseButtons.Left)
388+
{
389+
var args = new MouseEventArgs(mButtons, 1, mPos.X - rect.X, mPos.Y - rect.Y, 0);
390+
_htmlContainer.HandleMouseDown(_associatedControl, args);
391+
_htmlContainer.HandleMouseUp(_associatedControl, args);
392+
}
386393
}
387394
}
388395
}
@@ -402,23 +409,22 @@ protected virtual void OnToolTipDisposed(EventArgs e)
402409
Draw -= OnToolTipDraw;
403410
Disposed -= OnToolTipDisposed;
404411

412+
if (_linkHandlingTimer != null)
413+
{
414+
_linkHandlingTimer.Tick -= OnLinkHandlingTimerTick;
415+
_linkHandlingTimer.Dispose();
416+
_linkHandlingTimer = null;
417+
}
418+
405419
if (_htmlContainer != null)
406420
{
421+
_htmlContainer.LinkClicked -= OnLinkClicked;
407422
_htmlContainer.RenderError -= OnRenderError;
408423
_htmlContainer.StylesheetLoad -= OnStylesheetLoad;
409424
_htmlContainer.ImageLoad -= OnImageLoad;
410425
_htmlContainer.Dispose();
411426
_htmlContainer = null;
412427
}
413-
414-
if (_linkHandlingTimer != null)
415-
{
416-
_linkHandlingTimer.Dispose();
417-
_linkHandlingTimer = null;
418-
419-
if (_htmlContainer != null)
420-
_htmlContainer.LinkClicked -= OnLinkClicked;
421-
}
422428
}
423429

424430

‎Source/HtmlRenderer.WinForms/MetafileExtensions.cs‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@ public static void SaveAsEmf(Metafile me, string fileName)
1414
by : SWAT Team member _1
1515
Date : Friday, February 01, 2008 1:38 PM
1616
*/
17-
int enfMetafileHandle = me.GetHenhmetafile().ToInt32();
17+
IntPtr enfMetafileHandle = me.GetHenhmetafile();
1818
int bufferSize = GetEnhMetaFileBits(enfMetafileHandle, 0, null); // Get required buffer size.
1919
byte[] buffer = new byte[bufferSize]; // Allocate sufficient buffer
2020
if (GetEnhMetaFileBits(enfMetafileHandle, bufferSize, buffer) <= 0) // Get raw metafile data.
2121
throw new SystemException("Fail");
2222

23-
FileStream ms = File.Open(fileName, FileMode.Create);
24-
ms.Write(buffer, 0, bufferSize);
25-
ms.Close();
26-
ms.Dispose();
23+
using (var ms = File.Open(fileName, FileMode.Create))
24+
{
25+
ms.Write(buffer, 0, bufferSize);
26+
}
27+
2728
if (!DeleteEnhMetaFile(enfMetafileHandle)) //free handle
2829
throw new SystemException("Fail Free");
2930
}
3031

31-
[DllImport("gdi32")]
32-
public static extern int GetEnhMetaFileBits(int hemf, int cbBuffer, byte[] lpbBuffer);
32+
[DllImport("gdi32.dll")]
33+
public static extern int GetEnhMetaFileBits(IntPtr hemf, int cbBuffer, byte[] lpbBuffer);
3334

34-
[DllImport("gdi32")]
35-
public static extern bool DeleteEnhMetaFile(int hemfbitHandle);
35+
[DllImport("gdi32.dll")]
36+
public static extern bool DeleteEnhMetaFile(IntPtr hemfbitHandle);
3637
}
3738
}

‎Source/HtmlRenderer.WinForms/Utilities/Utils.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System;
1414
using System.Drawing;
1515
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
16+
using TheArtOfDev.HtmlRenderer.Core.Utils;
1617

1718
namespace TheArtOfDev.HtmlRenderer.WinForms.Utilities
1819
{
@@ -34,6 +35,7 @@ public static RPoint Convert(PointF p)
3435
/// </summary>
3536
public static PointF[] Convert(RPoint[] points)
3637
{
38+
ArgChecker.AssertArgNotNull(points, nameof(points));
3739
PointF[] myPoints = new PointF[points.Length];
3840
for (int i = 0; i < points.Length; i++)
3941
myPoints[i] = Convert(points[i]);

‎Source/HtmlRenderer.WinForms/Utilities/Win32Utils.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ internal static class Win32Utils
4747

4848
public const int TextAlignBaselineRtl = 256 + 24;
4949

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

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

5656
/// <summary>
5757
/// Create a compatible memory HDC from the given HDC.<br/>

0 commit comments

Comments
 (0)