All restore of snapped Windows

This commit is contained in:
kwlockwo 2016-08-28 14:35:21 +10:00
parent 4250eae2cb
commit ca03b6b652
17 changed files with 2019 additions and 2005 deletions

View file

@ -1,96 +1,96 @@
using System;
using NLog;
using NLog.Config;
using NLog.Targets;
namespace Ninjacrab.PersistentWindows.Common.Diagnostics
{
public class Log
{
static Log()
{
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var consoleTarget = new ColoredConsoleTarget();
config.AddTarget("console", consoleTarget);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
consoleTarget.Layout = @"${date:format=HH\\:mm\\:ss} ${logger} ${message}";
fileTarget.FileName = "${basedir}/PersistentWindows.Log";
fileTarget.Layout = "${date:format=HH\\:mm\\:ss} ${logger} ${message}";
// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Trace, consoleTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
/// <summary>
/// Occurs when something is logged. STATIC EVENT!
/// </summary>
public static event Action<LogLevel, string> LogEvent;
private static Logger _logger;
private static Logger Logger
{
get
{
if(_logger == null)
{
_logger = LogManager.GetLogger("Logger");
}
return _logger;
}
}
private static void RaiseLogEvent(LogLevel level, string message)
{
// could, should, would write a new logging target but this is brute force faster
if(LogEvent != null)
{
LogEvent(level, message);
}
}
public static void Trace(string format, params object[] args)
{
var message = Format(format, args);
Logger.Trace(message);
RaiseLogEvent(LogLevel.Trace, message);
}
public static void Info(string format, params object[] args)
{
var message = Format(format, args);
Logger.Info(Format(format, args));
RaiseLogEvent(LogLevel.Info, message);
}
public static void Error(string format, params object[] args)
{
var message = Format(format, args);
Logger.Error(Format(format, args));
RaiseLogEvent(LogLevel.Error, message);
}
/// <summary>
/// Since string.Format doesn't like args being null or having no entries.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
/// <returns></returns>
private static string Format(string format, params object[] args)
{
return args == null || args.Length == 0 ? format : string.Format(format, args);
}
}
}
using System;
using NLog;
using NLog.Config;
using NLog.Targets;
namespace Ninjacrab.PersistentWindows.Common.Diagnostics
{
public class Log
{
static Log()
{
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var consoleTarget = new ColoredConsoleTarget();
config.AddTarget("console", consoleTarget);
var fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
// Step 3. Set target properties
consoleTarget.Layout = @"${date:format=HH\\:mm\\:ss} ${logger} ${message}";
fileTarget.FileName = "${basedir}/PersistentWindows.Log";
fileTarget.Layout = "${date:format=HH\\:mm\\:ss} ${logger} ${message}";
// Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Trace, consoleTarget);
config.LoggingRules.Add(rule1);
var rule2 = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration
LogManager.Configuration = config;
}
/// <summary>
/// Occurs when something is logged. STATIC EVENT!
/// </summary>
public static event Action<LogLevel, string> LogEvent;
private static Logger _logger;
private static Logger Logger
{
get
{
if(_logger == null)
{
_logger = LogManager.GetLogger("Logger");
}
return _logger;
}
}
private static void RaiseLogEvent(LogLevel level, string message)
{
// could, should, would write a new logging target but this is brute force faster
if(LogEvent != null)
{
LogEvent(level, message);
}
}
public static void Trace(string format, params object[] args)
{
var message = Format(format, args);
Logger.Trace(message);
RaiseLogEvent(LogLevel.Trace, message);
}
public static void Info(string format, params object[] args)
{
var message = Format(format, args);
Logger.Info(Format(format, args));
RaiseLogEvent(LogLevel.Info, message);
}
public static void Error(string format, params object[] args)
{
var message = Format(format, args);
Logger.Error(Format(format, args));
RaiseLogEvent(LogLevel.Error, message);
}
/// <summary>
/// Since string.Format doesn't like args being null or having no entries.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="args">The args.</param>
/// <returns></returns>
private static string Format(string format, params object[] args)
{
return args == null || args.Length == 0 ? format : string.Format(format, args);
}
}
}

View file

@ -1,31 +1,31 @@
using System;
using Ninjacrab.PersistentWindows.Common.WinApiBridge;
namespace Ninjacrab.PersistentWindows.Common.Models
{
public class ApplicationDisplayMetrics
{
public IntPtr HWnd { get; set; }
public int ProcessId { get; set; }
public string ApplicationName { get; set; }
public WindowPlacement WindowPlacement { get; set; }
public string Key
{
get { return string.Format("{0}-{1}", HWnd.ToInt64(), ApplicationName); }
}
public bool EqualPlacement(ApplicationDisplayMetrics other)
{
return this.WindowPlacement.NormalPosition.Left == other.WindowPlacement.NormalPosition.Left
&& this.WindowPlacement.NormalPosition.Top == other.WindowPlacement.NormalPosition.Top
&& this.WindowPlacement.NormalPosition.Width == other.WindowPlacement.NormalPosition.Width
&& this.WindowPlacement.NormalPosition.Height == other.WindowPlacement.NormalPosition.Height;
}
public override string ToString()
{
return string.Format("{0}.{1} {2}", ProcessId, HWnd.ToInt64(), ApplicationName);
}
}
}
using System;
using Ninjacrab.PersistentWindows.Common.WinApiBridge;
namespace Ninjacrab.PersistentWindows.Common.Models
{
public class ApplicationDisplayMetrics
{
public IntPtr HWnd { get; set; }
public int ProcessId { get; set; }
public string ApplicationName { get; set; }
public WindowPlacement WindowPlacement { get; set; }
public string Key
{
get { return string.Format("{0}-{1}", HWnd.ToInt64(), ApplicationName); }
}
public bool EqualPlacement(ApplicationDisplayMetrics other)
{
return this.WindowPlacement.NormalPosition.Left == other.WindowPlacement.NormalPosition.Left
&& this.WindowPlacement.NormalPosition.Top == other.WindowPlacement.NormalPosition.Top
&& this.WindowPlacement.NormalPosition.Width == other.WindowPlacement.NormalPosition.Width
&& this.WindowPlacement.NormalPosition.Height == other.WindowPlacement.NormalPosition.Height;
}
public override string ToString()
{
return string.Format("{0}.{1} {2}", ProcessId, HWnd.ToInt64(), ApplicationName);
}
}
}

View file

@ -1,76 +1,76 @@
using System.Collections.Generic;
using System.Linq;
using Ninjacrab.PersistentWindows.Common.WinApiBridge;
namespace Ninjacrab.PersistentWindows.Common.Models
{
public class DesktopDisplayMetrics
{
public static DesktopDisplayMetrics AcquireMetrics()
{
DesktopDisplayMetrics metrics = new DesktopDisplayMetrics();
var displays = Display.GetDisplays();
int displayId = 0;
foreach (var display in displays)
{
metrics.SetMonitor(displayId++, display);
}
return metrics;
}
private Dictionary<int, Display> monitorResolutions = new Dictionary<int, Display>();
public int NumberOfDisplays { get { return monitorResolutions.Count; } }
public void SetMonitor(int id, Display display)
{
if (!monitorResolutions.ContainsKey(id) ||
monitorResolutions[id].ScreenWidth != display.ScreenWidth ||
monitorResolutions[id].ScreenHeight != display.ScreenHeight)
{
monitorResolutions.Add(id, display);
BuildKey();
}
}
private void BuildKey()
{
List<string> keySegments = new List<string>();
foreach(var entry in monitorResolutions.OrderBy(row => row.Key))
{
keySegments.Add(string.Format("[Id:{0} Loc:{1}x{2} Res:{3}x{4}]", entry.Key, entry.Value.Left, entry.Value.Top, entry.Value.ScreenWidth, entry.Value.ScreenHeight));
}
key = string.Join(",", keySegments);
}
private string key;
public string Key
{
get
{
return key;
}
}
public override bool Equals(object obj)
{
var other = obj as DesktopDisplayMetrics;
if (other == null)
{
return false;
}
return this.Key == other.key;
}
public override int GetHashCode()
{
return key.GetHashCode();
}
public int GetHashCode(DesktopDisplayMetrics obj)
{
return obj.key.GetHashCode();
}
}
}
using System.Collections.Generic;
using System.Linq;
using Ninjacrab.PersistentWindows.Common.WinApiBridge;
namespace Ninjacrab.PersistentWindows.Common.Models
{
public class DesktopDisplayMetrics
{
public static DesktopDisplayMetrics AcquireMetrics()
{
DesktopDisplayMetrics metrics = new DesktopDisplayMetrics();
var displays = Display.GetDisplays();
int displayId = 0;
foreach (var display in displays)
{
metrics.SetMonitor(displayId++, display);
}
return metrics;
}
private Dictionary<int, Display> monitorResolutions = new Dictionary<int, Display>();
public int NumberOfDisplays { get { return monitorResolutions.Count; } }
public void SetMonitor(int id, Display display)
{
if (!monitorResolutions.ContainsKey(id) ||
monitorResolutions[id].ScreenWidth != display.ScreenWidth ||
monitorResolutions[id].ScreenHeight != display.ScreenHeight)
{
monitorResolutions.Add(id, display);
BuildKey();
}
}
private void BuildKey()
{
List<string> keySegments = new List<string>();
foreach (var entry in monitorResolutions.OrderBy(row => row.Value.DeviceName))
{
keySegments.Add(string.Format("[DeviceName:{0} Loc:{1}x{2} Res:{3}x{4}]", entry.Value.DeviceName, entry.Value.Left, entry.Value.Top, entry.Value.ScreenWidth, entry.Value.ScreenHeight));
}
key = string.Join(",", keySegments);
}
private string key;
public string Key
{
get
{
return key;
}
}
public override bool Equals(object obj)
{
var other = obj as DesktopDisplayMetrics;
if (other == null)
{
return false;
}
return this.Key == other.key;
}
public override int GetHashCode()
{
return key.GetHashCode();
}
public int GetHashCode(DesktopDisplayMetrics obj)
{
return obj.key.GetHashCode();
}
}
}

View file

@ -1,15 +1,15 @@
using System;
namespace Ninjacrab.PersistentWindows.Common.Models
{
public class WindowsPositionInfo
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int Left;
public int Top;
public int Width;
public int Height;
public int Flags;
}
}
using System;
namespace Ninjacrab.PersistentWindows.Common.Models
{
public class WindowsPositionInfo
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int Left;
public int Top;
public int Width;
public int Height;
public int Flags;
}
}

