using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace SharpDX.DirectComposition
{
partial class DesktopDevice
{
///
/// Creates a new device object that can be used to create other Microsoft DirectComposition objects.
///
/// An optional reference to a DirectX device to be used to create DirectComposition surface objects. Must be a reference to an object implementing the or interfaces.
public DesktopDevice(ComObject renderingDevice)
: base(IntPtr.Zero)
{
IntPtr temp;
DComp.CreateDevice2(renderingDevice, Utilities.GetGuidFromType(typeof(DesktopDevice)), out temp);
NativePointer = temp;
}
private static class NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int index);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
public static extern int SetLayeredWindowAttributes(IntPtr hwnd, int color, byte alpha, int flags);
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int pvAttr, int attrSize);
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(IntPtr hwnd, int attr, ref int pvAttr, int attrSize);
public const int GWL_EXSTYLE = -20;
public const int WS_EX_LAYERED = 0x00080000;
public const int LWA_ALPHA = 2;
public const int DWMWA_CLOAK = 13;
}
///
/// Sets the DWM cloaking state for the given window.
///
/// A window handle.
/// True to cloak the window, false to make the window visible again.
///
/// When a window is used as a source in a DirectComposition tree, by default it will still be rendered in its original position and
/// layout. Cloaking the window causes it to disappear from its original position and render only through the DirectComposition tree.
///
public static void SetWindowIsCloaked(IntPtr hwnd, bool cloaked)
{
int value = 1;
int result = NativeMethods.DwmSetWindowAttribute(hwnd, NativeMethods.DWMWA_CLOAK, ref value, sizeof(int));
if (result != 0)
throw new Win32Exception(result);
}
///
/// Gets the cloaking state for a given window.
///
/// A window handle.
/// True if the window is currently being cloaked, false if it is visible in its regular position.
public static bool GetWindowIsCloaked(IntPtr hwnd)
{
int value = 1;
int result = NativeMethods.DwmGetWindowAttribute(hwnd, NativeMethods.DWMWA_CLOAK, ref value, sizeof(int));
if (result != 0)
throw new Win32Exception(result);
return value != 0;
}
///
/// Sets or clears the WS_EX_LAYERED extended style for a window.
///
/// A window handle.
/// True to apply the layered style to the window, false to clear the style.
///
/// Only windows with the WS_EX_LAYERED style can be used as Visual content. Starting in Windows 8, child windows can also be layered windows.
///
public static void SetWindowIsLayered(IntPtr hwnd, bool isLayered)
{
#if NET45
if (Environment.Is64BitProcess)
#else
if(IntPtr.Size == 8)
#endif
{
IntPtr exStyle = NativeMethods.GetWindowLongPtr(hwnd, NativeMethods.GWL_EXSTYLE);
if (exStyle == IntPtr.Zero)
throw new Win32Exception();
exStyle = NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GWL_EXSTYLE, (IntPtr)(exStyle.ToInt64() | NativeMethods.WS_EX_LAYERED));
if (exStyle == IntPtr.Zero)
throw new Win32Exception();
}
else
{
int exStyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_EXSTYLE);
if (exStyle == 0)
throw new Win32Exception();
exStyle = NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_EXSTYLE, exStyle | NativeMethods.WS_EX_LAYERED);
if (exStyle == 0)
throw new Win32Exception();
}
}
///
/// Gets whether a given window has the WS_EX_LAYERED extended style applied.
///
/// A window handle.
/// True if the window has the layered style applied, false otherwise.
public static bool GetWindowIsLayered(IntPtr hwnd)
{
#if NET45
if (Environment.Is64BitProcess)
#else
if (IntPtr.Size == 8)
#endif
{
IntPtr exStyle = NativeMethods.GetWindowLongPtr(hwnd, NativeMethods.GWL_EXSTYLE);
if (exStyle == IntPtr.Zero)
throw new Win32Exception();
return (int)(exStyle.ToInt64() & NativeMethods.WS_EX_LAYERED) == NativeMethods.WS_EX_LAYERED;
}
else
{
int exStyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_EXSTYLE);
if (exStyle == 0)
throw new Win32Exception();
return (exStyle & NativeMethods.WS_EX_LAYERED) == NativeMethods.WS_EX_LAYERED;
}
}
}
}