Skip to content

Commit

Permalink
Added the ability to insert menu items on WM_INITMENUPOPUP event for …
Browse files Browse the repository at this point in the history
…discord, chrome, terminal and other applications.
  • Loading branch information
AlexanderPro committed Aug 22, 2023
1 parent 3ab66dd commit dbc2455
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 197 deletions.
159 changes: 74 additions & 85 deletions SmartSystemMenu/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,39 +128,16 @@ protected override void OnLoad(EventArgs e)
{
var processPath = window.Process?.GetMainModuleFileName() ?? string.Empty;
var fileName = Path.GetFileName(processPath);
if (!string.IsNullOrEmpty(fileName) && _settings.ProcessExclusions.Contains(fileName.ToLower()))
if (!string.IsNullOrEmpty(fileName) && _settings.ExcludedProcessNames.Contains(fileName.ToLower()) || _settings.InitEventProcessNames.Contains(fileName.ToLower()))
{
continue;
}

var isAdded = window.Menu.Create();
if (isAdded)
{
window.Menu.CheckMenuItem(window.ProcessPriority.GetMenuItemId(), true);
if (window.AlwaysOnTop)
{
window.Menu.CheckMenuItem(MenuItemId.SC_TOPMOST, true);
}

if (window.IsExToolWindow)
{
window.Menu.CheckMenuItem(MenuItemId.SC_HIDE_FOR_ALT_TAB, true);
}

if (window.IsDisabledMinimizeButton)
{
window.Menu.CheckMenuItem(MenuItemId.SC_DISABLE_MINIMIZE_BUTTON, true);
}

if (window.IsDisabledMaximizeButton)
{
window.Menu.CheckMenuItem(MenuItemId.SC_DISABLE_MAXIMIZE_BUTTON, true);
}

if (window.IsDisabledCloseButton)
{
window.Menu.CheckMenuItem(MenuItemId.SC_DISABLE_CLOSE_BUTTON, true);
}
window.CheckDefaultMenuItems();
window.NoRestoreMenu = !string.IsNullOrEmpty(fileName) && _settings.NoRestoreMenuProcessNames.Contains(fileName.ToLower());

var windowClassName = window.GetClassName();
var states = _windowSettings.Find(windowClassName, processPath);
Expand All @@ -173,11 +150,13 @@ protected override void OnLoad(EventArgs e)
}

_callWndProcHook = new CallWndProcHook(Handle, MenuItemId.SC_DRAG_BY_MOUSE);
_callWndProcHook.CallWndProc += WindowProc;
_callWndProcHook.SysCommand += SysCommand;
_callWndProcHook.InitMenu += InitMenu;
_callWndProcHook.Start();

_getMsgHook = new GetMsgHook(Handle, MenuItemId.SC_DRAG_BY_MOUSE);
_getMsgHook.GetMsg += WindowGetMsg;
_getMsgHook.SysCommand += SysCommand;
_getMsgHook.InitMenu += InitMenu;
_getMsgHook.Start();

_shellHook = new ShellHook(Handle, MenuItemId.SC_DRAG_BY_MOUSE);
Expand Down Expand Up @@ -414,64 +393,79 @@ private void WindowCreated(object sender, WindowEventArgs e)
if (e.Handle != IntPtr.Zero && !_windows.Any(w => w.Handle == e.Handle))
{
GetWindowThreadProcessId(e.Handle, out int processId);
var process = SystemUtils.GetProcessByIdSafely(processId);
var processPath = process?.GetMainModuleFileName() ?? string.Empty;
var fileName = Path.GetFileName(processPath);
if (!string.IsNullOrEmpty(fileName) &&
_settings.ExcludedProcessNames.Contains(fileName.ToLower()) || _settings.InitEventProcessNames.Contains(fileName.ToLower()))
{
return;
}

var window = new Window(e.Handle, _settings.MenuItems, _settings.Language);
var filterTitles = new string[] { SHELL_WINDOW_NAME };
bool isWriteProcess;
CreateMenu(window, processId, processPath);
}
}