View file

@ -1,75 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4CC8B3FB-214B-42AB-8AAE-E7DC5E266EF0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Ninjacrab.PersistentWindows.Common</RootNamespace>
<AssemblyName>Ninjacrab.PersistentWindows.Common</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ManagedWinapi">
<HintPath>..\packages\ManagedWinapi.0.3\ManagedWinapi.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.3.2.0.0\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Diagnostics\Log.cs" />
<Compile Include="Models\ApplicationDisplayMetrics.cs" />
<Compile Include="Models\DesktopDisplayMetrics.cs" />
<Compile Include="Models\WindowPositionInfo.cs" />
<Compile Include="PersistentWindowProcessor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WinApiBridge\CallWindowProcedureParam.cs" />
<Compile Include="WinApiBridge\Display.cs" />
<Compile Include="WinApiBridge\MonitorInfo.cs" />
<Compile Include="WinApiBridge\SetWindowPosFlags.cs" />
<Compile Include="WinApiBridge\ShowWindowCommands.cs" />
<Compile Include="WinApiBridge\User32.cs" />
<Compile Include="WinApiBridge\WindowPlacement.cs" />
<Compile Include="WinApiBridge\WindowsMessage.cs" />
<Compile Include="WinApiBridge\WindowsPosition.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4CC8B3FB-214B-42AB-8AAE-E7DC5E266EF0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Ninjacrab.PersistentWindows.Common</RootNamespace>
<AssemblyName>Ninjacrab.PersistentWindows.Common</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Deploy|AnyCPU'">
<OutputPath>bin\Deploy\</OutputPath>
</PropertyGroup>
<ItemGroup>
<Reference Include="ManagedWinapi">
<HintPath>..\packages\ManagedWinapi.0.3\ManagedWinapi.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.3.2.0.0\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Diagnostics\Log.cs" />
<Compile Include="Models\ApplicationDisplayMetrics.cs" />
<Compile Include="Models\DesktopDisplayMetrics.cs" />
<Compile Include="Models\WindowPositionInfo.cs" />
<Compile Include="PersistentWindowProcessor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WinApiBridge\CallWindowProcedureParam.cs" />
<Compile Include="WinApiBridge\Display.cs" />
<Compile Include="WinApiBridge\MonitorInfo.cs" />
<Compile Include="WinApiBridge\SetWindowPosFlags.cs" />
<Compile Include="WinApiBridge\ShowWindowCommands.cs" />
<Compile Include="WinApiBridge\User32.cs" />
<Compile Include="WinApiBridge\WindowPlacement.cs" />
<Compile Include="WinApiBridge\WindowsMessage.cs" />
<Compile Include="WinApiBridge\WindowsPosition.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -1,385 +1,390 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using ManagedWinapi.Hooks;
using ManagedWinapi.Windows;
using Microsoft.Win32;
using Ninjacrab.PersistentWindows.Common.Diagnostics;
using Ninjacrab.PersistentWindows.Common.Models;
using Ninjacrab.PersistentWindows.Common.WinApiBridge;
namespace Ninjacrab.PersistentWindows.Common
{
public class PersistentWindowProcessor : IDisposable
{
// read and update this from a config file eventually
private int AppsMovedThreshold = 4;
private DesktopDisplayMetrics lastMetrics = null;
private Hook windowProcHook;
public void Start()
{
lastMetrics = DesktopDisplayMetrics.AcquireMetrics();
CaptureApplicationsOnCurrentDisplays(initialCapture: true);
var thread = new Thread(InternalRun);
thread.IsBackground = true;
thread.Name = "PersistentWindowProcessor.InternalRun()";
thread.Start();
SystemEvents.DisplaySettingsChanged += (s, e) =>
{
Log.Info("Display settings changed");
BeginRestoreApplicationsOnCurrentDisplays();
};
SystemEvents.PowerModeChanged += (s, e) =>
{
switch (e.Mode)
{
case PowerModes.Suspend:
Log.Info("System Suspending");
BeginCaptureApplicationsOnCurrentDisplays();
break;
case PowerModes.Resume:
Log.Info("System Resuming");
BeginRestoreApplicationsOnCurrentDisplays();
break;
}
};
//windowProcHook = new Hook();
//windowProcHook.Type = HookType.WH_CALLWNDPROC;
//windowProcHook.Callback += GlobalWindowProcCallback;
//windowProcHook.StartHook();
}
int GlobalWindowProcCallback(int code, IntPtr wParam, IntPtr lParam, ref bool callNext)
{
CallWindowProcedureParam callbackParam = (CallWindowProcedureParam)Marshal.PtrToStructure(lParam, typeof(CallWindowProcedureParam));
switch(callbackParam.message)
{
case WindowsMessage.WINDOWPOSCHANGED:
WindowPositionChangedHandler(callbackParam);
break;
case WindowsMessage.POWERBROADCAST:
Log.Info("Power Broadcast - {0} {1}", wParam, lParam);
break;
case WindowsMessage.ACTIVATE:
case WindowsMessage.ACTIVATEAPP:
case WindowsMessage.CAPTURECHANGED:
case WindowsMessage.ENTERSIZEMOVE:
case WindowsMessage.ERASEBKGND:
case WindowsMessage.EXITSIZEMOVE:
case WindowsMessage.GETTEXT:
case WindowsMessage.GETICON:
case WindowsMessage.GETMINMAXINFO:
case WindowsMessage.HSHELL_ACTIVATESHELLWINDOW:
case WindowsMessage.IME_NOTIFY:
case WindowsMessage.IME_SETCONTEXT:
case WindowsMessage.KILLFOCUS:
case WindowsMessage.MOVING:
case WindowsMessage.NCACTIVATE:
case WindowsMessage.NCCALCSIZE:
case WindowsMessage.NCHITTEST:
case WindowsMessage.NCPAINT:
case WindowsMessage.NULL:
case WindowsMessage.SETCURSOR:
case WindowsMessage.SIZING:
case WindowsMessage.SIZE:
case WindowsMessage.WININICHANGE:
case WindowsMessage.WINDOWPOSCHANGING:
break;
default:
int enumValue = (int)callbackParam.message;
switch(enumValue)
{
case 647:
case 49666:
break;
default:
Log.Info(callbackParam.message.ToString());
break;
}
break;
}
callNext = true;
return 0;
}
/// <summary>
/// OMG this method is awful!!! but yagni
/// </summary>
/// <param name="callbackParam"></param>
private void WindowPositionChangedHandler(CallWindowProcedureParam callbackParam)
{
ApplicationDisplayMetrics appMetrics = null;
if (monitorApplications == null ||
!monitorApplications.ContainsKey(lastMetrics.Key))
{
Log.Error("No definitions found for this resolution: {0}", lastMetrics.Key);
return;
}
appMetrics = monitorApplications[lastMetrics.Key]
.FirstOrDefault(row => row.Value.HWnd == callbackParam.hwnd)
.Value;
if (appMetrics == null)
{
var newAppWindow = SystemWindow.AllToplevelWindows
.FirstOrDefault(row => row.Parent.HWnd.ToInt64() == 0
&& !string.IsNullOrEmpty(row.Title)
&& !row.Title.Equals("Program Manager")
&& row.Visible
&& row.HWnd == callbackParam.hwnd);
if (newAppWindow == null)
{
Log.Error("Can't find hwnd {0}", callbackParam.hwnd.ToInt64());
return;
}
ApplicationDisplayMetrics applicationDisplayMetric = null;
AddOrUpdateWindow(lastMetrics.Key, newAppWindow, out applicationDisplayMetric);
return;
}
WindowPlacement windowPlacement = appMetrics.WindowPlacement;
WindowsPosition newPosition = (WindowsPosition)Marshal.PtrToStructure(callbackParam.lparam, typeof(WindowsPosition));
windowPlacement.NormalPosition.Left = newPosition.Left;
windowPlacement.NormalPosition.Top = newPosition.Top;
windowPlacement.NormalPosition.Right = newPosition.Left + newPosition.Width;
windowPlacement.NormalPosition.Bottom = newPosition.Top + newPosition.Height;
var key = appMetrics.Key;
if (monitorApplications[lastMetrics.Key].ContainsKey(key))
{
monitorApplications[lastMetrics.Key][appMetrics.Key].WindowPlacement = windowPlacement;
}
else
{
Log.Error("Hwnd {0} is not in list, we should capture", callbackParam.hwnd.ToInt64());
return;
}
Log.Info("WPCH - Capturing {0} at [{1}x{2}] size [{3}x{4}]",
appMetrics,
appMetrics.WindowPlacement.NormalPosition.Left,
appMetrics.WindowPlacement.NormalPosition.Top,
appMetrics.WindowPlacement.NormalPosition.Width,
appMetrics.WindowPlacement.NormalPosition.Height
);
}
private readonly Dictionary<string, SortedDictionary<string, ApplicationDisplayMetrics>> monitorApplications = new Dictionary<string, SortedDictionary<string, ApplicationDisplayMetrics>>();
private readonly object displayChangeLock = new object();
private void InternalRun()
{
while(true)
{
CaptureApplicationsOnCurrentDisplays();
Thread.Sleep(1000);
}
}
private void BeginCaptureApplicationsOnCurrentDisplays()
{
var thread = new Thread(() => CaptureApplicationsOnCurrentDisplays());
thread.IsBackground = true;
thread.Name = "PersistentWindowProcessor.BeginCaptureApplicationsOnCurrentDisplays()";
thread.Start();
}
private void CaptureApplicationsOnCurrentDisplays(string displayKey = null, bool initialCapture = false)
{
lock(displayChangeLock)
{
DesktopDisplayMetrics metrics = DesktopDisplayMetrics.AcquireMetrics();
if (displayKey == null)
{
displayKey = metrics.Key;
}
if (!metrics.Equals(lastMetrics))
{
// since the resolution doesn't match, lets wait till it's restored
Log.Info("Detected changes in display metrics, will capture once windows are restored");
return;
}
if (!monitorApplications.ContainsKey(displayKey))
{
monitorApplications.Add(displayKey, new SortedDictionary<string, ApplicationDisplayMetrics>());
}
var appWindows = CaptureWindowsOfInterest();
List<string> changeLog = new List<string>();
List<ApplicationDisplayMetrics> apps = new List<ApplicationDisplayMetrics>();
foreach (var window in appWindows)
{
ApplicationDisplayMetrics applicationDisplayMetric = null;
bool addToChangeLog = AddOrUpdateWindow(displayKey, window, out applicationDisplayMetric);
if (addToChangeLog)
{
apps.Add(applicationDisplayMetric);
changeLog.Add(string.Format("CAOCD - Capturing {0,-45} at [{1,4}x{2,4}] size [{3,4}x{4,4}] V:{5} {6} ",
applicationDisplayMetric,
applicationDisplayMetric.WindowPlacement.NormalPosition.Left,
applicationDisplayMetric.WindowPlacement.NormalPosition.Top,
applicationDisplayMetric.WindowPlacement.NormalPosition.Width,
applicationDisplayMetric.WindowPlacement.NormalPosition.Height,
window.Visible,
window.Title
));
}
}
// only save the updated if it didn't seem like something moved everything
if ((apps.Count > 0
&& apps.Count < AppsMovedThreshold)
|| initialCapture)
{
foreach(var app in apps)
{
if (!monitorApplications[displayKey].ContainsKey(app.Key))
{
monitorApplications[displayKey].Add(app.Key, app);
}
else if (!monitorApplications[displayKey][app.Key].EqualPlacement(app))
{
monitorApplications[displayKey][app.Key].WindowPlacement = app.WindowPlacement;
}
}
changeLog.Sort();
Log.Info("{0}Capturing applications for {1}", initialCapture ? "Initial " : "", displayKey);
Log.Trace("{0} windows recorded{1}{2}", apps.Count, Environment.NewLine, string.Join(Environment.NewLine, changeLog));
}
}
}
private IEnumerable<SystemWindow> CaptureWindowsOfInterest()
{
return SystemWindow.AllToplevelWindows
.Where(row => row.Parent.HWnd.ToInt64() == 0
&& !string.IsNullOrEmpty(row.Title)
&& !row.Title.Equals("Program Manager")
&& row.Visible);
}
private bool AddOrUpdateWindow(string displayKey, SystemWindow window, out ApplicationDisplayMetrics applicationDisplayMetric)
{
WindowPlacement windowPlacement = new WindowPlacement();
User32.GetWindowPlacement(window.HWnd, ref windowPlacement);
applicationDisplayMetric = new ApplicationDisplayMetrics
{
HWnd = window.HWnd,
ApplicationName = window.Process.ProcessName,
ProcessId = window.Process.Id,
WindowPlacement = windowPlacement
};
bool updated = false;
if (!monitorApplications[displayKey].ContainsKey(applicationDisplayMetric.Key))
{
updated = true;
}
else if (!monitorApplications[displayKey][applicationDisplayMetric.Key].EqualPlacement(applicationDisplayMetric))
{
updated = true;
}
return updated;
}
private void BeginRestoreApplicationsOnCurrentDisplays()
{
var thread = new Thread(() =>
{
try
{
RestoreApplicationsOnCurrentDisplays();
}
catch(Exception ex)
{
Log.Error(ex.ToString());
}
});
thread.IsBackground = true;
thread.Name = "PersistentWindowProcessor.RestoreApplicationsOnCurrentDisplays()";
thread.Start();
}
private void RestoreApplicationsOnCurrentDisplays(string displayKey = null)
{
lock (displayChangeLock)
{
DesktopDisplayMetrics metrics = DesktopDisplayMetrics.AcquireMetrics();
if (displayKey == null)
{
displayKey = metrics.Key;
}
lastMetrics = DesktopDisplayMetrics.AcquireMetrics();
if (!monitorApplications.ContainsKey(displayKey))
{
// no old profile, we're done
Log.Info("No old profile found for {0}", displayKey);
CaptureApplicationsOnCurrentDisplays(initialCapture: true);
return;
}
Log.Info("Restoring applications for {0}", displayKey);
foreach (var window in CaptureWindowsOfInterest())
{
string applicationKey = string.Format("{0}-{1}", window.HWnd.ToInt64(), window.Process.ProcessName);
if (monitorApplications[displayKey].ContainsKey(applicationKey))
{
// looks like the window is still here for us to restore
WindowPlacement windowPlacement = monitorApplications[displayKey][applicationKey].WindowPlacement;
if (windowPlacement.ShowCmd == ShowWindowCommands.Maximize)
{
// When restoring maximized windows, it occasionally switches res and when the maximized setting is restored
// the window thinks it's maxxed, but does not eat all the real estate. So we'll temporarily unmaximize then
// re-apply that
windowPlacement.ShowCmd = ShowWindowCommands.Normal;
User32.SetWindowPlacement(monitorApplications[displayKey][applicationKey].HWnd, ref windowPlacement);
windowPlacement.ShowCmd = ShowWindowCommands.Maximize;
}
var success = User32.SetWindowPlacement(monitorApplications[displayKey][applicationKey].HWnd, ref windowPlacement);
if(!success)
{
string error = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Log.Error(error);
}
Log.Info("SetWindowPlacement({0} [{1}x{2}]-[{3}x{4}]) - {5}",
window.Process.ProcessName,
windowPlacement.NormalPosition.Left,
windowPlacement.NormalPosition.Top,
windowPlacement.NormalPosition.Width,
windowPlacement.NormalPosition.Height,
success);
}
}
}
}
public void Dispose()
{
if (windowProcHook != null)
{
windowProcHook.Dispose();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using ManagedWinapi.Hooks;
using ManagedWinapi.Windows;
using Microsoft.Win32;
using Ninjacrab.PersistentWindows.Common.Diagnostics;
using Ninjacrab.PersistentWindows.Common.Models;
using Ninjacrab.PersistentWindows.Common.WinApiBridge;
namespace Ninjacrab.PersistentWindows.Common
{
public class PersistentWindowProcessor : IDisposable
{
// read and update this from a config file eventually
private int AppsMovedThreshold = 4;
private DesktopDisplayMetrics lastMetrics = null;
private Hook windowProcHook;
public void Start()
{
lastMetrics = DesktopDisplayMetrics.AcquireMetrics();
CaptureApplicationsOnCurrentDisplays(initialCapture: true);
var thread = new Thread(InternalRun);
thread.IsBackground = true;
thread.Name = "PersistentWindowProcessor.InternalRun()";
thread.Start();
SystemEvents.DisplaySettingsChanged += (s, e) =>
{
Log.Info("Display settings changed");
BeginRestoreApplicationsOnCurrentDisplays();
};
SystemEvents.PowerModeChanged += (s, e) =>
{
switch (e.Mode)
{
case PowerModes.Suspend:
Log.Info("System Suspending");
BeginCaptureApplicationsOnCurrentDisplays();
break;
case PowerModes.Resume:
Log.Info("System Resuming");
BeginRestoreApplicationsOnCurrentDisplays();
break;
}
};
//windowProcHook = new Hook();
//windowProcHook.Type = HookType.WH_CALLWNDPROC;
//windowProcHook.Callback += GlobalWindowProcCallback;
//windowProcHook.StartHook();
}
int GlobalWindowProcCallback(int code, IntPtr wParam, IntPtr lParam, ref bool callNext)
{
CallWindowProcedureParam callbackParam = (CallWindowProcedureParam)Marshal.PtrToStructure(lParam, typeof(CallWindowProcedureParam));
switch(callbackParam.message)
{
case WindowsMessage.WINDOWPOSCHANGED:
WindowPositionChangedHandler(callbackParam);
break;
case WindowsMessage.POWERBROADCAST:
Log.Info("Power Broadcast - {0} {1}", wParam, lParam);
break;
case WindowsMessage.ACTIVATE:
case WindowsMessage.ACTIVATEAPP:
case WindowsMessage.CAPTURECHANGED:
case WindowsMessage.ENTERSIZEMOVE:
case WindowsMessage.ERASEBKGND:
case WindowsMessage.EXITSIZEMOVE:
case WindowsMessage.GETTEXT:
case WindowsMessage.GETICON:
case WindowsMessage.GETMINMAXINFO:
case WindowsMessage.HSHELL_ACTIVATESHELLWINDOW:
case WindowsMessage.IME_NOTIFY:
case WindowsMessage.IME_SETCONTEXT:
case WindowsMessage.KILLFOCUS:
case WindowsMessage.MOVING:
case WindowsMessage.NCACTIVATE:
case WindowsMessage.NCCALCSIZE:
case WindowsMessage.NCHITTEST:
case WindowsMessage.NCPAINT:
case WindowsMessage.NULL:
case WindowsMessage.SETCURSOR:
case WindowsMessage.SIZING:
case WindowsMessage.SIZE:
case WindowsMessage.WININICHANGE:
case WindowsMessage.WINDOWPOSCHANGING:
break;
default:
int enumValue = (int)callbackParam.message;
switch(enumValue)
{
case 647:
case 49666:
break;
default:
Log.Info(callbackParam.message.ToString());
break;
}
break;
}
callNext = true;
return 0;
}
/// <summary>
/// OMG this method is awful!!! but yagni
/// </summary>
/// <param name="callbackParam"></param>
private void WindowPositionChangedHandler(CallWindowProcedureParam callbackParam)
{
ApplicationDisplayMetrics appMetrics = null;
if (monitorApplications == null ||
!monitorApplications.ContainsKey(lastMetrics.Key))
{
Log.Error("No definitions found for this resolution: {0}", lastMetrics.Key);
return;
}
appMetrics = monitorApplications[lastMetrics.Key]
.FirstOrDefault(row => row.Value.HWnd == callbackParam.hwnd)
.Value;
if (appMetrics == null)
{
var newAppWindow = SystemWindow.AllToplevelWindows
.FirstOrDefault(row => row.Parent.HWnd.ToInt64() == 0
&& !string.IsNullOrEmpty(row.Title)
&& !row.Title.Equals("Program Manager")
&& row.Visible
&& row.HWnd == callbackParam.hwnd);
if (newAppWindow == null)
{
Log.Error("Can't find hwnd {0}", callbackParam.hwnd.ToInt64());
return;
}
ApplicationDisplayMetrics applicationDisplayMetric = null;
AddOrUpdateWindow(lastMetrics.Key, newAppWindow, out applicationDisplayMetric);
return;
}
WindowPlacement windowPlacement = appMetrics.WindowPlacement;
WindowsPosition newPosition = (WindowsPosition)Marshal.PtrToStructure(callbackParam.lparam, typeof(WindowsPosition));
windowPlacement.NormalPosition.Left = newPosition.Left;
windowPlacement.NormalPosition.Top = newPosition.Top;
windowPlacement.NormalPosition.Right = newPosition.Left + newPosition.Width;
windowPlacement.NormalPosition.Bottom = newPosition.Top + newPosition.Height;
var key = appMetrics.Key;
if (monitorApplications[lastMetrics.Key].ContainsKey(key))
{
monitorApplications[lastMetrics.Key][appMetrics.Key].WindowPlacement = windowPlacement;
}
else
{
Log.Error("Hwnd {0} is not in list, we should capture", callbackParam.hwnd.ToInt64());
return;
}
Log.Info("WPCH - Capturing {0} at [{1}x{2}] size [{3}x{4}]",
appMetrics,
appMetrics.WindowPlacement.NormalPosition.Left,
appMetrics.WindowPlacement.NormalPosition.Top,
appMetrics.WindowPlacement.NormalPosition.Width,
appMetrics.WindowPlacement.NormalPosition.Height
);
}
private readonly Dictionary<string, SortedDictionary<string, ApplicationDisplayMetrics>> monitorApplications = new Dictionary<string, SortedDictionary<string, ApplicationDisplayMetrics>>();
private readonly object displayChangeLock = new object();
private void InternalRun()
{
while(true)
{
CaptureApplicationsOnCurrentDisplays();
Thread.Sleep(1000);
}
}
private void BeginCaptureApplicationsOnCurrentDisplays()
{
var thread = new Thread(() => CaptureApplicationsOnCurrentDisplays());
thread.IsBackground = true;
thread.Name = "PersistentWindowProcessor.BeginCaptureApplicationsOnCurrentDisplays()";
thread.Start();
}
private void CaptureApplicationsOnCurrentDisplays(string displayKey = null, bool initialCapture = false)
{
lock(displayChangeLock)
{
DesktopDisplayMetrics metrics = DesktopDisplayMetrics.AcquireMetrics();
if (displayKey == null)
{
displayKey = metrics.Key;
}
if (!metrics.Equals(lastMetrics))
{
// since the resolution doesn't match, lets wait till it's restored
Log.Info("Detected changes in display metrics, will capture once windows are restored");
return;
}
if (!monitorApplications.ContainsKey(displayKey))
{
monitorApplications.Add(displayKey, new SortedDictionary<string, ApplicationDisplayMetrics>());
}
var appWindows = CaptureWindowsOfInterest();
List<string> changeLog = new List<string>();
List<ApplicationDisplayMetrics> apps = new List<ApplicationDisplayMetrics>();
foreach (var window in appWindows)
{
ApplicationDisplayMetrics applicationDisplayMetric = null;
bool addToChangeLog = AddOrUpdateWindow(displayKey, window, out applicationDisplayMetric);
if (addToChangeLog)
{
apps.Add(applicationDisplayMetric);
changeLog.Add(string.Format("CAOCD - Capturing {0,-45} at [{1,4}x{2,4}] size [{3,4}x{4,4}] V:{5} {6} ",
applicationDisplayMetric,
applicationDisplayMetric.WindowPlacement.NormalPosition.Left,
applicationDisplayMetric.WindowPlacement.NormalPosition.Top,
applicationDisplayMetric.WindowPlacement.NormalPosition.Width,
applicationDisplayMetric.WindowPlacement.NormalPosition.Height,
window.Visible,
window.Title
));
}
}
// only save the updated if it didn't seem like something moved everything
if ((apps.Count > 0
&& apps.Count < AppsMovedThreshold)
|| initialCapture)
{
foreach(var app in apps)
{
if (!monitorApplications[displayKey].ContainsKey(app.Key))
{
monitorApplications[displayKey].Add(app.Key, app);
}
else if (!monitorApplications[displayKey][app.Key].EqualPlacement(app))
{
monitorApplications[displayKey][app.Key].WindowPlacement = app.WindowPlacement;
}
}
changeLog.Sort();
Log.Info("{0}Capturing applications for {1}", initialCapture ? "Initial " : "", displayKey);
Log.Trace("{0} windows recorded{1}{2}", apps.Count, Environment.NewLine, string.Join(Environment.NewLine, changeLog));
}
}
}
private IEnumerable<SystemWindow> CaptureWindowsOfInterest()
{
return SystemWindow.AllToplevelWindows
.Where(row => row.Parent.HWnd.ToInt64() == 0
&& !string.IsNullOrEmpty(row.Title)
&& !row.Title.Equals("Program Manager")
&& row.Visible);
}
private bool AddOrUpdateWindow(string displayKey, SystemWindow window, out ApplicationDisplayMetrics applicationDisplayMetric)
{
WindowPlacement windowPlacement = new WindowPlacement();
User32.GetWindowPlacement(window.HWnd, ref windowPlacement);
if (windowPlacement.ShowCmd == ShowWindowCommands.Normal)
{
User32.GetWindowRect(window.HWnd, ref windowPlacement.NormalPosition);
}
applicationDisplayMetric = new ApplicationDisplayMetrics
{
HWnd = window.HWnd,
ApplicationName = window.Process.ProcessName,
ProcessId = window.Process.Id,
WindowPlacement = windowPlacement
};
bool updated = false;
if (!monitorApplications[displayKey].ContainsKey(applicationDisplayMetric.Key))
{
updated = true;
}
else if (!monitorApplications[displayKey][applicationDisplayMetric.Key].EqualPlacement(applicationDisplayMetric))
{
updated = true;
}
return updated;
}
private void BeginRestoreApplicationsOnCurrentDisplays()
{
var thread = new Thread(() =>
{
try
{
RestoreApplicationsOnCurrentDisplays();
}
catch(Exception ex)
{
Log.Error(ex.ToString());
}
});
thread.IsBackground = true;
thread.Name = "PersistentWindowProcessor.RestoreApplicationsOnCurrentDisplays()";
thread.Start();
}
private void RestoreApplicationsOnCurrentDisplays(string displayKey = null)
{
lock (displayChangeLock)
{
DesktopDisplayMetrics metrics = DesktopDisplayMetrics.AcquireMetrics();
if (displayKey == null)
{
displayKey = metrics.Key;
}
lastMetrics = DesktopDisplayMetrics.AcquireMetrics();
if (!monitorApplications.ContainsKey(displayKey))
{
// no old profile, we're done
Log.Info("No old profile found for {0}", displayKey);
CaptureApplicationsOnCurrentDisplays(initialCapture: true);
return;
}
Log.Info("Restoring applications for {0}", displayKey);
foreach (var window in CaptureWindowsOfInterest())
{
string applicationKey = string.Format("{0}-{1}", window.HWnd.ToInt64(), window.Process.ProcessName);
if (monitorApplications[displayKey].ContainsKey(applicationKey))
{
// looks like the window is still here for us to restore
WindowPlacement windowPlacement = monitorApplications[displayKey][applicationKey].WindowPlacement;
if (windowPlacement.ShowCmd == ShowWindowCommands.Maximize)
{
// When restoring maximized windows, it occasionally switches res and when the maximized setting is restored
// the window thinks it's maxxed, but does not eat all the real estate. So we'll temporarily unmaximize then
// re-apply that
windowPlacement.ShowCmd = ShowWindowCommands.Normal;
User32.SetWindowPlacement(monitorApplications[displayKey][applicationKey].HWnd, ref windowPlacement);
windowPlacement.ShowCmd = ShowWindowCommands.Maximize;
}
var success = User32.SetWindowPlacement(monitorApplications[displayKey][applicationKey].HWnd, ref windowPlacement);
if(!success)
{
string error = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Log.Error(error);
}
Log.Info("SetWindowPlacement({0} [{1}x{2}]-[{3}x{4}]) - {5}",
window.Process.ProcessName,
windowPlacement.NormalPosition.Left,
windowPlacement.NormalPosition.Top,
windowPlacement.NormalPosition.Width,
windowPlacement.NormalPosition.Height,
success);
}
}
}
}
public void Dispose()
{
if (windowProcHook != null)
{
windowProcHook.Dispose();
}
}
}
}

View file

@ -1,36 +1,36 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Ninjacrab.PersistentWindows.Common")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Ninjacrab.PersistentWindows.Common")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("39340812-16cc-4ba7-b3ac-08f093ab98d0")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.0")]
[assembly: AssemblyFileVersion("1.0.7.0")]
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Ninjacrab.PersistentWindows.Common")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Ninjacrab.PersistentWindows.Common")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("39340812-16cc-4ba7-b3ac-08f093ab98d0")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.0")]
[assembly: AssemblyFileVersion("1.0.7.0")]

View file

@ -1,14 +1,14 @@
using System;
using System.Runtime.InteropServices;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential)]
public struct CallWindowProcedureParam
{
public IntPtr lparam;
public IntPtr wparam;
public WindowsMessage message;
public IntPtr hwnd;
}
}
using System;
using System.Runtime.InteropServices;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential)]
public struct CallWindowProcedureParam
{
public IntPtr lparam;
public IntPtr wparam;
public WindowsMessage message;
public IntPtr hwnd;
}
}

