Skip to content

Add implicit conversion from RECT -> System.Drawing.Rectangle (System.Drawing.Primitives v8.0.0 from base shared framework). #387

@AraHaan

Description

@AraHaan

Description (optional)

I think an helpful implicit conversion to Rectangle would help with everything with APIs that uses RECT or returns RECT.

Rationale

Why to System.Drawing.Rectangle? Because the type exists in System.Drawing.Primitives which is part of the base shared framework which means it is not locked down to Microsoft.WindowDesktop.App as it exists in the Microsoft.NETCore.App shared framework which means it is available everywhere for every operating system. With it existing as a reference to every project that references the framework without it disabled that means it is safe to use in TerraFX.Interop.Windows as it is a primitive type. Also having operators between the 2 will help with RECT <-> Rectangle conversions without things going wrong when the developer messes up conversions as the operators will take all the guess work out of it all.

Proposed API

namespace TerraFX.Interop.Windows;

public partial struct RECT
{
+    public static implicit operator Rectangle(RECT rectangle);
+    public static explicit operator RECT(Rectangle rectangle);
}

Drawbacks

I think an implicit conversion like this would have minimal drawbacks with an explicit cast from Rectangle -> RECT as well so people can also pass those to APIs without doing it the long way as well (it becomes problematic to remember what should go into the Left, Right, Top, Bottom so operators that handles it for us could also help prevent bugs in people's code as well as convenience as well.

Alternatives

I have not though of any alternatives yet.

Other thoughts

Implementation would look something like this of the new operators:

namespace TerraFX.Interop.Windows;

public partial struct RECT
{
    public static implicit operator Rectangle(RECT rectangle)
        => Rectangle.FromLTRB(rectangle.left, rectangle.top, rectangle.right, rectangle.bottom);

    public static explicit operator RECT(Rectangle rectangle)
        => new // or new RECT()
        {
            // Obtained from inspecting 'Rectangle.FromLTRB(int, int, int, int)'.
            left = rectangle.X,
            top = rectangle.Y,
            right = rectangle.Width + rectangle.X,
            bottom = rectangle.Height + rectangle.Y,
        };
}

Discussions (optional)

I have not discussed about this much, this idea just came into mind when I was looking at some of my old code that processes TitleBarInfo related things.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalAn issue that represents a proposed feature or change to the repo.untriagedAn issue that has not been triaged by the repo maintainers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions