Fixed problem with DwmGetWindowAttribute and DwmSetWindowAttribute helper functions not handling enum values.

pull/10/head
David Hall 2018-01-27 14:34:39 -07:00
parent 60297a9f32
commit 7678ee102c
2 changed files with 10 additions and 5 deletions

View File

@ -459,10 +459,12 @@ namespace Vanara.PInvoke
public static HRESULT DwmGetWindowAttribute<T>(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, out T pvAttribute)
{
if (!CorrespondingTypeAttribute.CanGet(dwAttribute, typeof(T))) throw new ArgumentException();
var isBool = typeof(T) == typeof(bool);
var m = new SafeCoTaskMemHandle(Marshal.SizeOf(isBool ? typeof(uint) : typeof(T)));
var type = typeof(T);
var isBool = type == typeof(bool);
type = isBool ? typeof(uint) : (type.IsEnum ? Enum.GetUnderlyingType(type) : type);
var m = new SafeCoTaskMemHandle(Marshal.SizeOf(type));
var hr = DwmGetWindowAttribute(hwnd, dwAttribute, (IntPtr)m, m.Size);
pvAttribute = isBool ? (T)Convert.ChangeType(m.ToStructure<uint>(), typeof(bool)) : m.ToStructure<T>();
pvAttribute = isBool ? (T)Convert.ChangeType(m.ToStructure<uint>(), typeof(bool)) : (T)Marshal.PtrToStructure((IntPtr)m, type);
return hr;
}
@ -618,7 +620,7 @@ namespace Vanara.PInvoke
public static HRESULT DwmSetWindowAttribute<T>(IntPtr hwnd, DWMWINDOWATTRIBUTE dwAttribute, [In] T pvAttribute)
{
if (pvAttribute == null || !CorrespondingTypeAttribute.CanSet(dwAttribute, typeof(T))) throw new ArgumentException();
var attr = pvAttribute is bool ? (object)Convert.ToUInt32(pvAttribute) : pvAttribute;
var attr = pvAttribute is bool ? Convert.ToUInt32(pvAttribute) : (pvAttribute.GetType().IsEnum ? Convert.ChangeType(pvAttribute, Enum.GetUnderlyingType(pvAttribute.GetType())) : pvAttribute);
using (var p = new PinnedObject(attr))
return DwmSetWindowAttribute(hwnd, dwAttribute, p, Marshal.SizeOf(attr));
}

View File

@ -74,7 +74,7 @@ namespace Vanara.PInvoke.Tests
err = DwmGetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_NCRENDERING_ENABLED, out bool b2);
if (err.Failed) TestContext.WriteLine($"Err:DWMWA_NCRENDERING_ENABLE={err}");
Assert.That(err.Succeeded);
err = DwmGetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, out int i2);
err = DwmGetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_CLOAKED, out DWM_CLOAKED i2);
if (err.Failed) TestContext.WriteLine($"Err:DWMWA_NCRENDERING_POLICY={err}");
Assert.That(err.Succeeded);
wnd.Hide();
@ -98,6 +98,9 @@ namespace Vanara.PInvoke.Tests
var err = DwmSetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_CLOAK, true);
if (err.Failed) TestContext.WriteLine($"Err:DWMWA_CLOAK={err}");
Assert.That(err.Succeeded);
err = DwmSetWindowAttribute(wnd.Handle, DWMWINDOWATTRIBUTE.DWMWA_NCRENDERING_POLICY, DWMNCRENDERINGPOLICY.DWMNCRP_USEWINDOWSTYLE);
if (err.Failed) TestContext.WriteLine($"Err:DWMWA_NCRENDERING_POLICY={err}");
Assert.That(err.Succeeded);
wnd.Hide();
}
}