Skip to content

Commit eb0b8a0

Browse files
author
ArthurHub
committed
refactor mono CreateGraphics handling
1 parent 38ff535 commit eb0b8a0

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

Source/HtmlRenderer.WinForms/HtmlLabel.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,21 @@ protected override void OnLayout(LayoutEventArgs levent)
471471
{
472472
if (_htmlContainer != null)
473473
{
474-
using (Graphics g = CreateGraphics())
475-
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
474+
Graphics g = Utils.CreateGraphics(this);
475+
if (g != null)
476476
{
477-
var newSize = HtmlRendererUtils.Layout(ig,
478-
_htmlContainer.HtmlContainerInt,
479-
new RSize(ClientSize.Width - Padding.Horizontal, ClientSize.Height - Padding.Vertical),
480-
new RSize(MinimumSize.Width - Padding.Horizontal, MinimumSize.Height - Padding.Vertical),
481-
new RSize(MaximumSize.Width - Padding.Horizontal, MaximumSize.Height - Padding.Vertical),
482-
AutoSize,
483-
AutoSizeHeightOnly);
484-
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
477+
using (g)
478+
using (var ig = new GraphicsAdapter(g, _htmlContainer.UseGdiPlusTextRendering))
479+
{
480+
var newSize = HtmlRendererUtils.Layout(ig,
481+
_htmlContainer.HtmlContainerInt,
482+
new RSize(ClientSize.Width - Padding.Horizontal, ClientSize.Height - Padding.Vertical),
483+
new RSize(MinimumSize.Width - Padding.Horizontal, MinimumSize.Height - Padding.Vertical),
484+
new RSize(MaximumSize.Width - Padding.Horizontal, MaximumSize.Height - Padding.Vertical),
485+
AutoSize,
486+
AutoSizeHeightOnly);
487+
ClientSize = Utils.ConvertRound(new RSize(newSize.Width + Padding.Horizontal, newSize.Height + Padding.Vertical));
488+
}
485489
}
486490
}
487491
base.OnLayout(levent);

Source/HtmlRenderer.WinForms/HtmlPanel.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,15 +457,15 @@ protected void PerformHtmlLayout()
457457
{
458458
_htmlContainer.MaxSize = new SizeF(ClientSize.Width - Padding.Horizontal, 0);
459459

460-
try
460+
Graphics g = Utils.CreateGraphics(this);
461+
if (g != null)
461462
{
462-
using (var g = CreateGraphics())
463+
using (g)
463464
{
464465
_htmlContainer.PerformLayout(g);
465466
}
466467
}
467-
catch
468-
{ }
468+
469469

470470
AutoScrollMinSize = Size.Round(new SizeF(_htmlContainer.ActualSize.Width + Padding.Horizontal, _htmlContainer.ActualSize.Height));
471471
}
@@ -683,11 +683,16 @@ protected virtual void InvokeMouseMove()
683683
{
684684
try
685685
{
686+
// mono has issue throwing exception for no reason
686687
var mp = PointToClient(MousePosition);
687688
_htmlContainer.HandleMouseMove(this, new MouseEventArgs(MouseButtons.None, 0, mp.X, mp.Y, 0));
688689
}
689690
catch
690-
{ }
691+
{
692+
#if !MONO
693+
throw;
694+
#endif
695+
}
691696
}
692697

693698
/// <summary>

Source/HtmlRenderer.WinForms/Utilities/Utils.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using System;
1414
using System.Drawing;
15+
using System.Windows.Forms;
1516
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
1617

1718
namespace TheArtOfDev.HtmlRenderer.WinForms.Utilities
@@ -119,5 +120,26 @@ public static Color Convert(RColor c)
119120
{
120121
return Color.FromArgb(c.A, c.R, c.G, c.B);
121122
}
123+
124+
/// <summary>
125+
/// mono has issue throwing exception for no reason.
126+
/// </summary>
127+
/// <param name="control">the control to create graphics object from</param>
128+
/// <returns>new graphics object or null in mono if failed</returns>
129+
public static Graphics CreateGraphics(Control control)
130+
{
131+
#if MONO
132+
try
133+
{
134+
return control.CreateGraphics();
135+
}
136+
catch
137+
{
138+
return null;
139+
}
140+
#else
141+
return control.CreateGraphics();
142+
#endif
143+
}
122144
}
123145
}

0 commit comments

Comments
 (0)