-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix getting stock objects #3774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ystem.Windows.Forms.Primitives/tests/TestUtilities/Metafiles/RecordTypes/EMRRECTRECORD.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System.Drawing; | ||
| using System.Runtime.InteropServices; | ||
| using static Interop; | ||
|
|
||
| namespace System.Windows.Forms.Metafiles | ||
| { | ||
| /// <summary> | ||
| /// Record that just has a single <see cref="RECT"/> value. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Not an actual Win32 define, encapsulates: | ||
| /// | ||
| /// - EMRFILLPATH | ||
| /// - EMRSTROKEANDFILLPATH | ||
| /// - EMRSTROKEPATH | ||
| /// - EMREXCLUDECLIPRECT | ||
| /// - EMRINTERSECTCLIPRECT | ||
| /// - EMRELLIPSE | ||
| /// - EMRRECTANGLE | ||
| /// </remarks> | ||
| [StructLayout(LayoutKind.Sequential)] | ||
| internal struct EMRRECTRECORD | ||
| { | ||
| public EMR emr; | ||
| public RECT rect; | ||
|
|
||
| public override string ToString() => $"[EMR{emr.iType}] RECT: {rect}"; | ||
| } | ||
| } |
143 changes: 143 additions & 0 deletions
143
...m.Windows.Forms.Primitives/tests/TestUtilities/Metafiles/Validators/RectangleValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System.Drawing; | ||
| using Xunit; | ||
| using static Interop; | ||
|
|
||
| namespace System.Windows.Forms.Metafiles | ||
| { | ||
| internal sealed class RectangleValidator : IEmfValidator | ||
| { | ||
| private readonly Flags _validate; | ||
| private readonly Rectangle _bounds; | ||
| private readonly int _penWidth; | ||
| private readonly Color _penColor; | ||
| private readonly Gdi32.PS _penStyle; | ||
| private readonly Gdi32.R2 _rop2; | ||
| private readonly Gdi32.BKMODE _backgroundMode; | ||
| private readonly Gdi32.BS _brushStyle; | ||
| private readonly Color _brushColor; | ||
|
|
||
| public RectangleValidator( | ||
| RECT bounds, | ||
| Color penColor, | ||
| Color brushColor, | ||
| int penWidth = 1, | ||
| Gdi32.PS penStyle = default, | ||
| Gdi32.R2 rop2 = Gdi32.R2.COPYPEN, | ||
| Gdi32.BKMODE backgroundMode = Gdi32.BKMODE.TRANSPARENT, | ||
| Gdi32.BS brushStyle = default, | ||
| Flags validate = default) | ||
| { | ||
| _bounds = bounds; | ||
| _brushColor = brushColor; | ||
| _brushStyle = brushStyle; | ||
| _penWidth = penWidth; | ||
| _penColor = penColor; | ||
| _penStyle = penStyle; | ||
| _rop2 = rop2; | ||
| _backgroundMode = backgroundMode; | ||
|
|
||
| if (validate != default) | ||
| { | ||
| _validate = validate; | ||
| return; | ||
| } | ||
|
|
||
| // Default values for all of these are valid expectations so we always turn them on. | ||
| _validate = Flags.Bounds | Flags.PenStyle | Flags.BrushStyle; | ||
|
|
||
| if (penWidth != 0) | ||
| { | ||
| _validate |= Flags.PenWidth; | ||
| } | ||
|
|
||
| if (!penColor.IsEmpty) | ||
| { | ||
| _validate |= Flags.PenColor; | ||
| } | ||
|
|
||
| if (!brushColor.IsEmpty) | ||
| { | ||
| _validate |= Flags.BrushColor; | ||
| } | ||
|
|
||
| if (backgroundMode != default) | ||
| { | ||
| _validate |= Flags.BackgroundMode; | ||
| } | ||
|
|
||
| if (_rop2 != default) | ||
| { | ||
| _validate |= Flags.RopMode; | ||
| } | ||
| } | ||
|
|
||
| public bool ShouldValidate(Gdi32.EMR recordType) => recordType == Gdi32.EMR.RECTANGLE; | ||
|
|
||
| public unsafe void Validate(ref EmfRecord record, DeviceContextState state, out bool complete) | ||
| { | ||
| // We're only checking one TextOut record, so this call completes our work. | ||
| complete = true; | ||
|
|
||
| EMRRECTRECORD* rectangle = record.RectangleRecord; | ||
|
|
||
| if (_validate.HasFlag(Flags.Bounds)) | ||
| { | ||
| Assert.Equal(_bounds, (Rectangle)rectangle->rect); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.BrushColor)) | ||
| { | ||
| Assert.Equal((COLORREF)_brushColor, state.SelectedBrush.lbColor); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.BrushStyle)) | ||
| { | ||
| Assert.Equal(_brushStyle, state.SelectedBrush.lbStyle); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.PenColor)) | ||
| { | ||
| Assert.Equal((COLORREF)_penColor, state.SelectedPen.lopnColor); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.PenWidth)) | ||
| { | ||
| Assert.Equal(_penWidth, state.SelectedPen.lopnWidth.X); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.PenStyle)) | ||
| { | ||
| Assert.Equal(_penStyle, state.SelectedPen.lopnStyle); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.BackgroundMode)) | ||
| { | ||
| Assert.Equal(_backgroundMode, state.BackgroundMode); | ||
| } | ||
|
|
||
| if (_validate.HasFlag(Flags.RopMode)) | ||
| { | ||
| Assert.Equal(_rop2, state.Rop2Mode); | ||
| } | ||
| } | ||
|
|
||
| [Flags] | ||
| public enum Flags : uint | ||
| { | ||
| Bounds = 0b00000000_00000000_00000000_00000001, | ||
| PenColor = 0b00000000_00000000_00000000_00000100, | ||
| PenWidth = 0b00000000_00000000_00000000_00001000, | ||
| PenStyle = 0b00000000_00000000_00000000_00010000, | ||
| RopMode = 0b00000000_00000000_00000000_00100000, | ||
| BackgroundMode = 0b00000000_00000000_00000000_01000000, | ||
| BrushColor = 0b00000000_00000000_00000000_10000000, | ||
| BrushStyle = 0b00000000_00000000_00000001_00000000 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/System.Windows.Forms.Primitives/tests/UnitTests/Interop/Gdi32/GetStockObjectTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Xunit; | ||
| using static Interop; | ||
|
|
||
| namespace System.Windows.Forms.Gdi32Tests | ||
| { | ||
| public class GetStockObjectTests | ||
| { | ||
| [Theory] | ||
| [InlineData((int)Gdi32.StockObject.BLACK_BRUSH, 0x00000000, (uint)Gdi32.BS.SOLID)] | ||
| [InlineData((int)Gdi32.StockObject.NULL_BRUSH, 0x00000000, (uint)Gdi32.BS.HOLLOW)] | ||
| [InlineData((int)Gdi32.StockObject.WHITE_BRUSH, 0x00FFFFFF, (uint)Gdi32.BS.SOLID)] | ||
| public void GetStockBrushes(int id, uint color, uint brushStyle) | ||
| { | ||
| Gdi32.HGDIOBJ hgdiobj = Gdi32.GetStockObject((Gdi32.StockObject)id); | ||
| Assert.False(hgdiobj.IsNull); | ||
|
|
||
| Gdi32.GetObjectW(hgdiobj, out Gdi32.LOGBRUSH logBrush); | ||
| Assert.Equal(color, logBrush.lbColor); | ||
| Assert.Equal((Gdi32.BS)brushStyle, logBrush.lbStyle); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,5 +91,34 @@ public unsafe void CaptureButtonOnForm() | |
|
|
||
| var details = emf.RecordsToString(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how you get an idea of the records involved. Once you |
||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public unsafe void Button_FlatStyle_WithText_Rectangle() | ||
| { | ||
| using Button button = new Button | ||
| { | ||
| Text = "Flat Style", | ||
| FlatStyle = FlatStyle.Flat, | ||
| }; | ||
|
|
||
| using var emf = new EmfScope(); | ||
| DeviceContextState state = new DeviceContextState(emf); | ||
|
|
||
| Rectangle bounds = button.Bounds; | ||
|
|
||
| button.PrintToMetafile(emf); | ||
|
|
||
| emf.Validate( | ||
| state, | ||
| Validate.SkipType(Gdi32.EMR.BITBLT), | ||
| Validate.TextOut("Flat Style"), | ||
| Validate.Rectangle( | ||
| new Rectangle(0, 0, button.Width - 2, button.Height - 2), | ||
| penColor: Color.Black, | ||
| penStyle: Gdi32.PS.ENDCAP_ROUND, | ||
| brushColor: Color.Empty, // Color doesn't really matter as we're using a null brush | ||
| brushStyle: Gdi32.BS.NULL, // Regressed in https://github.com/dotnet/winforms/pull/3667 | ||
| rop2: Gdi32.R2.COPYPEN)); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a comment about distinction between "regular" stock objects to EMF stock objects, but it looks like it did not get saved...
Because your explanation about the high order
1in EMF stock objects is in a different file, this code will be more readable if you qualify what kind of stock object it is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add some comments in master clarifying that EMF takes the twos complement to smuggle stock object identifiers.