private void InitMenu(object sender, SysCommandEventArgs e)
{
if (e.WParam != IntPtr.Zero && IsWindowVisible(e.WParam))
{
GetWindowThreadProcessId(e.WParam, out int processId);
var process = SystemUtils.GetProcessByIdSafely(processId);
var processPath = process?.GetMainModuleFileName() ?? string.Empty;
var fileName = Path.GetFileName(processPath);
if (!string.IsNullOrEmpty(fileName) &&
_settings.ExcludedProcessNames.Contains(fileName.ToLower()) || !_settings.InitEventProcessNames.Contains(fileName.ToLower()))
{
return;
}

var window = _windows.FirstOrDefault(w => w.Handle == e.WParam);
if (window == null)
{
window = new Window(e.WParam, _settings.MenuItems, _settings.Language);
CreateMenu(window, processId, processPath);
}
else
{
var systemMenuHandle = window.Menu.MenuHandle;
if (systemMenuHandle != IntPtr.Zero && !window.Menu.IsMenuItem(systemMenuHandle, MenuItemId.SC_SEPARATOR_BOTTOM))
{
CreateMenu(window, processId, processPath);
}
}
}
}

private void CreateMenu(Window window, int processId, string processPath)
{
bool isWriteProcess;
#if WIN32
isWriteProcess = !Environment.Is64BitOperatingSystem || SystemUtils.IsWow64Process(processId);
isWriteProcess = !Environment.Is64BitOperatingSystem || SystemUtils.IsWow64Process(processId);
#else
isWriteProcess = Environment.Is64BitOperatingSystem && !SystemUtils.IsWow64Process(processId);
isWriteProcess = Environment.Is64BitOperatingSystem && !SystemUtils.IsWow64Process(processId);
#endif

if (isWriteProcess && !filterTitles.Any(s => window.GetWindowText() == s))
var filterTitles = new string[] { SHELL_WINDOW_NAME };
if (isWriteProcess && !filterTitles.Any(s => window.GetWindowText() == s))
{
var isAdded = window.Menu.Create();
if (isAdded)
{
var processPath = window.Process?.GetMainModuleFileName() ?? string.Empty;
window.CheckDefaultMenuItems();

var fileName = Path.GetFileName(processPath);
if (!string.IsNullOrEmpty(fileName) && _settings.ProcessExclusions.Contains(fileName.ToLower()))
{
return;
}
window.NoRestoreMenu = !string.IsNullOrEmpty(fileName) && _settings.NoRestoreMenuProcessNames.Contains(fileName.ToLower());

_windows.Add(window);

var isAdded = window.Menu.Create();
if (isAdded)
var windowClassName = window.GetClassName();
var isConsoleClassName = string.Compare(windowClassName, Window.ConsoleClassName, StringComparison.CurrentCulture) == 0;
var states = isConsoleClassName ? _windowSettings.Find(windowClassName) : _windowSettings.Find(windowClassName, processPath);
if (states.Any())
{
var menuItemId = window.ProcessPriority.GetMenuItemId();
window.Menu.CheckMenuItem(menuItemId, true);
if (window.AlwaysOnTop)
{
window.Menu.CheckMenuItem(MenuItemId.SC_TOPMOST, true);
}

if (window.IsExToolWindow)
{
window.Menu.CheckMenuItem(MenuItemId.SC_HIDE_FOR_ALT_TAB, true);
}

if (window.IsDisabledMinimizeButton)
{
window.Menu.CheckMenuItem(MenuItemId.SC_DISABLE_MINIMIZE_BUTTON, true);
}

if (window.IsDisabledMaximizeButton)
{
window.Menu.CheckMenuItem(MenuItemId.SC_DISABLE_MAXIMIZE_BUTTON, true);
}

if (window.IsDisabledCloseButton)
{
window.Menu.CheckMenuItem(MenuItemId.SC_DISABLE_CLOSE_BUTTON, true);
}

_windows.Add(window);

var windowClassName = window.GetClassName();
var isConsoleClassName = string.Compare(windowClassName, Window.ConsoleClassName, StringComparison.CurrentCulture) == 0;
var states = isConsoleClassName ? _windowSettings.Find(windowClassName) : _windowSettings.Find(windowClassName, processPath);
if (states.Any())
{
window.ApplyState(states[0], _settings.SaveSelectedItems, _settings.MenuItems.WindowSizeItems);
window.Menu.CheckMenuItem(MenuItemId.SC_SAVE_SELECTED_ITEMS, true);
}
window.ApplyState(states[0], _settings.SaveSelectedItems, _settings.MenuItems.WindowSizeItems);
window.Menu.CheckMenuItem(MenuItemId.SC_SAVE_SELECTED_ITEMS, true);
}
}
}
Expand Down Expand Up @@ -547,11 +541,6 @@ private void WindowKeyboardEvent(object sender, BasicHookEventArgs e)
}
}

