|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Drawing; |
| 5 | +using System.Windows.Forms.Metafiles; |
| 6 | +using System.Windows.Forms.VisualStyles; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Tests; |
| 9 | + |
| 10 | +public class CheckBoxRenderingTests |
| 11 | +{ |
| 12 | + [WinFormsTheory] |
| 13 | + [InlineData(CheckBoxState.CheckedNormal)] |
| 14 | + [InlineData(CheckBoxState.MixedNormal)] |
| 15 | + public void CheckBoxRenderer_DrawCheckBox(CheckBoxState state) |
| 16 | + { |
| 17 | + using Bitmap bitmap = new(100, 100); |
| 18 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 19 | + Point rectangle = new Point(10, 20); |
| 20 | + |
| 21 | + CheckBoxRenderer.DrawCheckBox(graphics, rectangle, state); |
| 22 | + } |
| 23 | + |
| 24 | + [WinFormsTheory] |
| 25 | + [InlineData(CheckBoxState.CheckedNormal)] |
| 26 | + [InlineData(CheckBoxState.MixedNormal)] |
| 27 | + public void CheckBoxRenderer_DrawCheckBox_OverloadWithSizeAndText(CheckBoxState state) |
| 28 | + { |
| 29 | + using Bitmap bitmap = new(100, 100); |
| 30 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 31 | + Point point = new Point(10, 20); |
| 32 | + Rectangle bounds = new Rectangle(10, 20, 30, 40); |
| 33 | + |
| 34 | + CheckBoxRenderer.DrawCheckBox(graphics, point, bounds, "Text", SystemFonts.DefaultFont, false, state); |
| 35 | + } |
| 36 | + |
| 37 | + [WinFormsTheory] |
| 38 | + [InlineData(TextFormatFlags.Default, CheckBoxState.CheckedNormal)] |
| 39 | + [InlineData(TextFormatFlags.Default, CheckBoxState.MixedNormal)] |
| 40 | + [InlineData(TextFormatFlags.GlyphOverhangPadding, CheckBoxState.MixedHot)] |
| 41 | + [InlineData(TextFormatFlags.PreserveGraphicsTranslateTransform, CheckBoxState.CheckedPressed)] |
| 42 | + [InlineData(TextFormatFlags.TextBoxControl, CheckBoxState.UncheckedNormal)] |
| 43 | + public void CheckBoxRenderer_DrawCheckBox_OverloadWithTextFormat(TextFormatFlags textFormat, CheckBoxState state) |
| 44 | + { |
| 45 | + using Bitmap bitmap = new(100, 100); |
| 46 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 47 | + Point point = new Point(10, 20); |
| 48 | + Rectangle bounds = new Rectangle(10, 20, 30, 40); |
| 49 | + |
| 50 | + CheckBoxRenderer.DrawCheckBox(graphics, point, bounds, "Text", SystemFonts.DefaultFont, textFormat, false, state); |
| 51 | + } |
| 52 | + |
| 53 | + [WinFormsFact] |
| 54 | + public unsafe void CaptureButton() |
| 55 | + { |
| 56 | + using CheckBox checkBox = new(); |
| 57 | + using EmfScope emf = new(); |
| 58 | + checkBox.PrintToMetafile(emf); |
| 59 | + |
| 60 | + List<ENHANCED_METAFILE_RECORD_TYPE> types = []; |
| 61 | + List<string> details = []; |
| 62 | + emf.Enumerate((ref EmfRecord record) => |
| 63 | + { |
| 64 | + types.Add(record.Type); |
| 65 | + details.Add(record.ToString()); |
| 66 | + return true; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + [WinFormsFact] |
| 71 | + public unsafe void Button_VisualStyles_off_Default_LineDrawing() |
| 72 | + { |
| 73 | + if (Application.RenderWithVisualStyles) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + using CheckBox checkBox = new(); |
| 79 | + using EmfScope emf = new(); |
| 80 | + DeviceContextState state = new(emf); |
| 81 | + Rectangle bounds = checkBox.Bounds; |
| 82 | + |
| 83 | + checkBox.PrintToMetafile(emf); |
| 84 | + |
| 85 | + emf.Validate( |
| 86 | + state, |
| 87 | + Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_BITBLT), 1), |
| 88 | + Validate.LineTo( |
| 89 | + new(bounds.Right - 1, 0), new(0, 0), |
| 90 | + State.PenColor(SystemColors.ControlLightLight)), |
| 91 | + Validate.LineTo( |
| 92 | + new(0, 0), new(0, bounds.Bottom - 1), |
| 93 | + State.PenColor(SystemColors.ControlLightLight)), |
| 94 | + Validate.LineTo( |
| 95 | + new(0, bounds.Bottom - 1), new(bounds.Right - 1, bounds.Bottom - 1), |
| 96 | + State.PenColor(SystemColors.ControlDarkDark)), |
| 97 | + Validate.LineTo( |
| 98 | + new(bounds.Right - 1, bounds.Bottom - 1), new(bounds.Right - 1, -1), |
| 99 | + State.PenColor(SystemColors.ControlDarkDark)), |
| 100 | + Validate.LineTo( |
| 101 | + new(bounds.Right - 2, 1), new(1, 1), |
| 102 | + State.PenColor(SystemColors.Control)), |
| 103 | + Validate.LineTo( |
| 104 | + new(1, 1), new(1, bounds.Bottom - 2), |
| 105 | + State.PenColor(SystemColors.Control)), |
| 106 | + Validate.LineTo( |
| 107 | + new(1, bounds.Bottom - 2), new(bounds.Right - 2, bounds.Bottom - 2), |
| 108 | + State.PenColor(SystemColors.ControlDark)), |
| 109 | + Validate.LineTo( |
| 110 | + new(bounds.Right - 2, bounds.Bottom - 2), new(bounds.Right - 2, 0), |
| 111 | + State.PenColor(SystemColors.ControlDark))); |
| 112 | + } |
| 113 | + |
| 114 | + [WinFormsFact] |
| 115 | + public unsafe void Button_VisualStyles_off_Default_WithText_LineDrawing() |
| 116 | + { |
| 117 | + if (Application.RenderWithVisualStyles) |
| 118 | + { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + using CheckBox checkBox = new() { Text = "Hello" }; |
| 123 | + using EmfScope emf = new(); |
| 124 | + DeviceContextState state = new(emf); |
| 125 | + Rectangle bounds = checkBox.Bounds; |
| 126 | + |
| 127 | + checkBox.PrintToMetafile(emf); |
| 128 | + |
| 129 | + emf.Validate( |
| 130 | + state, |
| 131 | + Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_BITBLT), |
| 132 | + Validate.TextOut("Hello"), |
| 133 | + Validate.LineTo( |
| 134 | + new(bounds.Right - 1, 0), new(0, 0), |
| 135 | + State.PenColor(SystemColors.ControlLightLight)), |
| 136 | + Validate.LineTo( |
| 137 | + new(0, 0), new(0, bounds.Bottom - 1), |
| 138 | + State.PenColor(SystemColors.ControlLightLight)), |
| 139 | + Validate.LineTo( |
| 140 | + new(0, bounds.Bottom - 1), new(bounds.Right - 1, bounds.Bottom - 1), |
| 141 | + State.PenColor(SystemColors.ControlDarkDark)), |
| 142 | + Validate.LineTo( |
| 143 | + new(bounds.Right - 1, bounds.Bottom - 1), new(bounds.Right - 1, -1), |
| 144 | + State.PenColor(SystemColors.ControlDarkDark)), |
| 145 | + Validate.LineTo( |
| 146 | + new(bounds.Right - 2, 1), new(1, 1), |
| 147 | + State.PenColor(SystemColors.Control)), |
| 148 | + Validate.LineTo( |
| 149 | + new(1, 1), new(1, bounds.Bottom - 2), |
| 150 | + State.PenColor(SystemColors.Control)), |
| 151 | + Validate.LineTo( |
| 152 | + new(1, bounds.Bottom - 2), new(bounds.Right - 2, bounds.Bottom - 2), |
| 153 | + State.PenColor(SystemColors.ControlDark)), |
| 154 | + Validate.LineTo( |
| 155 | + new(bounds.Right - 2, bounds.Bottom - 2), new(bounds.Right - 2, 0), |
| 156 | + State.PenColor(SystemColors.ControlDark))); |
| 157 | + } |
| 158 | + |
| 159 | + [WinFormsFact] |
| 160 | + public unsafe void CaptureButtonOnForm() |
| 161 | + { |
| 162 | + using Form form = new(); |
| 163 | + using CheckBox checkBox = new(); |
| 164 | + form.Controls.Add(checkBox); |
| 165 | + |
| 166 | + using EmfScope emf = new(); |
| 167 | + form.PrintToMetafile(emf); |
| 168 | + |
| 169 | + string details = emf.RecordsToString(); |
| 170 | + } |
| 171 | +} |
0 commit comments