Skip to content

Commit

Permalink
Issues #109 Replaced RegisterWindowMessage function calls with WM_APP…
Browse files Browse the repository at this point in the history
… messages
  • Loading branch information
AlexanderPro committed Jul 30, 2023
1 parent 350f4a9 commit dbbf020
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 205 deletions.
79 changes: 41 additions & 38 deletions SmartSystemMenu/Hooks/CBTHook.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using SmartSystemMenu.Native;
using System.Windows.Forms;
using static SmartSystemMenu.Native.Constants;
using static SmartSystemMenu.Native.User32;


namespace SmartSystemMenu.Hooks
{
class CBTHook : Hook
{
private int _msgIdCbtCreateWnd;
private int _msgIdCbtDestroyWnd;
private int _msgIdCbtMinMax;
private int _msgIdCbtMoveSize;
private int _msgIdCbtActivate;

public event EventHandler<WindowEventArgs> WindowCreated;
public event EventHandler<WindowEventArgs> WindowDestroyed;
public event EventHandler<SysCommandEventArgs> MinMax;
Expand All @@ -24,20 +20,15 @@ public CBTHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragByMous

protected override void OnStart()
{
_msgIdCbtCreateWnd = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_HCBT_CREATEWND");
_msgIdCbtDestroyWnd = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_HCBT_DESTROYWND");
_msgIdCbtMinMax = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_HCBT_MINMAX");
_msgIdCbtMoveSize = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_HCBT_MOVESIZE");
_msgIdCbtActivate = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_HCBT_ACTIVATE");

if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(_msgIdCbtCreateWnd, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdCbtDestroyWnd, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdCbtMinMax, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdCbtMoveSize, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdCbtActivate, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_HCBT_CREATEWND, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_HCBT_DESTROYWND, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_HCBT_MINMAX, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_HCBT_MOVESIZE, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_HCBT_ACTIVATE, MSGFLT_ADD);
}

NativeHookMethods.InitializeCbtHook(0, _handle, _dragByMouseMenuItem);
}

Expand All @@ -46,28 +37,40 @@ protected override void OnStop()
NativeHookMethods.UninitializeCbtHook();
}

public override void ProcessWindowMessage(ref System.Windows.Forms.Message m)
public override void ProcessWindowMessage(ref Message m)
{
if (m.Msg == _msgIdCbtCreateWnd)
switch (m.Msg)
{
RaiseEvent(WindowCreated, new WindowEventArgs(m.WParam));
}
else if (m.Msg == _msgIdCbtDestroyWnd)
{
RaiseEvent(WindowDestroyed, new WindowEventArgs(m.WParam));
}
else if (m.Msg == _msgIdCbtMinMax)
{
RaiseEvent(MinMax, new SysCommandEventArgs(m.WParam, m.LParam));
}
else if (m.Msg == _msgIdCbtMoveSize)
{
RaiseEvent(MoveSize, new WindowEventArgs(m.WParam));
}
else if (m.Msg == _msgIdCbtActivate)
{
RaiseEvent(Activate, new WindowEventArgs(m.WParam));
}
case WM_SSM_HOOK_HCBT_CREATEWND:
{
RaiseEvent(WindowCreated, new WindowEventArgs(m.WParam));
}
break;

case WM_SSM_HOOK_HCBT_DESTROYWND:
{
RaiseEvent(WindowDestroyed, new WindowEventArgs(m.WParam));
}
break;

case WM_SSM_HOOK_HCBT_MINMAX:
{
RaiseEvent(MinMax, new SysCommandEventArgs(m.WParam, m.LParam));
}
break;

case WM_SSM_HOOK_HCBT_MOVESIZE:
{
RaiseEvent(MoveSize, new WindowEventArgs(m.WParam));
}
break;

case WM_SSM_HOOK_HCBT_ACTIVATE:
{
RaiseEvent(Activate, new WindowEventArgs(m.WParam));
}
break;
};
}
}
}
21 changes: 9 additions & 12 deletions SmartSystemMenu/Hooks/CallWndProcHook.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using SmartSystemMenu.Native;
using System.Windows.Forms;
using static SmartSystemMenu.Native.Constants;
using static SmartSystemMenu.Native.User32;