private void WindowProc(object sender, WndProcEventArgs e)
{
WindowGetMsg(sender, new WndProcEventArgs(e.Handle, e.Message, e.WParam, e.LParam));
}

private void WindowActivate(object sender, WindowEventArgs e)
{
if (_dimHandle == e.Handle)
Expand All @@ -560,7 +549,7 @@ private void WindowActivate(object sender, WindowEventArgs e)
}
}

private void WindowGetMsg(object sender, WndProcEventArgs e)
private void SysCommand(object sender, SysCommandEventArgs e)
{
var message = e.Message.ToInt64();
if (message == WM_SYSCOMMAND)
Expand Down Expand Up @@ -1111,7 +1100,7 @@ private void HotKeyHooked(object sender, HotKeyEventArgs e)
{
}

if (!_settings.ProcessExclusions.Contains(processName.ToLower()))
if (!_settings.ExcludedProcessNames.Contains(processName.ToLower()))
{
PostMessage(handle, WM_SYSCOMMAND, (uint)e.MenuItemId, 0);
e.Succeeded = true;
Expand Down
14 changes: 12 additions & 2 deletions SmartSystemMenu/Forms/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void InitializeControls(SmartSystemMenuSettings settings)
btnCancel.Text = settings.Language.GetValue("settings_btn_cancel");
Text = settings.Language.GetValue("settings_form");

foreach (var processExclusion in settings.ProcessExclusions)
foreach (var processExclusion in settings.ExcludedProcessNames)
{
var index = gvProcessExclusions.Rows.Add();
var row = gvProcessExclusions.Rows[index];
Expand Down Expand Up @@ -543,7 +543,7 @@ private void ButtonApplyClick(object sender, EventArgs e)

foreach (DataGridViewRow row in gvProcessExclusions.Rows)
{
settings.ProcessExclusions.Add(row.Cells[0].Value.ToString());
settings.ExcludedProcessNames.Add(row.Cells[0].Value.ToString());
}

foreach (DataGridViewRow row in gvWindowSize.Rows)
Expand All @@ -569,6 +569,16 @@ private void ButtonApplyClick(object sender, EventArgs e)
settings.MenuItems.StartProgramItems.Add((StartProgramMenuItem)row.Tag);
}

foreach (var processName in _settings.InitEventProcessNames)
{
settings.InitEventProcessNames.Add(processName);
}

foreach (var processName in _settings.NoRestoreMenuProcessNames)
{
settings.NoRestoreMenuProcessNames.Add(processName);
}

settings.MenuItems.Items = (IList<Settings.MenuItem>)gvHotkeys.Tag;
settings.Closer.Key1 = _closerSettings.Key1;
settings.Closer.Key2 = _closerSettings.Key2;
Expand Down
2 changes: 1 addition & 1 deletion SmartSystemMenu/Hooks/CBTHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void ProcessWindowMessage(ref Message m)

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

Expand Down
20 changes: 13 additions & 7 deletions SmartSystemMenu/Hooks/CallWndProcHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class CallWndProcHook : Hook
private IntPtr _cacheHandle;
private IntPtr _cacheMessage;

public event EventHandler<WndProcEventArgs> CallWndProc;
public event EventHandler<SysCommandEventArgs> SysCommand;
public event EventHandler<SysCommandEventArgs> InitMenu;

public CallWndProcHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragByMouseMenuItem)
{
Expand All @@ -20,8 +21,9 @@ protected override void OnStart()
{
if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC_PARAMS, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND_PARAMS, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_CALLWNDPROC_INITMENU, MSGFLT_ADD);
}

NativeHookMethods.InitializeCallWndProcHook(0, _handle, _dragByMouseMenuItem);
Expand All @@ -34,20 +36,24 @@ protected override void OnStop()

public override void ProcessWindowMessage(ref Message m)
{
if (m.Msg == WM_SSM_HOOK_CALLWNDPROC)
if (m.Msg == WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND)
{
_cacheHandle = m.WParam;
_cacheMessage = m.LParam;
}
else if (m.Msg == WM_SSM_HOOK_CALLWNDPROC_PARAMS)
else if (m.Msg == WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND_PARAMS)
{
if (CallWndProc != null && _cacheHandle != IntPtr.Zero && _cacheMessage != IntPtr.Zero)
if (SysCommand != null && _cacheHandle != IntPtr.Zero && _cacheMessage != IntPtr.Zero)
{
RaiseEvent(CallWndProc, new WndProcEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
RaiseEvent(SysCommand, new SysCommandEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
}
_cacheHandle = IntPtr.Zero;
_cacheMessage = IntPtr.Zero;
}
else if (m.Msg == WM_SSM_HOOK_CALLWNDPROC_INITMENU)
{
RaiseEvent(InitMenu, new SysCommandEventArgs(IntPtr.Zero, IntPtr.Zero, m.WParam, m.LParam));
}
}
}
}
20 changes: 13 additions & 7 deletions SmartSystemMenu/Hooks/GetMsgHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class GetMsgHook : Hook
private IntPtr _cacheHandle;
private IntPtr _cacheMessage;

public event EventHandler<WndProcEventArgs> GetMsg;
public event EventHandler<SysCommandEventArgs> SysCommand;
public event EventHandler<SysCommandEventArgs> InitMenu;

public GetMsgHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragByMouseMenuItem)
{
Expand All @@ -20,8 +21,9 @@ protected override void OnStart()
{
if (Environment.OSVersion.Version.Major >= 6)
{
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG_PARAMS, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG_SYSCOMMAND, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG_SYSCOMMAND_PARAMS, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SSM_HOOK_GETMSG_INITMENU, MSGFLT_ADD);
}

NativeHookMethods.InitializeGetMsgHook(0, _handle, _dragByMouseMenuItem);
Expand All @@ -34,20 +36,24 @@ protected override void OnStop()

public override void ProcessWindowMessage(ref Message m)
{
if (m.Msg == WM_SSM_HOOK_GETMSG)
if (m.Msg == WM_SSM_HOOK_GETMSG_SYSCOMMAND)
{
_cacheHandle = m.WParam;
_cacheMessage = m.LParam;
}
else if (m.Msg == WM_SSM_HOOK_GETMSG_PARAMS)
else if (m.Msg == WM_SSM_HOOK_GETMSG_SYSCOMMAND_PARAMS)
{
if (GetMsg != null && _cacheHandle != IntPtr.Zero && _cacheMessage != IntPtr.Zero)
if (SysCommand != null && _cacheHandle != IntPtr.Zero && _cacheMessage != IntPtr.Zero)
{
RaiseEvent(GetMsg, new WndProcEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
RaiseEvent(SysCommand, new SysCommandEventArgs(_cacheHandle, _cacheMessage, m.WParam, m.LParam));
}
_cacheHandle = IntPtr.Zero;
_cacheMessage = IntPtr.Zero;
}
else if (m.Msg == WM_SSM_HOOK_GETMSG_INITMENU)
{
RaiseEvent(InitMenu, new SysCommandEventArgs(IntPtr.Zero, IntPtr.Zero, m.WParam, m.LParam));
}
}
}
}
8 changes: 7 additions & 1 deletion SmartSystemMenu/Hooks/SysCommandEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ namespace SmartSystemMenu.Hooks
{
class SysCommandEventArgs : EventArgs
{
public IntPtr Handle { get; }

public IntPtr Message { get; }

public IntPtr WParam { get; }

public IntPtr LParam { get; }

public SysCommandEventArgs(IntPtr wParam, IntPtr lParam)
public SysCommandEventArgs(IntPtr handle, IntPtr message, IntPtr wParam, IntPtr lParam)
{
Handle = handle;
Message = message;
WParam = wParam;
LParam = lParam;
}
Expand Down
Loading

0 comments on commit dbc2455

Please sign in to comment.