Skip to content

Commit

Permalink
Improve PInvoke WinApi and add DPI Support
Browse files Browse the repository at this point in the history
  • Loading branch information
memoarfaa committed Nov 27, 2022
1 parent f6a955a commit c4bddaf
Show file tree
Hide file tree
Showing 93 changed files with 4,691 additions and 3,010 deletions.
19 changes: 11 additions & 8 deletions SkinFramWorkCore.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32929.386
# Visual Studio Version 17
VisualStudioVersion = 17.4.33103.184
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkinFramWorkCore", "SkinFramWorkCore\SkinFramWorkCore.csproj", "{2D1532E1-CB47-467C-AB40-3C910A0100AE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkinFramWorkCore", "SkinFramWorkCore\SkinFramWorkCore.csproj", "{2D1532E1-CB47-467C-AB40-3C910A0100AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp.csproj", "{28D26C77-C889-4603-9F5C-8234A8C0690E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp1", "TestApp1\TestApp1.csproj", "{12A05D1D-6EFC-49D0-9C58-28DBE9EE743B}"
ProjectSection(ProjectDependencies) = postProject
{2D1532E1-CB47-467C-AB40-3C910A0100AE} = {2D1532E1-CB47-467C-AB40-3C910A0100AE}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -17,10 +20,10 @@ Global
{2D1532E1-CB47-467C-AB40-3C910A0100AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D1532E1-CB47-467C-AB40-3C910A0100AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D1532E1-CB47-467C-AB40-3C910A0100AE}.Release|Any CPU.Build.0 = Release|Any CPU
{28D26C77-C889-4603-9F5C-8234A8C0690E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28D26C77-C889-4603-9F5C-8234A8C0690E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28D26C77-C889-4603-9F5C-8234A8C0690E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28D26C77-C889-4603-9F5C-8234A8C0690E}.Release|Any CPU.Build.0 = Release|Any CPU
{12A05D1D-6EFC-49D0-9C58-28DBE9EE743B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12A05D1D-6EFC-49D0-9C58-28DBE9EE743B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12A05D1D-6EFC-49D0-9C58-28DBE9EE743B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12A05D1D-6EFC-49D0-9C58-28DBE9EE743B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 0 additions & 12 deletions SkinFramWorkCore/CaptionButton.cs

This file was deleted.

68 changes: 0 additions & 68 deletions SkinFramWorkCore/CaptionButtons.cs

This file was deleted.

2 changes: 1 addition & 1 deletion SkinFramWorkCore/DebugConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Runtime.InteropServices;
namespace SkinFramWorkCore
{
public class DebugConsole
internal class DebugConsole
{
#region Methods
/// <summary>
Expand Down
10 changes: 0 additions & 10 deletions SkinFramWorkCore/DwmButtonState.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 System;
using System.Runtime.InteropServices;
internal static partial class Interop
{
internal static partial class Dwmapi
{
[DllImport(Libraries.Dwmapi, CharSet = CharSet.Auto, ExactSpelling = false)]
public static extern HRESULT DwmExtendFrameIntoClientArea(IntPtr hWnd, ref UxTheme.MARGINS pMarInset);
public static HRESULT DwmExtendFrameIntoClientArea(IHandle hWnd, ref UxTheme.MARGINS pMarInset)
{
var result = DwmExtendFrameIntoClientArea(hWnd.Handle, ref pMarInset);
GC.KeepAlive(hWnd);
return result;
}
}
}
13 changes: 13 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.CreateRoundRectRgn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 System.Runtime.InteropServices;
internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32)]
public static extern HRGN CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
}
}
14 changes: 14 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.CreateSolidBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, SetLastError = true)]
public static extern HBRUSH CreateSolidBrush(int crColor);
}
}
16 changes: 16 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.DeleteObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 System.Runtime.InteropServices;
using System;
internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public static extern BOOL DeleteObject(IntPtr hObject);

