1. Adding event capture logic

2. Trying to modify where the changes are captured.
3. More logging
This commit is contained in:
Min Yong Kim 2014-11-05 17:42:12 -05:00
parent d2d1819552
commit 7e49f459b4
7 changed files with 196 additions and 14 deletions

View file

@ -1,6 +1,7 @@
using System.Windows;
using System.Windows.Controls;
using Ninjacrab.PersistentWindows.WpfShell.Diagnostics;
using NLog;
namespace Ninjacrab.PersistentWindows.WpfShell
{
@ -18,7 +19,9 @@ namespace Ninjacrab.PersistentWindows.WpfShell
this.DataContext = viewModel;
Log.LogEvent += (level, message) =>
{
Application.Current.Dispatcher.Invoke(() =>
if (level != LogLevel.Trace)
{
Application.Current.Dispatcher.Invoke(() =>
{
viewModel.EventLog.Add(string.Format("{0}: {1}", level, message));
if (viewModel.EventLog.Count > 500)
@ -26,6 +29,7 @@ namespace Ninjacrab.PersistentWindows.WpfShell
viewModel.EventLog.RemoveAt(0);
}
});
}
};
}
}

View file

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ninjacrab.PersistentWindows.WpfShell.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

@ -82,13 +82,16 @@
</ApplicationDefinition>
<Compile Include="Diagnostics\Log.cs" />
<Compile Include="Models\ApplicationDisplayMetrics.cs" />
<Compile Include="Models\WindowPositionInfo.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" />
<Page Include="DiagnosticsView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View file

@ -62,27 +62,98 @@ namespace Ninjacrab.PersistentWindows.WpfShell
switch(callbackParam.message)
{
case WindowsMessage.WINDOWPOSCHANGED:
ApplicationDisplayMetrics appMetrics = null;
if (monitorApplications != null &&
monitorApplications.ContainsKey(lastMetrics.Key))
{
appMetrics = monitorApplications[lastMetrics.Key]
.FirstOrDefault(row => row.Value.HWnd == callbackParam.hwnd)
.Value;
}
Log.Info("{0} {1}", appMetrics != null ? appMetrics.Key : callbackParam.hwnd.ToInt64().ToString(), callbackParam.message);
WindowPositionChangedHandler(callbackParam);
break;
case WindowsMessage.POWERBROADCAST:
Log.Info(callbackParam.message.ToString());
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;
}
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;
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("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();
@ -90,7 +161,7 @@ namespace Ninjacrab.PersistentWindows.WpfShell
{
while(true)
{
CaptureApplicationsOnCurrentDisplays();
//CaptureApplicationsOnCurrentDisplays();
Thread.Sleep(1000);
}
}

View file

@ -0,0 +1,65 @@
using System;
namespace Ninjacrab.PersistentWindows.WpfShell.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

@ -23,6 +23,9 @@ namespace Ninjacrab.PersistentWindows.WpfShell.WinApiBridge
[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);

View file

@ -0,0 +1,17 @@
using System;
using System.Runtime.InteropServices;
namespace Ninjacrab.PersistentWindows.WpfShell.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;
}
}