View file

@ -1,41 +1,46 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
public class Display
{
public int ScreenWidth { get; internal set; }
public int ScreenHeight { get; internal set; }
public int Left { get; internal set; }
public int Top { get; internal set; }
public uint Flags { get; internal set; }
public static List<Display> GetDisplays()
{
List<Display> displays = new List<Display>();
User32.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
{
MonitorInfo monitorInfo = new MonitorInfo();
monitorInfo.StructureSize = Marshal.SizeOf(monitorInfo);
bool success = User32.GetMonitorInfo(hMonitor, ref monitorInfo);
if (success)
{
Display display = new Display();
display.ScreenWidth = monitorInfo.Monitor.Width;
display.ScreenHeight = monitorInfo.Monitor.Height;
display.Left = monitorInfo.Monitor.Left;
display.Top = monitorInfo.Monitor.Top;
display.Flags = monitorInfo.Flags;
displays.Add(display);
}
return true;
}, IntPtr.Zero);
return displays;
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
public class Display
{
public int ScreenWidth { get; internal set; }
public int ScreenHeight { get; internal set; }
public int Left { get; internal set; }
public int Top { get; internal set; }
public uint Flags { get; internal set; }
public String DeviceName { get; internal set; }
public static List<Display> GetDisplays()
{
List<Display> displays = new List<Display>();
User32.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
{
MonitorInfo monitorInfo = new MonitorInfo();
monitorInfo.StructureSize = Marshal.SizeOf(monitorInfo);
bool success = User32.GetMonitorInfo(hMonitor, ref monitorInfo);
if (success)
{
Display display = new Display();
display.ScreenWidth = monitorInfo.Monitor.Width;
display.ScreenHeight = monitorInfo.Monitor.Height;
display.Left = monitorInfo.Monitor.Left;
display.Top = monitorInfo.Monitor.Top;
display.Flags = monitorInfo.Flags;
int pos = monitorInfo.DeviceName.LastIndexOf("\\") + 1;
display.DeviceName = monitorInfo.DeviceName.Substring(pos, monitorInfo.DeviceName.Length - pos);
displays.Add(display);
}
return true;
}, IntPtr.Zero);
return displays;
}
}
}