namespace SmartSystemMenu.Hooks
{
class CallWndProcHook : Hook
{
private int _msgIdCallWndProc;
private int _msgIdCallWndProcParams;
private IntPtr _cacheHandle;
private IntPtr _cacheMessage;

Expand All @@ -19,14 +18,12 @@ public CallWndProcHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dr

protected override void OnStart()
{
_msgIdCallWndProc = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_CALLWNDPROC");
_msgIdCallWndProcParams = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_CALLWNDPROC_PARAMS");

if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(_msgIdCallWndProc, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdCallWndProcParams, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC_PARAMS, MSGFLT_ADD);
}

NativeHookMethods.InitializeCallWndProcHook(0, _handle, _dragByMouseMenuItem);
}

Expand All @@ -35,18 +32,18 @@ protected override void OnStop()
NativeHookMethods.UninitializeCallWndProcHook();
}

public override void ProcessWindowMessage(ref System.Windows.Forms.Message m)
public override void ProcessWindowMessage(ref Message m)
{
if (m.Msg == _msgIdCallWndProc)
if (m.Msg == WM_SSM_HOOK_CALLWNDPROC)
{
_cacheHandle = m.WParam;
_cacheMessage = m.LParam;
}
else if (m.Msg == _msgIdCallWndProcParams)
else if (m.Msg == WM_SSM_HOOK_CALLWNDPROC_PARAMS)
{
if (CallWndProc != null && _cacheHandle != IntPtr.Zero && _cacheMessage != IntPtr.Zero)
{
RaiseEvent( CallWndProc, new WndProcEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
RaiseEvent(CallWndProc, new WndProcEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
}
_cacheHandle = IntPtr.Zero;
_cacheMessage = IntPtr.Zero;
Expand Down
32 changes: 9 additions & 23 deletions SmartSystemMenu/Hooks/GetMsgHook.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using SmartSystemMenu.Native;
using System.Windows.Forms;
using static SmartSystemMenu.Native.Constants;
using static SmartSystemMenu.Native.User32;

namespace SmartSystemMenu.Hooks
{
class GetMsgHook : Hook
{
private int _msgIdGetMsg;
private int _msgIdGetMsgParams;
private IntPtr _cacheHandle;
private IntPtr _cacheMessage;

Expand All @@ -19,14 +18,12 @@ public GetMsgHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragByM

protected override void OnStart()
{
_msgIdGetMsg = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_GETMSG");
_msgIdGetMsgParams = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_GETMSG_PARAMS");

if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(_msgIdGetMsg, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdGetMsgParams, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG_PARAMS, MSGFLT_ADD);
}

NativeHookMethods.InitializeGetMsgHook(0, _handle, _dragByMouseMenuItem);
}

Expand All @@ -35,33 +32,22 @@ protected override void OnStop()
NativeHookMethods.UninitializeGetMsgHook();
}

public override void ProcessWindowMessage(ref System.Windows.Forms.Message m)
public override void ProcessWindowMessage(ref Message m)
{
//string dbgMessage = "";
if (m.Msg == _msgIdGetMsg)
if (m.Msg == WM_SSM_HOOK_GETMSG)
{
//if (m.LParam.ToInt64() == NativeConstants.WM_SYSCOMMAND)
//{
// dbgMessage = string.Format("WM_SYSCOMMAND, GetMsg, Handle = {0}", m.WParam);
// System.Diagnostics.Trace.WriteLine(dbgMessage);
//}
_cacheHandle = m.WParam;
_cacheMessage = m.LParam;
}
else if (m.Msg == _msgIdGetMsgParams)
else if (m.Msg == WM_SSM_HOOK_GETMSG_PARAMS)
{
if (GetMsg != null && _cacheHandle != IntPtr.Zero && _cacheMessage != IntPtr.Zero)
{
//if (cacheMessage.ToInt64() == NativeConstants.WM_SYSCOMMAND)
//{
// dbgMessage = string.Format("WM_SYSCOMMAND, GetMsgParams, Handle = {0}, WParam = {1}", cacheHandle, m.WParam);
// System.Diagnostics.Trace.WriteLine(dbgMessage);
//}
RaiseEvent(GetMsg, new WndProcEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
}
_cacheHandle = IntPtr.Zero;
_cacheMessage = IntPtr.Zero;
}
}
}
}
}
37 changes: 20 additions & 17 deletions SmartSystemMenu/Hooks/KeyboardHook.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using SmartSystemMenu.Native;
using System.Windows.Forms;
using static SmartSystemMenu.Native.Constants;
using static SmartSystemMenu.Native.User32;

namespace SmartSystemMenu.Hooks
{
class KeyboardHook : Hook
{
private int _msgIdKeyboard;
private int _msgIdKeyboardHookReplaced;

public event EventHandler<EventArgs> HookReplaced;
public event EventHandler<BasicHookEventArgs> KeyboardEvent;

Expand All @@ -18,14 +16,12 @@ public KeyboardHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragB

protected override void OnStart()
{
_msgIdKeyboard = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_KEYBOARD");
_msgIdKeyboardHookReplaced = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_KEYBOARD_REPLACED");

if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(_msgIdKeyboard, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdKeyboardHookReplaced, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_KEYBOARD, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_KEYBOARD_REPLACED, MSGFLT_ADD);
}

NativeHookMethods.InitializeKeyboardHook(0, _handle, _dragByMouseMenuItem);
}

Expand All @@ -34,16 +30,23 @@ protected override void OnStop()
NativeHookMethods.UninitializeKeyboardHook();
}

public override void ProcessWindowMessage(ref System.Windows.Forms.Message m)
public override void ProcessWindowMessage(ref Message m)
{
if (m.Msg == _msgIdKeyboard)
switch (m.Msg)
{
RaiseEvent(KeyboardEvent, new BasicHookEventArgs(m.WParam, m.LParam));
}
else if (m.Msg == _msgIdKeyboardHookReplaced)
{
RaiseEvent(HookReplaced, EventArgs.Empty);
case WM_SSM_HOOK_KEYBOARD:
{
RaiseEvent(KeyboardEvent, new BasicHookEventArgs(m.WParam, m.LParam));
}
break;

case WM_SSM_HOOK_KEYBOARD_REPLACED:
{
RaiseEvent(HookReplaced, EventArgs.Empty);
}
break;

}
}
}
}
}
36 changes: 19 additions & 17 deletions SmartSystemMenu/Hooks/KeyboardLLHook.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;
using SmartSystemMenu.Native;
using System.Windows.Forms;
using static SmartSystemMenu.Native.Constants;
using static SmartSystemMenu.Native.User32;

