Skip to content

Commit

Permalink
Merge pull request #3 from RussKie/theming
Browse files Browse the repository at this point in the history
Add UxTheme.GetCurrentThemeName
  • Loading branch information
JeremyKuhne authored Mar 5, 2021
2 parents 29e4c47 + 7292ad2 commit 6e5f832
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 57 deletions.
23 changes: 23 additions & 0 deletions src/Tests/WInterop.Tests/Theming/ThemesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using FluentAssertions;
using WInterop.Windows;
using Xunit;

namespace WindowsTests
{
public class ThemesTests
{
[Fact]
public unsafe void GetCurrentThemeName()
{
string themeName = Themes.GetCurrentThemeName();

// Name would be something like: C:\WINDOWS\resources\themes\Aero\Aero.msstyles

themeName.Length.Should().BeGreaterThan(0);
themeName.Should().EndWith(".msstyles");
}
}
}
24 changes: 24 additions & 0 deletions src/WInterop.Desktop/Windows/Native/WindowsImports.Kernel32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.InteropServices;

namespace WInterop.Windows.Native
{
public static partial class WindowsImports
{
// https://msdn.microsoft.com/library/windows/desktop/ms679277.aspx
[DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
public static extern bool Beep(
uint dwFreq,
uint dwDurations);

// https://docs.microsoft.com/windows/win32/api/winbase/nf-winbase-muldiv
[SuppressGCTransition]
[DllImport(Libraries.Kernel32, ExactSpelling = true)]
public static extern int MulDiv(
int nNumber,
int nNumerator,
int nDenominator);
}
}
57 changes: 57 additions & 0 deletions src/WInterop.Desktop/Windows/Native/WindowsImports.UxTheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Jeremy W. Kuhne. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;
using WInterop.Errors;
using WInterop.Support;

namespace WInterop.Windows.Native
{
public static partial class WindowsImports
{
// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-closethemedata
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern HResult CloseThemeData(
HTHEME hTheme);

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-getcurrentthemename
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static unsafe extern HResult GetCurrentThemeName(
char* pszThemeFileName,
int cchMaxNameChars,
char* pszColorBuff,
int cchMaxColorChars,
char* pszSizeBuff,
int cchMaxSizeChars);

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-isappthemed
[SuppressGCTransition]
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsAppThemed();

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-iscompositionactive
[SuppressGCTransition]
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsCompositionActive();

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-isthemeactive
[SuppressGCTransition]
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsThemeActive();

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-isthemepartdefined
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsThemePartDefined(
HTHEME hTheme,
int iPartId,
int iStateId);

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-openthemedataex
[DllImport(Libraries.UxTheme, ExactSpelling = true, CharSet = CharSet.Unicode)]
public static unsafe extern HTHEME OpenThemeDataEx(
HWND hwnd,
char* pszClassList,
uint dwFlags);
}
}
48 changes: 0 additions & 48 deletions src/WInterop.Desktop/Windows/Native/WindowsImports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,12 +1309,6 @@ public static extern unsafe IntBoolean TrackPopupMenuEx(
HWND hwnd,
TPMPARAMS* lptpm);

// https://msdn.microsoft.com/library/windows/desktop/ms679277.aspx
[DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
public static extern bool Beep(
uint dwFreq,
uint dwDurations);

// https://msdn.microsoft.com/library/windows/desktop/ms679277.aspx
[DllImport(Libraries.User32, SetLastError = true, ExactSpelling = true)]
public static extern bool MessageBeep(
Expand Down Expand Up @@ -1373,47 +1367,5 @@ public static extern IntBoolean SetProcessDpiAwarenessContext(
// https://docs.microsoft.com/windows/win32/api/winuser/nf-winuser-getdpifromdpiawarenesscontext
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern uint GetDpiFromDpiAwarenessContext(DpiAwarenessContext value);

// https://docs.microsoft.com/windows/win32/api/winbase/nf-winbase-muldiv
[SuppressGCTransition]
[DllImport(Libraries.Kernel32, ExactSpelling = true)]
public static extern int MulDiv(
int nNumber,
int nNumerator,
int nDenominator);

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-isappthemed
[SuppressGCTransition]
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsAppThemed();

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-iscompositionactive
[SuppressGCTransition]
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsCompositionActive();

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-isthemeactive
[SuppressGCTransition]
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsThemeActive();

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-isthemepartdefined
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern IntBoolean IsThemePartDefined(
HTHEME hTheme,
int iPartId,
int iStateId);

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-openthemedataex
[DllImport(Libraries.UxTheme, ExactSpelling = true, CharSet = CharSet.Unicode)]
public static unsafe extern HTHEME OpenThemeDataEx(
HWND hwnd,
char* pszClassList,
uint dwFlags);

// https://docs.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-closethemedata
[DllImport(Libraries.UxTheme, ExactSpelling = true)]
public static extern HResult CloseThemeData(
HTHEME hTheme);
}
}
20 changes: 11 additions & 9 deletions src/WInterop.Desktop/Windows/Windows.Theming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Buffers;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WInterop.Errors;
using WInterop.Gdi;
using WInterop.Globalization;
using WInterop.Modules;
using WInterop.Support;
using WInterop.Support.Buffers;
using WInterop.Windows.Native;

namespace WInterop.Windows
{
public static partial class Windows
{
public static unsafe string GetCurrentThemeName()
{
Span<char> themeName = stackalloc char[Paths.MaxPath];
fixed (char* theme = themeName)
{
WindowsImports.GetCurrentThemeName(theme, Paths.MaxPath, null, 0, null, 0).ThrowIfFailed();
}

return themeName.SliceAtNull().ToString();
}

public static bool IsAppThemed() => WindowsImports.IsAppThemed();

public static bool IsCompositionActive() => WindowsImports.IsCompositionActive();
Expand Down

0 comments on commit 6e5f832

Please sign in to comment.