Converted ShellItemChangeWatcher from using NativeWindow to using SystemEventHandler to support console use

pull/211/head
dahall 2020-12-22 11:46:13 -07:00
parent 8e1057e676
commit 4502fcb5af
1 changed files with 10 additions and 10 deletions

View File

@ -7,6 +7,7 @@ using System.Windows.Forms;
using System.Windows.Forms.Design; using System.Windows.Forms.Design;
using Vanara.Extensions; using Vanara.Extensions;
using Vanara.PInvoke; using Vanara.PInvoke;
using static Vanara.PInvoke.User32;
using static Vanara.PInvoke.Shell32; using static Vanara.PInvoke.Shell32;
namespace Vanara.Windows.Shell namespace Vanara.Windows.Shell
@ -322,7 +323,7 @@ namespace Vanara.Windows.Shell
SHGetIDListFromObject(Item.IShellItem, out PIDL pidlWatch).ThrowIfFailed(); SHGetIDListFromObject(Item.IShellItem, out PIDL pidlWatch).ThrowIfFailed();
SHChangeNotifyEntry[] entries = { new SHChangeNotifyEntry { pidl = pidlWatch.DangerousGetHandle(), fRecursive = IncludeChildren } }; SHChangeNotifyEntry[] entries = { new SHChangeNotifyEntry { pidl = pidlWatch.DangerousGetHandle(), fRecursive = IncludeChildren } };
ulRegister = SHChangeNotifyRegister(hPump.Handle, sources, (SHCNE)NotifyFilter, hPump.MessageId, entries.Length, entries); ulRegister = SHChangeNotifyRegister(hPump.MessageWindowHandle, sources, (SHCNE)NotifyFilter, hPump.MessageId, entries.Length, entries);
if (ulRegister == 0) throw new InvalidOperationException("Unable to register shell notifications."); if (ulRegister == 0) throw new InvalidOperationException("Unable to register shell notifications.");
} }
@ -355,28 +356,27 @@ namespace Vanara.Windows.Shell
public ChangeFilters ChangeType { get; } public ChangeFilters ChangeType { get; }
} }
private class WatcherNativeWindow : NativeWindow private class WatcherNativeWindow : SystemEventHandler
{ {
private readonly ShellItemChangeWatcher p; private readonly ShellItemChangeWatcher p;
public WatcherNativeWindow(ShellItemChangeWatcher parent) public WatcherNativeWindow(ShellItemChangeWatcher parent) : base()
{ {
MessageId = User32.RegisterWindowMessage($"{parent.GetType()}{DateTime.Now.Ticks}"); MessageId = User32.RegisterWindowMessage($"{parent.GetType()}{DateTime.Now.Ticks}");
p = parent; p = parent;
var cp = new CreateParams { Style = 0, ExStyle = 0, ClassStyle = 0, Parent = IntPtr.Zero, Caption = GetType().Name };
CreateHandle(cp);
} }
public uint MessageId { get; set; } public uint MessageId { get; set; }
protected override void WndProc(ref Message m) protected override bool MessageFilter(HWND hwnd, uint msg, IntPtr wParam, IntPtr lParam, out IntPtr lReturn)
{ {
if (m.Msg == MessageId && p.enabled && !p.IsSuspended) lReturn = default;
if (msg == MessageId && p.enabled && !p.IsSuspended)
{ {
HLOCK hNotifyLock = default; HLOCK hNotifyLock = default;
try try
{ {
hNotifyLock = SHChangeNotification_Lock(m.WParam, (uint)m.LParam.ToInt32(), out IntPtr rgpidl, out SHCNE lEvent); hNotifyLock = SHChangeNotification_Lock(wParam, (uint)lParam.ToInt32(), out IntPtr rgpidl, out SHCNE lEvent);
if (hNotifyLock != IntPtr.Zero && rgpidl != IntPtr.Zero && p.NotifyFilter.IsFlagSet((ChangeFilters)lEvent)) if (hNotifyLock != IntPtr.Zero && rgpidl != IntPtr.Zero && p.NotifyFilter.IsFlagSet((ChangeFilters)lEvent))
{ {
ShellItemChangeEventArgs args; ShellItemChangeEventArgs args;
@ -394,9 +394,9 @@ namespace Vanara.Windows.Shell
if (hNotifyLock != default) if (hNotifyLock != default)
SHChangeNotification_Unlock(hNotifyLock); SHChangeNotification_Unlock(hNotifyLock);
} }
return; return true;
} }
base.WndProc(ref m); return false;
} }
} }
} }