namespace SmartSystemMenu.Hooks
{
class KeyboardLLHook : Hook
{
private int _msgIdKeyboardLL;
private int _msgIdKeyboardLLHookReplaced;

public event EventHandler<EventArgs> HookReplaced;
public event EventHandler<BasicHookEventArgs> KeyboardLLEvent;

Expand All @@ -18,14 +16,12 @@ public KeyboardLLHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dra

protected override void OnStart()
{
_msgIdKeyboardLL = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_KEYBOARDLL");
_msgIdKeyboardLLHookReplaced = RegisterWindowMessage("SMART_SYSTEM_MENU_HOOK_KEYBOARDLL_REPLACED");

if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(_msgIdKeyboardLL, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(_msgIdKeyboardLLHookReplaced, Constants.MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_KEYBOARDLL, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_KEYBOARDLL_REPLACED, MSGFLT_ADD);
}

NativeHookMethods.InitializeKeyboardLLHook(0, _handle, _dragByMouseMenuItem);
}

Expand All @@ -34,16 +30,22 @@ protected override void OnStop()
NativeHookMethods.UninitializeKeyboardLLHook();
}

public override void ProcessWindowMessage(ref System.Windows.Forms.Message m)
public override void ProcessWindowMessage(ref Message m)
{
if (m.Msg == _msgIdKeyboardLL)
switch (m.Msg)
{
RaiseEvent(KeyboardLLEvent, new BasicHookEventArgs(m.WParam, m.LParam));
}
else if (m.Msg == _msgIdKeyboardLLHookReplaced)
{
RaiseEvent(HookReplaced, EventArgs.Empty);
case WM_SSM_HOOK_KEYBOARDLL:
{
RaiseEvent(KeyboardLLEvent, new BasicHookEventArgs(m.WParam, m.LParam));
}
break;

case WM_SSM_HOOK_KEYBOARDLL_REPLACED:
{
RaiseEvent(HookReplaced, EventArgs.Empty);
}
break;
}
}
}
}
}
Loading

0 comments on commit dbbf020

Please sign in to comment.