View file

@ -1,20 +1,20 @@
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MonitorInfo
{
// size of a device name string
private const int CCHDEVICENAME = 32;
public int StructureSize;
public RECT Monitor;
public RECT WorkArea;
public uint Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string DeviceName;
}
}
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MonitorInfo
{
// size of a device name string
private const int CCHDEVICENAME = 32;
public int StructureSize;
public RECT Monitor;
public RECT WorkArea;
public uint Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string DeviceName;
}
}

View file

@ -1,65 +1,65 @@
using System;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[Flags()]
public enum SetWindowPosFlags : uint
{
/// <summary>If the calling thread and the thread that owns the window are attached to different input queues,
/// the system posts the request to the thread that owns the window. This prevents the calling thread from
/// blocking its execution while other threads process the request.</summary>
/// <remarks>SWP_ASYNCWINDOWPOS</remarks>
AsynchronousWindowPosition = 0x4000,
/// <summary>Prevents generation of the WM_SYNCPAINT message.</summary>
/// <remarks>SWP_DEFERERASE</remarks>
DeferErase = 0x2000,
/// <summary>Draws a frame (defined in the window's class description) around the window.</summary>
/// <remarks>SWP_DRAWFRAME</remarks>
DrawFrame = 0x0020,
/// <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to
/// the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE
/// is sent only when the window's size is being changed.</summary>
/// <remarks>SWP_FRAMECHANGED</remarks>
FrameChanged = 0x0020,
/// <summary>Hides the window.</summary>
/// <remarks>SWP_HIDEWINDOW</remarks>
HideWindow = 0x0080,
/// <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the
/// top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter
/// parameter).</summary>
/// <remarks>SWP_NOACTIVATE</remarks>
DoNotActivate = 0x0010,
/// <summary>Discards the entire contents of the client area. If this flag is not specified, the valid
/// contents of the client area are saved and copied back into the client area after the window is sized or
/// repositioned.</summary>
/// <remarks>SWP_NOCOPYBITS</remarks>
DoNotCopyBits = 0x0100,
/// <summary>Retains the current position (ignores X and Y parameters).</summary>
/// <remarks>SWP_NOMOVE</remarks>
IgnoreMove = 0x0002,
/// <summary>Does not change the owner window's position in the Z order.</summary>
/// <remarks>SWP_NOOWNERZORDER</remarks>
DoNotChangeOwnerZOrder = 0x0200,
/// <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to
/// the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent
/// window uncovered as a result of the window being moved. When this flag is set, the application must
/// explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary>
/// <remarks>SWP_NOREDRAW</remarks>
DoNotRedraw = 0x0008,
/// <summary>Same as the SWP_NOOWNERZORDER flag.</summary>
/// <remarks>SWP_NOREPOSITION</remarks>
DoNotReposition = 0x0200,
/// <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary>
/// <remarks>SWP_NOSENDCHANGING</remarks>
DoNotSendChangingEvent = 0x0400,
/// <summary>Retains the current size (ignores the cx and cy parameters).</summary>
/// <remarks>SWP_NOSIZE</remarks>
IgnoreResize = 0x0001,
/// <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary>
/// <remarks>SWP_NOZORDER</remarks>
IgnoreZOrder = 0x0004,
/// <summary>Displays the window.</summary>
/// <remarks>SWP_SHOWWINDOW</remarks>
ShowWindow = 0x0040,
}
}
using System;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[Flags()]
public enum SetWindowPosFlags : uint
{
/// <summary>If the calling thread and the thread that owns the window are attached to different input queues,
/// the system posts the request to the thread that owns the window. This prevents the calling thread from
/// blocking its execution while other threads process the request.</summary>
/// <remarks>SWP_ASYNCWINDOWPOS</remarks>
AsynchronousWindowPosition = 0x4000,
/// <summary>Prevents generation of the WM_SYNCPAINT message.</summary>
/// <remarks>SWP_DEFERERASE</remarks>
DeferErase = 0x2000,
/// <summary>Draws a frame (defined in the window's class description) around the window.</summary>
/// <remarks>SWP_DRAWFRAME</remarks>
DrawFrame = 0x0020,
/// <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to
/// the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE
/// is sent only when the window's size is being changed.</summary>
/// <remarks>SWP_FRAMECHANGED</remarks>
FrameChanged = 0x0020,
/// <summary>Hides the window.</summary>
/// <remarks>SWP_HIDEWINDOW</remarks>
HideWindow = 0x0080,
/// <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the
/// top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter
/// parameter).</summary>
/// <remarks>SWP_NOACTIVATE</remarks>
DoNotActivate = 0x0010,
/// <summary>Discards the entire contents of the client area. If this flag is not specified, the valid
/// contents of the client area are saved and copied back into the client area after the window is sized or
/// repositioned.</summary>
/// <remarks>SWP_NOCOPYBITS</remarks>
DoNotCopyBits = 0x0100,
/// <summary>Retains the current position (ignores X and Y parameters).</summary>
/// <remarks>SWP_NOMOVE</remarks>
IgnoreMove = 0x0002,
/// <summary>Does not change the owner window's position in the Z order.</summary>
/// <remarks>SWP_NOOWNERZORDER</remarks>
DoNotChangeOwnerZOrder = 0x0200,
/// <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to
/// the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent
/// window uncovered as a result of the window being moved. When this flag is set, the application must
/// explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary>
/// <remarks>SWP_NOREDRAW</remarks>
DoNotRedraw = 0x0008,
/// <summary>Same as the SWP_NOOWNERZORDER flag.</summary>
/// <remarks>SWP_NOREPOSITION</remarks>
DoNotReposition = 0x0200,
/// <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary>
/// <remarks>SWP_NOSENDCHANGING</remarks>
DoNotSendChangingEvent = 0x0400,
/// <summary>Retains the current size (ignores the cx and cy parameters).</summary>
/// <remarks>SWP_NOSIZE</remarks>
IgnoreResize = 0x0001,
/// <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary>
/// <remarks>SWP_NOZORDER</remarks>
IgnoreZOrder = 0x0004,
/// <summary>Displays the window.</summary>
/// <remarks>SWP_SHOWWINDOW</remarks>
ShowWindow = 0x0040,
}
}

