Skip to content

Commit

Permalink
Extensiones para ventanas de Windows
Browse files Browse the repository at this point in the history
Pruebas unitarias
  • Loading branch information
TheXDS committed Aug 5, 2023
1 parent 186d67c commit be57267
Show file tree
Hide file tree
Showing 17 changed files with 704 additions and 74 deletions.
39 changes: 34 additions & 5 deletions src/Extras/Wpf.Win19041/Types/Extensions/UIColorTypeExtensions.cs
@@ -1,9 +1,38 @@
using System.Windows.Media;
// UIColorTypeExtensions.cs
//
// This file is part of Morgan's CLR Advanced Runtime (MCART)
//
// Author(s):
// César Andrés Morgan <xds_xps_ivx@hotmail.com>
//
// Released under the MIT License (MIT)
// Copyright © 2011 - 2023 César Andrés Morgan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the “Software”), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Windows.Media;
using Windows.UI.ViewManagement;
using MC = TheXDS.MCART.Types.Color;
using DC = System.Drawing.Color;
using MC = TheXDS.MCART.Types.Color;
using WC = System.Windows.Media.Color;

namespace TheXDS.MCART.Wpf.Win19041.Types.Extensions;
namespace TheXDS.MCART.Types.Extensions;

/// <summary>
/// Contiene extensiones para el tipo <see cref="UIColorType"/>.
Expand Down Expand Up @@ -32,10 +61,10 @@ public static Brush ToMediaBrush(this UIColorType color)
/// Un nuevo <see cref="Color"/> equivalente al <see cref="UIColorType"/>
/// especificado.
/// </returns>
public static Color ToMediaColor(this UIColorType color)
public static WC ToMediaColor(this UIColorType color)
{
var c = new UISettings().GetColorValue(color);
return Color.FromArgb(c.A, c.R, c.G, c.B);
return WC.FromArgb(c.A, c.R, c.G, c.B);
}

/// <summary>
Expand Down
Expand Up @@ -32,7 +32,7 @@
using System.Windows.Data;
using System.Windows.Media;
using TheXDS.MCART.ValueConverters.Base;
using TheXDS.MCART.Wpf.Win19041.Types.Extensions;
using TheXDS.MCART.Types.Extensions;
using Windows.UI.ViewManagement;
using MT = TheXDS.MCART.Types;

Expand Down
11 changes: 9 additions & 2 deletions src/Lib/MCART.Windows/PInvoke/DwmApi.cs
Expand Up @@ -28,13 +28,20 @@
SOFTWARE.
*/

using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using TheXDS.MCART.PInvoke.Structs;

namespace TheXDS.MCART.PInvoke;

[ExcludeFromCodeCoverage]
internal partial class DwmApi
{
#pragma warning disable SYSLIB1054
[DllImport("dwmapi.dll", SetLastError = false, ExactSpelling = true)]
internal static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, in DWM_BLURBEHIND pDwmBlurbehind);
#pragma warning restore SYSLIB1054

#if NET7_0_OR_GREATER

[LibraryImport("dwmapi.dll")]
Expand All @@ -48,7 +55,7 @@ internal partial class DwmApi
internal static partial int DwmGetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, [MarshalAs(UnmanagedType.Bool)] out bool pvAttribute, int cbAttribute);

[LibraryImport("dwmapi.dll")]
internal static partial int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, ref int pvAttribute, int cbAttribute);
internal static partial int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, ref nint pvAttribute, nint cbAttribute);

[LibraryImport("dwmapi.dll")]
internal static partial int DwmGetColorizationColor(out uint pcrColorization, [MarshalAs(UnmanagedType.Bool)] out bool pfOpaqueBlend);
Expand All @@ -65,7 +72,7 @@ internal partial class DwmApi
internal static extern int DwmGetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, out bool pvAttribute, int cbAttribute);

[DllImport("dwmapi.dll")]
internal static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, ref int pvAttribute, int cbAttribute);
internal static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, ref nint pvAttribute, int cbAttribute);

[DllImport("dwmapi.dll")]
internal static extern int DwmGetColorizationColor(out uint pcrColorization, [MarshalAs(UnmanagedType.Bool)] out bool pfOpaqueBlend);
Expand Down
94 changes: 94 additions & 0 deletions src/Lib/MCART.Windows/PInvoke/Structs/ColorRef.cs
@@ -0,0 +1,94 @@
// COLORREF.cs
//
// This file is part of Morgan's CLR Advanced Runtime (MCART)
//
// Author(s):
// César Andrés Morgan <xds_xps_ivx@hotmail.com>
//
// Released under the MIT License (MIT)
// Copyright © 2011 - 2023 César Andrés Morgan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the “Software”), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Runtime.InteropServices;

namespace TheXDS.MCART.PInvoke.Structs;

