Skip to content

Commit 4b6b0e4

Browse files
committed
DXCanvas: recreate resources when device lost
1 parent 1d322a0 commit 4b6b0e4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Source/DXControl/DXCanvas.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class DXCanvas : Control
3636
protected int _lastFps = 0;
3737
protected DateTime _lastFpsUpdate = DateTime.UtcNow;
3838

39+
const uint D2DERR_RECREATE_TARGET = 0x8899000C;
40+
3941
#endregion // Internal properties
4042

4143

@@ -513,7 +515,23 @@ protected override void OnPaint(PaintEventArgs e)
513515
_device.BeginDraw();
514516
_device.Clear(BackColor.ToD3DCOLORVALUE());
515517
OnRender(_graphicsD2d);
516-
_device.EndDraw();
518+
519+
try
520+
{
521+
_device.EndDraw();
522+
}
523+
catch (Win32Exception ex)
524+
{
525+
// handle device lost
526+
if (ex.ErrorCode == unchecked((int)D2DERR_RECREATE_TARGET))
527+
{
528+
CreateDevice(DeviceCreatedReason.DeviceLost);
529+
}
530+
else
531+
{
532+
throw;
533+
}
534+
}
517535
}
518536

519537

Source/DXControl/Enums.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public enum DeviceCreatedReason
1414
{
1515
FirstTime,
1616
UseHardwareAccelerationChanged,
17+
DeviceLost,
1718
}
1819

1920

0 commit comments

Comments
 (0)