|
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; |
5 | 6 | using System.Windows.Forms.VisualStyles;
|
6 | 7 |
|
7 | 8 | namespace System.Windows.Forms;
|
@@ -44,39 +45,50 @@ public void RenderButton(
|
44 | 45 | ArgumentNullException.ThrowIfNull(paintImage);
|
45 | 46 | ArgumentNullException.ThrowIfNull(paintField);
|
46 | 47 |
|
47 |
| - // Clear the background over the whole button area. |
48 |
| - ClearBackground(graphics, parentBackgroundColor); |
| 48 | + GraphicsState? graphicsState = default; |
49 | 49 |
|
50 |
| - // Use padding from ButtonDarkModeRenderer |
51 |
| - Padding padding = PaddingCore; |
52 |
| - |
53 |
| - Rectangle paddedBounds = new( |
54 |
| - x: bounds.X + padding.Left, |
55 |
| - y: bounds.Y + padding.Top, |
56 |
| - width: bounds.Width - padding.Horizontal, |
57 |
| - height: bounds.Height - padding.Vertical); |
58 |
| - |
59 |
| - // Draw button background and get content bounds |
60 |
| - Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault); |
61 |
| - |
62 |
| - // Offset content bounds for Popup style when button is pressed |
63 |
| - // if (flatStyle == FlatStyle.Popup && state == PushButtonState.Pressed) |
64 |
| - // { |
65 |
| - // contentBounds.Offset(1, 1); |
66 |
| - // } |
67 |
| - |
68 |
| - // Paint image and field using the provided delegates |
69 |
| - paintImage(contentBounds); |
70 |
| - |
71 |
| - paintField( |
72 |
| - contentBounds, |
73 |
| - GetTextColor(state, isDefault), |
74 |
| - false); |
75 |
| - |
76 |
| - if (focused && showFocusCues) |
| 50 | + try |
| 51 | + { |
| 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 | + // Clear the background over the whole button area. |
| 57 | + ClearBackground(graphics, parentBackgroundColor); |
| 58 | + |
| 59 | + // Use padding from ButtonDarkModeRenderer |
| 60 | + Padding padding = PaddingCore; |
| 61 | + |
| 62 | + Rectangle paddedBounds = new( |
| 63 | + x: bounds.X + padding.Left, |
| 64 | + y: bounds.Y + padding.Top, |
| 65 | + width: bounds.Width - padding.Horizontal, |
| 66 | + height: bounds.Height - padding.Vertical); |
| 67 | + |
| 68 | + // Draw button background and get content bounds |
| 69 | + Rectangle contentBounds = DrawButtonBackground(graphics, paddedBounds, state, isDefault); |
| 70 | + |
| 71 | + // Paint image and field using the provided delegates |
| 72 | + paintImage(contentBounds); |
| 73 | + |
| 74 | + paintField( |
| 75 | + contentBounds, |
| 76 | + GetTextColor(state, isDefault), |
| 77 | + false); |
| 78 | + |
| 79 | + if (focused && showFocusCues) |
| 80 | + { |
| 81 | + // Draw focus indicator for other styles |
| 82 | + DrawFocusIndicator(graphics, bounds, isDefault); |
| 83 | + } |
| 84 | + } |
| 85 | + finally |
77 | 86 | {
|
78 |
| - // Draw focus indicator for other styles |
79 |
| - DrawFocusIndicator(graphics, bounds, isDefault); |
| 87 | + if (graphicsState is not null) |
| 88 | + { |
| 89 | + // Restore the graphics state to ensure no side effects |
| 90 | + graphics.Restore(graphicsState); |
| 91 | + } |
80 | 92 | }
|
81 | 93 | }
|
82 | 94 |
|
|
0 commit comments