Skip to content

Commit

Permalink
DXCanvas: recreate resources when device lost
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Nov 30, 2024
1 parent 1d322a0 commit 4b6b0e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Source/DXControl/DXCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class DXCanvas : Control
protected int _lastFps = 0;
protected DateTime _lastFpsUpdate = DateTime.UtcNow;

const uint D2DERR_RECREATE_TARGET = 0x8899000C;

#endregion // Internal properties


Expand Down Expand Up @@ -513,7 +515,23 @@ protected override void OnPaint(PaintEventArgs e)
_device.BeginDraw();
_device.Clear(BackColor.ToD3DCOLORVALUE());
OnRender(_graphicsD2d);
_device.EndDraw();

try
{
_device.EndDraw();
}
catch (Win32Exception ex)
{
// handle device lost
if (ex.ErrorCode == unchecked((int)D2DERR_RECREATE_TARGET))
{
CreateDevice(DeviceCreatedReason.DeviceLost);
}
else
{
throw;
}
}
}


Expand Down
1 change: 1 addition & 0 deletions Source/DXControl/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum DeviceCreatedReason
{
FirstTime,
UseHardwareAccelerationChanged,
DeviceLost,
}


Expand Down

0 comments on commit 4b6b0e4

Please sign in to comment.