using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Vanara.PInvoke;
using static Vanara.PInvoke.Gdi32;
using static Vanara.PInvoke.UxTheme;
namespace Vanara.Extensions
{
public static partial class VisualStylesRendererExtension
{
/// Retrieves the value of a MARGINS property.
/// The visual style to query.
/// A device context for any font selection. This value can be .
/// The property to retrieve.
/// The margins defined for the property.
public static Padding GetMargins2(this VisualStyleRenderer rnd, IDeviceContext dc = null, MarginProperty prop = MarginProperty.ContentMargins)
{
using (var hdc = new SafeHDC(dc))
{
GetThemeMargins(rnd.GetSafeHandle(), hdc, rnd.Part, rnd.State, (int)prop, null, out MARGINS m);
return new Padding(m.cxLeftWidth, m.cyTopHeight, m.cxRightWidth, m.cyBottomHeight);
}
}
/// Gets the duration of the specified transition.
/// The visual style to query.
/// State ID of the part after the transition.
/// State ID of the part before the transition.
/// The transition duration, in milliseconds.
public static uint GetTransitionDuration(this VisualStyleRenderer rnd, int toState, int fromState = 0)
{
GetThemeTransitionDuration(rnd.GetSafeHandle(), rnd.Part, fromState == 0 ? rnd.State : fromState, toState, (int)ThemeProperty.TMT_TRANSITIONDURATIONS, out var dwDuration);
return dwDuration;
}
/// Gets the transition matrix for a visual style.
/// The visual style to query.
/// A two dimensional array that represents the transition durations, in milliseconds, between any two parts.
public static int[,] GetTransitionMatrix(this VisualStyleRenderer rnd)
{
var res = GetThemeIntList(rnd.GetSafeHandle(), rnd.Part, rnd.State, (int)ThemeProperty.TMT_TRANSITIONDURATIONS);
if (res == null || res.Length == 0) return null;
var dim = res[0];
var ret = new int[dim, dim];
for (var i = 0; i < dim; i++)
for (var j = 0; j < dim; j++)
ret[i, j] = res[i*dim + j + 1];
return ret;
}
/// Determines whether a different part is defined for this visual theme.
/// The visual style to query.
/// The part ID to consider.
/// true if the part is defined; otherwise, false.
public static bool IsPartDefined(this VisualStyleRenderer rnd, int part) => IsThemePartDefined(rnd.GetSafeHandle(), part, 0);
/// Prevents the application of visual styling for this specific window or control.
/// The window or control.
public static void PreventVisualStyling(this IWin32Window window) => SetWindowTheme(window, " ", new[] { " " });
///
/// Sets the state of the .
///
/// The instance.
/// The state.
public static void SetState(this VisualStyleRenderer rnd, int state) { rnd.SetParameters(rnd.Class, rnd.Part, state); }
/// Sets the window theme.
/// The window on which to apply the theme.
/// Name of the sub application. This is the theme name (e.g. "Explorer").
/// The sub identifier list. This can be left null.
public static void SetWindowTheme(this IWin32Window window, string subAppName, string[] subIdList = null)
{
var idl = subIdList == null ? null : string.Join(";", subIdList);
try { UxTheme.SetWindowTheme(window.Handle, subAppName, idl); } catch { }
}
/// Sets attributes to control how visual styles are applied to a specified window.
/// The window.
/// The attributes to apply or disable.
/// if set to true enable the attribute, otherwise disable it.
public static void SetWindowThemeAttribute(this IWin32Window window, WTNCA attr, bool enable = true)
{
try { UxTheme.SetWindowThemeNonClientAttributes(window.Handle, attr, enable); }
catch (EntryPointNotFoundException) { }
}
private static SafeHTHEME GetSafeHandle(this VisualStyleRenderer rnd) => new SafeHTHEME(rnd.Handle, false);
}
}