[StructLayout(LayoutKind.Explicit, Size = 4)]
internal struct ColorRef
{
[FieldOffset(0)] private uint Value;

[FieldOffset(0)] public byte R;

[FieldOffset(1)] public byte G;

[FieldOffset(2)] public byte B;

[FieldOffset(3)] public byte A;

/// <summary>Initializes a new instance of the <see cref="ColorRef"/> struct.</summary>
/// <param name="r">The intensity of the red color.</param>
/// <param name="g">The intensity of the green color.</param>
/// <param name="b">The intensity of the blue color.</param>
public ColorRef(byte r, byte g, byte b)
{
Value = 0;
A = 0;
R = r;
G = g;
B = b;
}

/// <summary>A method to darken a color by a percentage of the difference between the color and Black.</summary>
/// <param name="percent">The percentage by which to darken the original color.</param>
/// <returns>
/// The return color's Alpha value will be unchanged, but the RGB content will have been increased by the specified percentage. If
/// percent is 100 then the returned Color will be Black with original Alpha.
/// </returns>
public ColorRef Darken(float percent)
{
if (percent < 0 || percent > 1.0)
throw new ArgumentOutOfRangeException(nameof(percent));

return new(Conv(R), Conv(G), Conv(B)) { Value = Value };

byte Conv(byte c) => (byte)(c - (int)(c * percent));
}

/// <summary>A method to lighten a color by a percentage of the difference between the color and Black.</summary>
/// <param name="percent">The percentage by which to lighten the original color.</param>
/// <returns>
/// The return color's Alpha value will be unchanged, but the RGB content will have been decreased by the specified percentage. If
/// percent is 100 then the returned Color will be White with original Alpha.
/// </returns>
public ColorRef Lighten(float percent)
{
if (percent < 0 || percent > 1.0)
throw new ArgumentOutOfRangeException(nameof(percent));

return new(Conv(R), Conv(G), Conv(B)) { Value = Value };

byte Conv(byte c) => (byte)(c + (int)((255f - c) * percent));
}

public static implicit operator TheXDS.MCART.Types.Color(ColorRef value) => new(value.R, value.G, value.B, value.A);

public static implicit operator ColorRef(TheXDS.MCART.Types.Color value) => new(value.R, value.G, value.B) { A = value.A };
}
63 changes: 63 additions & 0 deletions src/Lib/MCART.Windows/PInvoke/Structs/DWM_BLURBEHIND.cs
@@ -0,0 +1,63 @@
// COLORREF.cs
//
// This file is part of Morgan's CLR Advanced Runtime (MCART)
//
// Author(s):
// César Andrés Morgan <xds_xps_ivx@hotmail.com>
//
// Released under the MIT License (MIT)
// Copyright © 2011 - 2023 César Andrés Morgan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the “Software”), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Runtime.InteropServices;

namespace TheXDS.MCART.PInvoke.Structs;

[StructLayout(LayoutKind.Sequential)]
internal struct DWM_BLURBEHIND
{
public DWM_BLURBEHIND_Mask dwFlags;

[MarshalAs(UnmanagedType.Bool)]
public bool fEnable;

public IntPtr hRgnBlur;

[MarshalAs(UnmanagedType.Bool)]
public bool fTransitionOnMaximized;

public DWM_BLURBEHIND(bool enabled)
{
fEnable = enabled;
hRgnBlur = IntPtr.Zero;
fTransitionOnMaximized = false;
dwFlags = DWM_BLURBEHIND_Mask.DWM_BB_ENABLE;
}

public bool TransitionOnMaximized
{
readonly get => fTransitionOnMaximized;
set
{
fTransitionOnMaximized = value;
dwFlags |= DWM_BLURBEHIND_Mask.DWM_BB_TRANSITIONONMAXIMIZED;
}
}
}
39 changes: 39 additions & 0 deletions src/Lib/MCART.Windows/PInvoke/Structs/DWM_BLURBEHIND_Mask.cs
@@ -0,0 +1,39 @@
// COLORREF.cs
//
// This file is part of Morgan's CLR Advanced Runtime (MCART)
//
// Author(s):
// César Andrés Morgan <xds_xps_ivx@hotmail.com>
//
// Released under the MIT License (MIT)
// Copyright © 2011 - 2023 César Andrés Morgan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the “Software”), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

namespace TheXDS.MCART.PInvoke.Structs;

[Flags]
internal enum DWM_BLURBEHIND_Mask
{
DWM_BB_ENABLE = 0X00000001,

DWM_BB_BLURREGION = 0X00000002,

DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004
}
43 changes: 27 additions & 16 deletions src/Lib/MCART.Windows/PInvoke/Structs/DwmWindowAttribute.cs
Expand Up @@ -30,21 +30,32 @@

namespace TheXDS.MCART.PInvoke.Structs;

internal enum DwmWindowAttribute
internal enum DwmWindowAttribute : uint
{
NCRenderingEnabled = 1,
NCRenderingPolicy,
TransitionsForceDisabled,
AllowNCPaint,
CaptionButtonBounds,
NonClientRtlLayout,
ForceIconicRepresentation,
Flip3DPolicy,
ExtendedFrameBounds,
HasIconicBitmap,
DisallowPeek,
ExcludedFromPeek,
Cloak,
Cloaked,
FreezeRepresentation
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_PASSIVE_UPDATE_MODE,
DWMWA_USE_HOSTBACKDROPBRUSH,
DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
DWMWA_WINDOW_CORNER_PREFERENCE = 33,
DWMWA_BORDER_COLOR,
DWMWA_CAPTION_COLOR,
DWMWA_TEXT_COLOR,
DWMWA_VISIBLE_FRAME_BORDER_THICKNESS,
DWMWA_SYSTEMBACKDROP_TYPE,
DWMWA_LAST,
DWMWA_MICA = 1029,
}
6 changes: 6 additions & 0 deletions src/Lib/MCART.Windows/PInvoke/Structs/Margins.cs
Expand Up @@ -57,4 +57,10 @@ public struct Margins
/// Describe el margen inferior.
/// </summary>
public int Bottom;

/// <summary>
/// Inicialiiza una nueva instancia de la estructura <see cref="Margins"/>.
/// </summary>
/// <param name="allMargins"></param>
public Margins(int allMargins) => Left = Right = Top = Bottom = allMargins;
}

0 comments on commit be57267

Please sign in to comment.