View file

@ -1,74 +1,74 @@
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
public enum ShowWindowCommands
{
/// <summary>
/// Hides the window and activates another window.
/// </summary>
Hide = 0,
/// <summary>
/// Activates and displays a window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when displaying the window
/// for the first time.
/// </summary>
Normal = 1,
/// <summary>
/// Activates the window and displays it as a minimized window.
/// </summary>
ShowMinimized = 2,
/// <summary>
/// Maximizes the specified window.
/// </summary>
Maximize = 3, // is this the right value?
/// <summary>
/// Activates the window and displays it as a maximized window.
/// </summary>
ShowMaximized = 3,
/// <summary>
/// Displays a window in its most recent size and position. This value
/// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
/// the window is not activated.
/// </summary>
ShowNoActivate = 4,
/// <summary>
/// Activates the window and displays it in its current size and position.
/// </summary>
Show = 5,
/// <summary>
/// Minimizes the specified window and activates the next top-level
/// window in the Z order.
/// </summary>
Minimize = 6,
/// <summary>
/// Displays the window as a minimized window. This value is similar to
/// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
/// window is not activated.
/// </summary>
ShowMinNoActive = 7,
/// <summary>
/// Displays the window in its current size and position. This value is
/// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
/// window is not activated.
/// </summary>
ShowNA = 8,
/// <summary>
/// Activates and displays the window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when restoring a minimized window.
/// </summary>
Restore = 9,
/// <summary>
/// Sets the show state based on the SW_* value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.
/// </summary>
ShowDefault = 10,
/// <summary>
/// <b>Windows 2000/XP:</b> Minimizes a window, even if the thread
/// that owns the window is not responding. This flag should only be
/// used when minimizing windows from a different thread.
/// </summary>
ForceMinimize = 11
}
}
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
public enum ShowWindowCommands
{
/// <summary>
/// Hides the window and activates another window.
/// </summary>
Hide = 0,
/// <summary>
/// Activates and displays a window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when displaying the window
/// for the first time.
/// </summary>
Normal = 1,
/// <summary>
/// Activates the window and displays it as a minimized window.
/// </summary>
ShowMinimized = 2,
/// <summary>
/// Maximizes the specified window.
/// </summary>
Maximize = 3, // is this the right value?
/// <summary>
/// Activates the window and displays it as a maximized window.
/// </summary>
ShowMaximized = 3,
/// <summary>
/// Displays a window in its most recent size and position. This value
/// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
/// the window is not activated.
/// </summary>
ShowNoActivate = 4,
/// <summary>
/// Activates the window and displays it in its current size and position.
/// </summary>
Show = 5,
/// <summary>
/// Minimizes the specified window and activates the next top-level
/// window in the Z order.
/// </summary>
Minimize = 6,
/// <summary>
/// Displays the window as a minimized window. This value is similar to
/// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
/// window is not activated.
/// </summary>
ShowMinNoActive = 7,
/// <summary>
/// Displays the window in its current size and position. This value is
/// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
/// window is not activated.
/// </summary>
ShowNA = 8,
/// <summary>
/// Activates and displays the window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when restoring a minimized window.
/// </summary>
Restore = 9,
/// <summary>
/// Sets the show state based on the SW_* value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.
/// </summary>
ShowDefault = 10,
/// <summary>
/// <b>Windows 2000/XP:</b> Minimizes a window, even if the thread
/// that owns the window is not responding. This flag should only be
/// used when minimizing windows from a different thread.
/// </summary>
ForceMinimize = 11
}
}