public static BOOL DeleteObject(HGDIOBJ hObject) => DeleteObject(hObject.Handle);
}
}
13 changes: 13 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.ExcludeClipRect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 System.Runtime.InteropServices;
internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32)]
public static extern int ExcludeClipRect(HDC hdc, int left, int top, int right, int bottom);
}
}
23 changes: 23 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.GetObjectType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Gdi32
{
// This does set last error but *only* in one error case that doesn't add any real value.
[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public static extern OBJ GetObjectType(HGDIOBJ h);

public static OBJ GetObjectType(HandleRef h)
{
OBJ result = GetObjectType((HGDIOBJ)h.Handle);
GC.KeepAlive(h.Wrapper);
return result;
}
}
}
14 changes: 14 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.GetRegionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 System.Runtime.InteropServices;
using System;
internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public static extern uint GetRegionData(IntPtr hRgn, uint nCount, IntPtr lpRgnData);
}
}
23 changes: 23 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.HBRUSH.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 System;
internal static partial class Interop
{
internal static partial class Gdi32
{
public readonly struct HBRUSH
{
public IntPtr Handle { get; }

public HBRUSH(IntPtr handle) => Handle = handle;

public bool IsNull => Handle == IntPtr.Zero;

public static explicit operator IntPtr(HBRUSH hbrush) => hbrush.Handle;
public static explicit operator HBRUSH(IntPtr hbrush) => new HBRUSH(hbrush);
public static implicit operator HGDIOBJ(HBRUSH hbrush) => new HGDIOBJ(hbrush.Handle);
public static explicit operator HBRUSH(HGDIOBJ hbrush) => new HBRUSH(hbrush.Handle);
}
}
}
29 changes: 29 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.HDC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 System;

internal static partial class Interop
{
internal static partial class Gdi32
{
public readonly struct HDC
{
public IntPtr Handle { get; }

public HDC(IntPtr handle) => Handle = handle;

public bool IsNull => Handle == IntPtr.Zero;

public static explicit operator IntPtr(HDC hdc) => hdc.Handle;
public static explicit operator HDC(IntPtr hdc) => new HDC(hdc);
public static implicit operator HGDIOBJ(HDC hdc) => new HGDIOBJ(hdc.Handle);

public static bool operator ==(HDC value1, HDC value2) => value1.Handle == value2.Handle;
public static bool operator !=(HDC value1, HDC value2) => value1.Handle != value2.Handle;
public override bool Equals(object? obj) => obj is HDC hdc && hdc.Handle == Handle;
public override int GetHashCode() => Handle.GetHashCode();
}
}
}
30 changes: 30 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.HGDIOBJ.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 System;

internal static partial class Interop
{
internal static partial class Gdi32
{
public struct HGDIOBJ
{
public IntPtr Handle { get; }

public HGDIOBJ(IntPtr handle) => Handle = handle;

public bool IsNull => Handle == IntPtr.Zero;

public static explicit operator IntPtr(HGDIOBJ hgdiobj) => hgdiobj.Handle;
public static explicit operator HGDIOBJ(IntPtr hgdiobj) => new HGDIOBJ(hgdiobj);

public static bool operator ==(HGDIOBJ value1, HGDIOBJ value2) => value1.Handle == value2.Handle;
public static bool operator !=(HGDIOBJ value1, HGDIOBJ value2) => value1.Handle != value2.Handle;
public override bool Equals(object? obj) => obj is HGDIOBJ hgdiobj && hgdiobj.Handle == Handle;
public override int GetHashCode() => Handle.GetHashCode();

public OBJ ObjectType => GetObjectType(this);
}
}
}
47 changes: 47 additions & 0 deletions SkinFramWorkCore/Interop/Gdi32/Interop.HRGN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 System.Buffers;
using System;
internal static partial class Interop
{
internal static partial class Gdi32
{
public readonly struct HRGN
{
public IntPtr Handle { get; }

public HRGN(IntPtr handle) => Handle = handle;

public bool IsNull => Handle == IntPtr.Zero;

public static implicit operator HGDIOBJ(HRGN hrgn) => new HGDIOBJ(hrgn.Handle);
public static explicit operator IntPtr(HRGN hrgn) => hrgn.Handle;
public static explicit operator HRGN(IntPtr hrgn) => new HRGN(hrgn);

public unsafe RECT[] GetRegionRects()
{
uint regionDataSize = GetRegionData(Handle, 0, IntPtr.Zero);
if (regionDataSize == 0)
{
return Array.Empty<RECT>();
}

byte[] buffer = ArrayPool<byte>.Shared.Rent((int)regionDataSize);

fixed (byte* b = buffer)
{
if (GetRegionData(Handle, regionDataSize, (IntPtr)b) != regionDataSize)
{
return Array.Empty<RECT>();
}

RECT[] result = RGNDATAHEADER.GetRegionRects((RGNDATAHEADER*)b);
ArrayPool<byte>.Shared.Return(buffer);
return result;
}
}
}
}
}
Loading

0 comments on commit c4bddaf

Please sign in to comment.