Moved direct WinAPI access into a service

This commit is contained in:
Anton Kasyanov
2018-02-03 21:09:15 +02:00
parent 2e6d3ffb4d
commit 16f934df62
18 changed files with 299 additions and 170 deletions

View File

@@ -0,0 +1,76 @@
using System;
using EveOPreview.DwmInterop;
namespace EveOPreview.DwmAPI
{
class DwmThumbnail : IDwmThumbnail
{
#region Private fields
private readonly IWindowManager _windowManager;
private IntPtr _handle;
private DWM_THUMBNAIL_PROPERTIES _properties;
#endregion
public DwmThumbnail(IWindowManager windowManager)
{
this._windowManager = windowManager;
}
public void Register(IntPtr destination, IntPtr source)
{
this._properties = new DWM_THUMBNAIL_PROPERTIES();
this._properties.dwFlags = DWM_TNP_CONSTANTS.DWM_TNP_VISIBLE
+ DWM_TNP_CONSTANTS.DWM_TNP_OPACITY
+ DWM_TNP_CONSTANTS.DWM_TNP_RECTDESTINATION
+ DWM_TNP_CONSTANTS.DWM_TNP_SOURCECLIENTAREAONLY;
this._properties.opacity = 255;
this._properties.fVisible = true;
this._properties.fSourceClientAreaOnly = true;
if (!this._windowManager.IsCompositionEnabled)
{
return;
}
this._handle = DwmApiNativeMethods.DwmRegisterThumbnail(destination, source);
}
public void Unregister()
{
if (!this._windowManager.IsCompositionEnabled)
{
return;
}
try
{
DwmApiNativeMethods.DwmUnregisterThumbnail(this._handle);
}
catch (ArgumentException)
{
}
}
public void Move(int left, int top, int right, int bottom)
{
this._properties.rcDestination = new RECT(left, top, right, bottom);
}
public void Update()
{
if (!this._windowManager.IsCompositionEnabled)
{
return;
}
try
{
DwmApiNativeMethods.DwmUpdateThumbnailProperties(this._handle, this._properties);
}
catch (ArgumentException)
{
//This exception will be thrown if the EVE client disappears while this method is running
}
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
using EveOPreview.DwmInterop;
namespace EveOPreview.DwmAPI
{
class WindowManager : IWindowManager
{
public WindowManager()
{
this.IsCompositionEnabled = DwmApiNativeMethods.DwmIsCompositionEnabled();
}
public bool IsCompositionEnabled { get; }
public IntPtr GetForegroundWindowHandle()
{
return User32NativeMethods.GetForegroundWindow();
}
public void ActivateWindow(IntPtr handle)
{
User32NativeMethods.SetForegroundWindow(handle);
int style = User32NativeMethods.GetWindowLong(handle, InteropConstants.GWL_STYLE);
if ((style & InteropConstants.WS_MINIMIZE) == InteropConstants.WS_MINIMIZE)
{
User32NativeMethods.ShowWindowAsync(handle, InteropConstants.SW_SHOWNORMAL);
}
}
public void DeactivateWindow(IntPtr handle)
{
User32NativeMethods.SendMessage(handle, InteropConstants.WM_SYSCOMMAND, InteropConstants.SC_MINIMIZE, 0);
}
public void MoveWindow(IntPtr handle, int left, int top, int width, int height)
{
User32NativeMethods.MoveWindow(handle, left, top, width, height, true);
}
public void GetWindowCoordinates(IntPtr handle, out int left, out int top, out int right, out int bottom)
{
User32NativeMethods.GetWindowRect(handle, out RECT windowRectangle);
left = windowRectangle.Left;
top = windowRectangle.Top;
right = windowRectangle.Right;
bottom = windowRectangle.Bottom;
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
namespace EveOPreview
{
public interface IDwmThumbnail
{
void Register(IntPtr destination, IntPtr source);
void Unregister();
void Move(int left, int top, int right, int bottom);
void Update();
}
}

View File

@@ -0,0 +1,17 @@
using System;
namespace EveOPreview
{
public interface IWindowManager
{
bool IsCompositionEnabled { get; }
IntPtr GetForegroundWindowHandle();
void ActivateWindow(IntPtr handle);
void DeactivateWindow(IntPtr handle);
void MoveWindow(IntPtr handle, int left, int top, int width, int height);
void GetWindowCoordinates(IntPtr handle, out int left, out int top, out int right, out int bottom);
}
}

View File

@@ -1,66 +1,9 @@
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System;
namespace EveOPreview
{
// Desktop Windows Manager APIs
static class WindowManagerNativeMethods
public static class InteropConstants
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr window);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmEnableBlurBehindWindow(IntPtr hWnd, DWM_BLURBEHIND pBlurBehind);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, MARGINS pMargins);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmGetColorizationColor(
out int pcrColorization,
[MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmEnableComposition(bool bEnable);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern IntPtr DwmRegisterThumbnail(IntPtr dest, IntPtr source);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmUnregisterThumbnail(IntPtr hThumbnail);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmUpdateThumbnailProperties(IntPtr hThumbnail, DWM_THUMBNAIL_PROPERTIES props);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmQueryThumbnailSourceSize(IntPtr hThumbnail, out Size size);
public const int SW_SHOWNORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public const int GWL_ID = (-12);
public const int GWL_STYLE = (-16);
public const int GWL_EXSTYLE = (-20);
@@ -129,10 +72,11 @@ namespace EveOPreview
public const int SIZE_MAXSHOW = 3;
public const int SIZE_MAXHIDE = 4;
[DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hwnd, out RECT rect);
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
public const int SW_SHOWNORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
}
}

View File

@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;
namespace EveOPreview
namespace EveOPreview.DwmInterop
{
[StructLayout(LayoutKind.Sequential)]
class DWM_BLURBEHIND

View File

@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;
namespace EveOPreview
namespace EveOPreview.DwmInterop
{
[StructLayout(LayoutKind.Sequential)]
class DWM_THUMBNAIL_PROPERTIES

View File

@@ -1,4 +1,4 @@
namespace EveOPreview
namespace EveOPreview.DwmInterop
{
static class DWM_TNP_CONSTANTS
{

View File

@@ -0,0 +1,38 @@
using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace EveOPreview.DwmInterop
{
static class DwmApiNativeMethods
{
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmEnableBlurBehindWindow(IntPtr hWnd, DWM_BLURBEHIND pBlurBehind);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, MARGINS pMargins);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmGetColorizationColor(
out int pcrColorization,
[MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmEnableComposition(bool bEnable);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern IntPtr DwmRegisterThumbnail(IntPtr dest, IntPtr source);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmUnregisterThumbnail(IntPtr hThumbnail);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmUpdateThumbnailProperties(IntPtr hThumbnail, DWM_THUMBNAIL_PROPERTIES props);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmQueryThumbnailSourceSize(IntPtr hThumbnail, out Size size);
}
}

View File

@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;
namespace EveOPreview
namespace EveOPreview.DwmInterop
{
[StructLayout(LayoutKind.Sequential)]
class MARGINS

View File

@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;
namespace EveOPreview
namespace EveOPreview.DwmInterop
{
[StructLayout(LayoutKind.Sequential)]
struct RECT

View File

@@ -0,0 +1,32 @@
using System;
using System.Runtime.InteropServices;
namespace EveOPreview.DwmInterop
{
static class User32NativeMethods
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr window);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hwnd, out RECT rect);
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
}