View file

@ -1,34 +1,35 @@
using System;
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
public class User32
{
#region EnumDisplayMonitors
public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
#endregion
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo lpmi);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WindowPlacement lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int left, int top, int width, int height, SetWindowPosFlags uFlags);
#region Hooks
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
#endregion
}
}
using System;
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
public class User32
{
#region EnumDisplayMonitors
public delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
[DllImport("user32.dll")]
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);
#endregion
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo lpmi);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WindowPlacement lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WindowPlacement lpwndpl);
#region Hooks
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
#endregion
}
}

View file

@ -1,55 +1,55 @@
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential)]
public struct WindowPlacement
{
/// <summary>
/// The length of the structure, in bytes. Before calling the GetWindowPlacement or SetWindowPlacement functions, set this member to sizeof(WINDOWPLACEMENT).
/// <para>
/// GetWindowPlacement and SetWindowPlacement fail if this member is not set correctly.
/// </para>
/// </summary>
public int Length;
/// <summary>
/// Specifies flags that control the position of the minimized window and the method by which the window is restored.
/// </summary>
public int Flags;
/// <summary>
/// The current show state of the window.
/// </summary>
public ShowWindowCommands ShowCmd;
/// <summary>
/// The coordinates of the window's upper-left corner when the window is minimized.
/// </summary>
public POINT MinPosition;
/// <summary>
/// The coordinates of the window's upper-left corner when the window is maximized.
/// </summary>
public POINT MaxPosition;
/// <summary>
/// The window's coordinates when the window is in the restored position.
/// </summary>
public RECT NormalPosition;
/// <summary>
/// Gets the default (empty) value.
/// </summary>
public static WindowPlacement Default
{
get
{
WindowPlacement result = new WindowPlacement();
result.Length = Marshal.SizeOf(result);
return result;
}
}
}
}
using System.Runtime.InteropServices;
using ManagedWinapi.Windows;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential)]
public struct WindowPlacement
{
/// <summary>
/// The length of the structure, in bytes. Before calling the GetWindowPlacement or SetWindowPlacement functions, set this member to sizeof(WINDOWPLACEMENT).
/// <para>
/// GetWindowPlacement and SetWindowPlacement fail if this member is not set correctly.
/// </para>
/// </summary>
public int Length;
/// <summary>
/// Specifies flags that control the position of the minimized window and the method by which the window is restored.
/// </summary>
public int Flags;
/// <summary>
/// The current show state of the window.
/// </summary>
public ShowWindowCommands ShowCmd;
/// <summary>
/// The coordinates of the window's upper-left corner when the window is minimized.
/// </summary>
public POINT MinPosition;
/// <summary>
/// The coordinates of the window's upper-left corner when the window is maximized.
/// </summary>
public POINT MaxPosition;
/// <summary>
/// The window's coordinates when the window is in the restored position.
/// </summary>
public RECT NormalPosition;
/// <summary>
/// Gets the default (empty) value.
/// </summary>
public static WindowPlacement Default
{
get
{
WindowPlacement result = new WindowPlacement();
result.Length = Marshal.SizeOf(result);
return result;
}
}
}
}

View file

@ -1,17 +1,17 @@
using System;
using System.Runtime.InteropServices;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential)]
public struct WindowsPosition
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int Left;
public int Top;
public int Width;
public int Height;
public int Flags;
}
}
using System;
using System.Runtime.InteropServices;
namespace Ninjacrab.PersistentWindows.Common.WinApiBridge
{
[StructLayout(LayoutKind.Sequential)]
public struct WindowsPosition
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int Left;
public int Top;
public int Width;
public int Height;
public int Flags;
}
}

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="3.2.0.0" targetFramework="net45" />
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="3.2.0.0" targetFramework="net45" />
</packages>