2014-03-22 15:02:28 +08:00
|
|
|
|
using System;
|
2015-10-08 06:52:52 +08:00
|
|
|
|
using System.Drawing;
|
2014-03-22 15:02:28 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Windows;
|
2015-10-08 06:52:52 +08:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Windows.Interop;
|
2014-03-22 15:02:28 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Helper
|
|
|
|
|
{
|
|
|
|
|
public class WindowIntelopHelper
|
|
|
|
|
{
|
|
|
|
|
private const int GWL_STYLE = -16; //WPF's Message code for Title Bar's Style
|
|
|
|
|
private const int WS_SYSMENU = 0x80000; //WPF's Message code for System Menu
|
2015-10-08 06:52:52 +08:00
|
|
|
|
private static IntPtr _hwnd_shell;
|
|
|
|
|
private static IntPtr _hwnd_desktop;
|
2015-10-15 00:41:45 +08:00
|
|
|
|
|
2015-10-08 06:52:52 +08:00
|
|
|
|
//Accessors for shell and desktop handlers
|
|
|
|
|
//Will set the variables once and then will return them
|
|
|
|
|
private static IntPtr HWND_SHELL
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-10-15 00:41:45 +08:00
|
|
|
|
return _hwnd_shell != IntPtr.Zero ? _hwnd_shell : _hwnd_shell = GetShellWindow();
|
2015-10-08 06:52:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static IntPtr HWND_DESKTOP
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-10-15 00:41:45 +08:00
|
|
|
|
return _hwnd_desktop != IntPtr.Zero ? _hwnd_desktop : _hwnd_desktop = GetDesktopWindow();
|
2015-10-08 06:52:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-22 15:02:28 +08:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
|
|
|
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
|
|
|
|
|
2015-10-08 06:17:37 +08:00
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
|
|
2015-10-08 06:52:52 +08:00
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern IntPtr GetDesktopWindow();
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern IntPtr GetShellWindow();
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
|
|
|
private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
|
|
|
|
|
|
|
|
|
|
public static bool IsWindowFullscreen()
|
2015-10-08 06:17:37 +08:00
|
|
|
|
{
|
2015-10-08 06:52:52 +08:00
|
|
|
|
RECT foreWinBounds;
|
|
|
|
|
Rectangle screenBounds;
|
|
|
|
|
var hWnd = GetForegroundWindow();
|
|
|
|
|
if (!hWnd.Equals(IntPtr.Zero))
|
|
|
|
|
{
|
|
|
|
|
if (!(hWnd.Equals(HWND_DESKTOP) || hWnd.Equals(HWND_SHELL)))
|
|
|
|
|
{
|
|
|
|
|
GetWindowRect(hWnd, out foreWinBounds);
|
|
|
|
|
screenBounds = Screen.FromHandle(hWnd).Bounds;
|
|
|
|
|
if ((foreWinBounds.Bottom - foreWinBounds.Top) == screenBounds.Height && (foreWinBounds.Right - foreWinBounds.Left) == screenBounds.Width)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2015-10-08 06:17:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-22 15:02:28 +08:00
|
|
|
|
/// <summary>
|
2015-10-08 06:52:52 +08:00
|
|
|
|
/// disable windows toolbar's control box
|
|
|
|
|
/// this will also disable system menu with Alt+Space hotkey
|
2014-03-22 15:02:28 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static void DisableControlBox(Window win)
|
|
|
|
|
{
|
2015-10-08 06:52:52 +08:00
|
|
|
|
var hwnd = new WindowInteropHelper(win).Handle;
|
2014-03-22 15:02:28 +08:00
|
|
|
|
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
|
|
|
|
|
}
|
2015-10-08 06:52:52 +08:00
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
public struct RECT
|
|
|
|
|
{
|
|
|
|
|
public int Left;
|
|
|
|
|
public int Top;
|
|
|
|
|
public int Right;
|
|
|
|
|
public int Bottom;
|
|
|
|
|
}
|
2015-11-10 07:51:22 +08:00
|
|
|
|
|
2015-11-10 19:18:03 +08:00
|
|
|
|
#region Blur Handling
|
|
|
|
|
/*
|
|
|
|
|
Found on https://github.com/riverar/sample-win10-aeroglass
|
|
|
|
|
*/
|
|
|
|
|
public enum AccentState
|
2015-11-10 07:51:22 +08:00
|
|
|
|
{
|
|
|
|
|
ACCENT_DISABLED = 0,
|
|
|
|
|
ACCENT_ENABLE_GRADIENT = 1,
|
|
|
|
|
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
|
|
|
|
|
ACCENT_ENABLE_BLURBEHIND = 3,
|
|
|
|
|
ACCENT_INVALID_STATE = 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
internal struct AccentPolicy
|
|
|
|
|
{
|
|
|
|
|
public AccentState AccentState;
|
|
|
|
|
public int AccentFlags;
|
|
|
|
|
public int GradientColor;
|
|
|
|
|
public int AnimationId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
internal struct WindowCompositionAttributeData
|
|
|
|
|
{
|
|
|
|
|
public WindowCompositionAttribute Attribute;
|
|
|
|
|
public IntPtr Data;
|
|
|
|
|
public int SizeOfData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum WindowCompositionAttribute
|
|
|
|
|
{
|
|
|
|
|
WCA_ACCENT_POLICY = 19
|
|
|
|
|
}
|
|
|
|
|
[DllImport("user32.dll")]
|
2015-11-10 19:18:03 +08:00
|
|
|
|
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the blur for a window via SetWindowCompositionAttribute
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wind">window to blur</param>
|
|
|
|
|
/// <param name="status">true/false - on or off correspondingly</param>
|
|
|
|
|
public static void SetBlurForWindow(Window wind, bool status)
|
|
|
|
|
{
|
|
|
|
|
SetWindowAccent(wind, status ? AccentState.ACCENT_ENABLE_BLURBEHIND : AccentState.ACCENT_DISABLED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetWindowAccent(Window wind, AccentState themeAccentMode)
|
2015-11-10 07:51:22 +08:00
|
|
|
|
{
|
|
|
|
|
var windowHelper = new WindowInteropHelper(wind);
|
|
|
|
|
var accent = new AccentPolicy();
|
2015-11-10 19:18:03 +08:00
|
|
|
|
accent.AccentState = themeAccentMode;
|
2015-11-10 07:51:22 +08:00
|
|
|
|
var accentStructSize = Marshal.SizeOf(accent);
|
|
|
|
|
|
|
|
|
|
var accentPtr = Marshal.AllocHGlobal(accentStructSize);
|
|
|
|
|
Marshal.StructureToPtr(accent, accentPtr, false);
|
|
|
|
|
|
|
|
|
|
var data = new WindowCompositionAttributeData();
|
|
|
|
|
data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
|
|
|
|
|
data.SizeOfData = accentStructSize;
|
|
|
|
|
data.Data = accentPtr;
|
|
|
|
|
|
|
|
|
|
SetWindowCompositionAttribute(windowHelper.Handle, ref data);
|
|
|
|
|
|
|
|
|
|
Marshal.FreeHGlobal(accentPtr);
|
|
|
|
|
}
|
2015-11-10 19:18:03 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
2014-03-22 15:02:28 +08:00
|
|
|
|
}
|
2015-10-08 06:52:52 +08:00
|
|
|
|
}
|