|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Drawing;
|
5 |
| -using System.Drawing.Drawing2D; |
6 | 5 | using System.Windows.Forms.VisualStyles;
|
7 | 6 |
|
8 | 7 | namespace System.Windows.Forms;
|
@@ -45,14 +44,9 @@ public void RenderButton(
|
45 | 44 | ArgumentNullException.ThrowIfNull(paintImage);
|
46 | 45 | ArgumentNullException.ThrowIfNull(paintField);
|
47 | 46 |
|
48 |
| - GraphicsState? graphicsState = default; |
49 |
| - |
50 |
| - try |
| 47 | + // Scope the graphics state so all changes are reverted after rendering |
| 48 | + using (new GraphicsStateScope(graphics)) |
51 | 49 | {
|
52 |
| - // Save the graphics state, so we can restore it later and the |
53 |
| - // overrides do not to be concerned with restoring the state. |
54 |
| - graphicsState = graphics.Save(); |
55 |
| - |
56 | 50 | // Clear the background over the whole button area.
|
57 | 51 | ClearBackground(graphics, parentBackgroundColor);
|
58 | 52 |
|
@@ -82,14 +76,6 @@ public void RenderButton(
|
82 | 76 | DrawFocusIndicator(graphics, bounds, isDefault);
|
83 | 77 | }
|
84 | 78 | }
|
85 |
| - finally |
86 |
| - { |
87 |
| - if (graphicsState is not null) |
88 |
| - { |
89 |
| - // Restore the graphics state to ensure no side effects |
90 |
| - graphics.Restore(graphicsState); |
91 |
| - } |
92 |
| - } |
93 | 79 | }
|
94 | 80 |
|
95 | 81 | public abstract Rectangle DrawButtonBackground(Graphics graphics, Rectangle bounds, PushButtonState state, bool isDefault);
|
|
0 commit comments