Example #1
0
 /// <summary>
 /// Gets the specified window attribute from the Desktop Window Manager (DWM).
 /// </summary>
 /// <typeparam name="T">Return type. Must match the attribute.</typeparam>
 /// <param name="window">The window.</param>
 /// <param name="attribute">The attribute.</param>
 /// <returns>Value of the windows attribute.</returns>
 public static T GetWindowAttribute <T>(this IWin32Window window, GetWindowAttr attribute) where T : struct
 {
     if (window == null)
     {
         throw new ArgumentNullException(nameof(window));
     }
     using (var ptr = SafeHGlobalHandle.AllocHGlobal <T>())
     {
         NativeMethods.DwmGetWindowAttribute(window.Handle, (NativeMethods.DWMWINDOWATTRIBUTE)attribute, ptr, ptr.Size);
         return(ptr.ToStructure <T>());
     }
 }
Example #2
0
 /// <summary>
 /// Sets the specified window attribute through the Desktop Window Manager (DWM).
 /// </summary>
 /// <param name="window">The window.</param>
 /// <param name="attribute">The attribute.</param>
 /// <param name="value">The value.</param>
 public static void SetWindowAttribute(this IWin32Window window, SetWindowAttr attribute, object value)
 {
     if (window == null)
     {
         throw new ArgumentNullException(nameof(window));
     }
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
     using (var ptr = new SafeHGlobalHandle(value))
         NativeMethods.DwmSetWindowAttribute(window.Handle, (NativeMethods.DWMWINDOWATTRIBUTE)attribute, ptr, ptr.Size);
 }
        /// <summary>
        /// Allocates from unmanaged memory to represent a structure with a
        /// variable length array at the end and marshal these structure
        /// elements. It is the callers responsibility to marshal what precedes
        /// the trailing array into the unmanaged memory. ONLY structures with
        /// attribute StructLayout of LayoutKind.Sequential are supported.
        /// </summary>
        /// <typeparam name="T">Type of the trailing array of structures</typeparam>
        /// <param name="prefixBytes">Number of bytes preceding the trailing array of structures</param>
        /// <param name="values">Collection of structure objects</param>
        /// <param name="count">Number of items in <paramref name="values"/>.</param>
        /// <returns>SafeHGlobalHandle object to an native (unmanaged) structure with a trail array of structures</returns>
        public static SafeHGlobalHandle AllocHGlobal <T>(int prefixBytes, IEnumerable <T> values, int count) where T : struct
        {
            Debug.Assert(typeof(T).StructLayoutAttribute?.Value == LayoutKind.Sequential);

            var result = new SafeHGlobalHandle(prefixBytes + Marshal.SizeOf(typeof(T)) * count);
            var ptr    = new IntPtr(result.handle.ToInt32() + prefixBytes);

            foreach (var value in values)
            {
                Marshal.StructureToPtr(value, ptr, false);
                ptr = new IntPtr(ptr.ToInt32() + Marshal.SizeOf(typeof(T)));
            }
            return(result);
        }