diff --git a/SmartSystemMenu/Forms/MainForm.cs b/SmartSystemMenu/Forms/MainForm.cs index 32365b4..4e92380 100644 --- a/SmartSystemMenu/Forms/MainForm.cs +++ b/SmartSystemMenu/Forms/MainForm.cs @@ -128,7 +128,7 @@ 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; } @@ -136,31 +136,8 @@ protected override void OnLoad(EventArgs e) 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); @@ -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); @@ -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); } } } @@ -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) @@ -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) @@ -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; diff --git a/SmartSystemMenu/Forms/SettingsForm.cs b/SmartSystemMenu/Forms/SettingsForm.cs index 2d455bd..0f6cad0 100644 --- a/SmartSystemMenu/Forms/SettingsForm.cs +++ b/SmartSystemMenu/Forms/SettingsForm.cs @@ -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]; @@ -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) @@ -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)gvHotkeys.Tag; settings.Closer.Key1 = _closerSettings.Key1; settings.Closer.Key2 = _closerSettings.Key2; diff --git a/SmartSystemMenu/Hooks/CBTHook.cs b/SmartSystemMenu/Hooks/CBTHook.cs index 07dce76..6aa271f 100644 --- a/SmartSystemMenu/Hooks/CBTHook.cs +++ b/SmartSystemMenu/Hooks/CBTHook.cs @@ -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; diff --git a/SmartSystemMenu/Hooks/CallWndProcHook.cs b/SmartSystemMenu/Hooks/CallWndProcHook.cs index 892dec3..1e699da 100644 --- a/SmartSystemMenu/Hooks/CallWndProcHook.cs +++ b/SmartSystemMenu/Hooks/CallWndProcHook.cs @@ -10,7 +10,8 @@ class CallWndProcHook : Hook private IntPtr _cacheHandle; private IntPtr _cacheMessage; - public event EventHandler CallWndProc; + public event EventHandler SysCommand; + public event EventHandler InitMenu; public CallWndProcHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragByMouseMenuItem) { @@ -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); @@ -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)); + } } } } \ No newline at end of file diff --git a/SmartSystemMenu/Hooks/GetMsgHook.cs b/SmartSystemMenu/Hooks/GetMsgHook.cs index b9db692..87ceb76 100644 --- a/SmartSystemMenu/Hooks/GetMsgHook.cs +++ b/SmartSystemMenu/Hooks/GetMsgHook.cs @@ -10,7 +10,8 @@ class GetMsgHook : Hook private IntPtr _cacheHandle; private IntPtr _cacheMessage; - public event EventHandler GetMsg; + public event EventHandler SysCommand; + public event EventHandler InitMenu; public GetMsgHook(IntPtr handle, int dragByMouseMenuItem) : base(handle, dragByMouseMenuItem) { @@ -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); @@ -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)); + } } } } \ No newline at end of file diff --git a/SmartSystemMenu/Hooks/SysCommandEventArgs.cs b/SmartSystemMenu/Hooks/SysCommandEventArgs.cs index b9c3cc9..a3c9a87 100644 --- a/SmartSystemMenu/Hooks/SysCommandEventArgs.cs +++ b/SmartSystemMenu/Hooks/SysCommandEventArgs.cs @@ -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; } diff --git a/SmartSystemMenu/Hooks/WndProcEventArgs.cs b/SmartSystemMenu/Hooks/WndProcEventArgs.cs deleted file mode 100644 index e3e01b9..0000000 --- a/SmartSystemMenu/Hooks/WndProcEventArgs.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace SmartSystemMenu.Hooks -{ - class WndProcEventArgs : EventArgs - { - public IntPtr Handle { get; } - - public IntPtr Message { get; } - - public IntPtr WParam { get; } - - public IntPtr LParam { get; } - - public WndProcEventArgs(IntPtr handle, IntPtr message, IntPtr wParam, IntPtr lParam) - { - Handle = handle; - Message = message; - WParam = wParam; - LParam = lParam; - } - } -} diff --git a/SmartSystemMenu/Native/Constants.cs b/SmartSystemMenu/Native/Constants.cs index 64effac..d6da7f4 100644 --- a/SmartSystemMenu/Native/Constants.cs +++ b/SmartSystemMenu/Native/Constants.cs @@ -82,20 +82,22 @@ static class Constants public const int WM_SSM_HOOK_HCBT_MINMAX = WM_APP + 0x0203; public const int WM_SSM_HOOK_HCBT_MOVESIZE = WM_APP + 0x0204; public const int WM_SSM_HOOK_HCBT_ACTIVATE = WM_APP + 0x0205; - public const int WM_SSM_HOOK_CALLWNDPROC = WM_APP + 0x0206; - public const int WM_SSM_HOOK_CALLWNDPROC_PARAMS = WM_APP + 0x0207; - public const int WM_SSM_HOOK_GETMSG = WM_APP + 0x0208; - public const int WM_SSM_HOOK_GETMSG_PARAMS = WM_APP + 0x0209; - public const int WM_SSM_HOOK_KEYBOARD = WM_APP + 0x020A; - public const int WM_SSM_HOOK_KEYBOARD_REPLACED = WM_APP + 0x020B; - public const int WM_SSM_HOOK_KEYBOARDLL = WM_APP + 0x020C; - public const int WM_SSM_HOOK_KEYBOARDLL_REPLACED = WM_APP + 0x020D; - public const int WM_SSM_HOOK_MOUSE = WM_APP + 0x020E; - public const int WM_SSM_HOOK_MOUSE_REPLACED = WM_APP + 0x020F; - public const int WM_SSM_HOOK_MOUSELL = WM_APP + 0x0210; - public const int WM_SSM_HOOK_MOUSELL_REPLACED = WM_APP + 0x0211; - public const int WM_SSM_HOOK_HSHELL_WINDOWCREATED = WM_APP + 0x0212; - public const int WM_SSM_HOOK_HSHELL_WINDOWDESTROYED = WM_APP + 0x0213; + public const int WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND = WM_APP + 0x0206; + public const int WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND_PARAMS = WM_APP + 0x0207; + public const int WM_SSM_HOOK_CALLWNDPROC_INITMENU = WM_APP + 0x0208; + public const int WM_SSM_HOOK_GETMSG_SYSCOMMAND = WM_APP + 0x0209; + public const int WM_SSM_HOOK_GETMSG_SYSCOMMAND_PARAMS = WM_APP + 0x020A; + public const int WM_SSM_HOOK_GETMSG_INITMENU = WM_APP + 0x020B; + public const int WM_SSM_HOOK_KEYBOARD = WM_APP + 0x020C; + public const int WM_SSM_HOOK_KEYBOARD_REPLACED = WM_APP + 0x020D; + public const int WM_SSM_HOOK_KEYBOARDLL = WM_APP + 0x020E; + public const int WM_SSM_HOOK_KEYBOARDLL_REPLACED = WM_APP + 0x020F; + public const int WM_SSM_HOOK_MOUSE = WM_APP + 0x0210; + public const int WM_SSM_HOOK_MOUSE_REPLACED = WM_APP + 0x0211; + public const int WM_SSM_HOOK_MOUSELL = WM_APP + 0x0212; + public const int WM_SSM_HOOK_MOUSELL_REPLACED = WM_APP + 0x0213; + public const int WM_SSM_HOOK_HSHELL_WINDOWCREATED = WM_APP + 0x0214; + public const int WM_SSM_HOOK_HSHELL_WINDOWDESTROYED = WM_APP + 0x0215; // SetWindowPos public const uint SWP_NOSIZE = 0x0001; diff --git a/SmartSystemMenu/Settings/SmartSystemMenuSettings.cs b/SmartSystemMenu/Settings/SmartSystemMenuSettings.cs index bb799b0..f1ba53f 100644 --- a/SmartSystemMenu/Settings/SmartSystemMenuSettings.cs +++ b/SmartSystemMenu/Settings/SmartSystemMenuSettings.cs @@ -11,7 +11,11 @@ namespace SmartSystemMenu.Settings { public class SmartSystemMenuSettings : ICloneable { - public IList ProcessExclusions { get; private set; } + public IList ExcludedProcessNames { get; private set; } + + public IList InitEventProcessNames { get; private set; } + + public IList NoRestoreMenuProcessNames { get; private set; } public MenuItems MenuItems { get; private set; } @@ -34,7 +38,9 @@ public class SmartSystemMenuSettings : ICloneable public SmartSystemMenuSettings() { - ProcessExclusions = new List(); + ExcludedProcessNames = new List(); + InitEventProcessNames = new List(); + NoRestoreMenuProcessNames = new List(); MenuItems = new MenuItems(); Closer = new CloserSettings(); Dimmer = new DimmerSettings(); @@ -50,9 +56,19 @@ public object Clone() { var settings = new SmartSystemMenuSettings(); - foreach (var processExclusion in ProcessExclusions) + foreach (var processName in ExcludedProcessNames) + { + settings.ExcludedProcessNames.Add(processName); + } + + foreach (var processName in InitEventProcessNames) { - settings.ProcessExclusions.Add(processExclusion); + settings.InitEventProcessNames.Add(processName); + } + + foreach (var processName in NoRestoreMenuProcessNames) + { + settings.NoRestoreMenuProcessNames.Add(processName); } foreach (var menuItem in MenuItems.WindowSizeItems) @@ -122,7 +138,17 @@ public bool Equals(SmartSystemMenuSettings other) return false; } - if (ProcessExclusions.Count != other.ProcessExclusions.Count) + if (ExcludedProcessNames.Count != other.ExcludedProcessNames.Count) + { + return false; + } + + if (InitEventProcessNames.Count != other.InitEventProcessNames.Count) + { + return false; + } + + if (NoRestoreMenuProcessNames.Count != other.NoRestoreMenuProcessNames.Count) { return false; } @@ -142,9 +168,25 @@ public bool Equals(SmartSystemMenuSettings other) return false; } - for (var i = 0; i < ProcessExclusions.Count; i++) + for (var i = 0; i < ExcludedProcessNames.Count; i++) + { + if (string.Compare(ExcludedProcessNames[i], other.ExcludedProcessNames[i], StringComparison.CurrentCultureIgnoreCase) != 0) + { + return false; + } + } + + for (var i = 0; i < InitEventProcessNames.Count; i++) { - if (string.Compare(ProcessExclusions[i], other.ProcessExclusions[i], StringComparison.CurrentCultureIgnoreCase) != 0) + if (string.Compare(InitEventProcessNames[i], other.InitEventProcessNames[i], StringComparison.CurrentCultureIgnoreCase) != 0) + { + return false; + } + } + + for (var i = 0; i < NoRestoreMenuProcessNames.Count; i++) + { + if (string.Compare(NoRestoreMenuProcessNames[i], other.NoRestoreMenuProcessNames[i], StringComparison.CurrentCultureIgnoreCase) != 0) { return false; } @@ -260,9 +302,19 @@ public override int GetHashCode() { var hashCode = 0; - foreach (var processExclusion in ProcessExclusions) + foreach (var processName in ExcludedProcessNames) { - hashCode ^= processExclusion.GetHashCode(); + hashCode ^= processName.GetHashCode(); + } + + foreach (var processName in InitEventProcessNames) + { + hashCode ^= processName.GetHashCode(); + } + + foreach (var processName in NoRestoreMenuProcessNames) + { + hashCode ^= processName.GetHashCode(); } foreach (var item in MenuItems.WindowSizeItems) @@ -311,12 +363,24 @@ public static SmartSystemMenuSettings Read(string fileName, string languageFileN var document = XDocument.Load(fileName); var languageDocument = XDocument.Load(languageFileName); - settings.ProcessExclusions = document + settings.ExcludedProcessNames = document .XPathSelectElements("/smartSystemMenu/processExclusions/processName") .Where(x => !string.IsNullOrWhiteSpace(x.Value)) .Select(x => x.Value.ToLower()) .ToList(); + settings.InitEventProcessNames = document + .XPathSelectElements("/smartSystemMenu/createMenuOnInitEvent/processName") + .Where(x => !string.IsNullOrWhiteSpace(x.Value)) + .Select(x => x.Value.ToLower()) + .ToList(); + + settings.NoRestoreMenuProcessNames = document + .XPathSelectElements("/smartSystemMenu/noRestoreMenuOnExit/processName") + .Where(x => !string.IsNullOrWhiteSpace(x.Value)) + .Select(x => x.Value.ToLower()) + .ToList(); + settings.MenuItems.WindowSizeItems = document .XPathSelectElements("/smartSystemMenu/menuItems/windowSizeItems/item") .Select(x => new WindowSizeMenuItem @@ -502,7 +566,9 @@ public static void Save(string fileName, SmartSystemMenuSettings settings) { var document = new XDocument(); document.Add(new XElement("smartSystemMenu", - new XElement("processExclusions", settings.ProcessExclusions.Select(x => new XElement("processName", x))), + new XElement("processExclusions", settings.ExcludedProcessNames.Select(x => new XElement("processName", x))), + new XElement("createMenuOnInitEvent", settings.InitEventProcessNames.Select(x => new XElement("processName", x))), + new XElement("noRestoreMenuOnExit", settings.NoRestoreMenuProcessNames.Select(x => new XElement("processName", x))), new XElement("menuItems", new XElement("items", settings.MenuItems.Items.Select(x => new XElement("item", new XAttribute("type", x.Type.ToString()), diff --git a/SmartSystemMenu/SmartSystemMenu.csproj b/SmartSystemMenu/SmartSystemMenu.csproj index fce00d0..ffd8b11 100644 --- a/SmartSystemMenu/SmartSystemMenu.csproj +++ b/SmartSystemMenu/SmartSystemMenu.csproj @@ -253,10 +253,9 @@ - - + Form diff --git a/SmartSystemMenu/SmartSystemMenu.xml b/SmartSystemMenu/SmartSystemMenu.xml index 24ffb4a..2814530 100644 --- a/SmartSystemMenu/SmartSystemMenu.xml +++ b/SmartSystemMenu/SmartSystemMenu.xml @@ -1,13 +1,21 @@  - discord.exe slack.exe dllhost.exe sihost.exe code.exe smartscreen.exe + + discord.exe + chrome.exe + msedge.exe + WindowsTerminal.exe + + + discord.exe + diff --git a/SmartSystemMenu/SmartSystemMenu64.exe b/SmartSystemMenu/SmartSystemMenu64.exe index 1cadea3..a61cdbc 100644 Binary files a/SmartSystemMenu/SmartSystemMenu64.exe and b/SmartSystemMenu/SmartSystemMenu64.exe differ diff --git a/SmartSystemMenu/SystemMenu.cs b/SmartSystemMenu/SystemMenu.cs index 04b9c19..2dde9b2 100644 --- a/SmartSystemMenu/SystemMenu.cs +++ b/SmartSystemMenu/SystemMenu.cs @@ -142,9 +142,13 @@ public bool Create() return true; } - public void Destroy() + public void Destroy(bool restoreMenu = true) { var menuHandle = GetSystemMenu(WindowHandle, false); + if (menuHandle == IntPtr.Zero) + { + return; + } foreach (var item in _menuItems.Items.Where(x => x.Show)) { @@ -157,10 +161,13 @@ public void Destroy() DeleteMenu(menuHandle, MenuItemId.SC_SEPARATOR_BOTTOM, Constants.MF_BYCOMMAND); - var numberItems = GetMenuItemCount(menuHandle); - if (numberItems == DEFAULT_SYSTEM_MENU_NUMBER_ITEMS) + if (restoreMenu) { - GetSystemMenu(WindowHandle, true); + var numberItems = GetMenuItemCount(menuHandle); + if (numberItems == DEFAULT_SYSTEM_MENU_NUMBER_ITEMS) + { + GetSystemMenu(WindowHandle, true); + } } } @@ -239,6 +246,14 @@ public void UncheckTransparencyMenu() CheckMenuItem(MenuItemId.SC_TRANS_DEFAULT, false); } + public bool IsMenuItem(IntPtr menuHandle, int item) + { + var mmi = new MenuItemInfo(); + mmi.cbSize = (uint)Marshal.SizeOf(mmi); + mmi.fMask = MIIM.ID; + return GetMenuItemInfo(menuHandle, item, false, ref mmi); + } + private string GetTransparencyTitle(int id) => id switch { MenuItemId.SC_TRANS_00 => "0%" + GetTitle("trans_opaque", null, false), @@ -288,13 +303,5 @@ private bool InsertSubMenu(IntPtr menuHandle, IntPtr subMenuHandle, int uPositio return true; } - - private bool IsMenuItem(IntPtr menuHandle, int item) - { - var mmi = new MenuItemInfo(); - mmi.cbSize = (uint)Marshal.SizeOf(mmi); - mmi.fMask = MIIM.ID; - return GetMenuItemInfo(menuHandle, item, false, ref mmi); - } } } \ No newline at end of file diff --git a/SmartSystemMenu/Window.cs b/SmartSystemMenu/Window.cs index 49ed674..0fd1f25 100644 --- a/SmartSystemMenu/Window.cs +++ b/SmartSystemMenu/Window.cs @@ -140,6 +140,8 @@ public bool IsDisabledCloseButton public IWin32Window Win32Window => new Win32WindowWrapper(Handle); + public bool NoRestoreMenu { get; set; } + public Window(IntPtr windowHandle) { Handle = windowHandle; @@ -188,7 +190,7 @@ public void Dispose() if (_isManaged) { RestoreFromSystemTray(); - Menu?.Destroy(); + Menu?.Destroy(!NoRestoreMenu); _menuItemRestore?.Dispose(); _menuItemClose?.Dispose(); _systemTrayMenu?.Dispose(); @@ -681,6 +683,36 @@ public void SetStateMinimizeToTrayAlways(bool minimizeAlways) State.MinimizeToTrayAlways = minimizeAlways; } + public void CheckDefaultMenuItems() + { + var menuItemId = ProcessPriority.GetMenuItemId(); + Menu.CheckMenuItem(menuItemId, true); + if (AlwaysOnTop) + { + Menu.CheckMenuItem(MenuItemId.SC_TOPMOST, true); + } + + if (IsExToolWindow) + { + Menu.CheckMenuItem(MenuItemId.SC_HIDE_FOR_ALT_TAB, true); + } + + if (IsDisabledMinimizeButton) + { + Menu.CheckMenuItem(MenuItemId.SC_DISABLE_MINIMIZE_BUTTON, true); + } + + if (IsDisabledMaximizeButton) + { + Menu.CheckMenuItem(MenuItemId.SC_DISABLE_MAXIMIZE_BUTTON, true); + } + + if (IsDisabledCloseButton) + { + Menu.CheckMenuItem(MenuItemId.SC_DISABLE_CLOSE_BUTTON, true); + } + } + public void ApplyState(WindowState state, SaveSelectedItemsSettings settings, IList sizeItems) { if (state.Width > 0 && state.Height > 0) diff --git a/SmartSystemMenuHook/SmartSystemMenuHook.cpp b/SmartSystemMenuHook/SmartSystemMenuHook.cpp index 54825d7..928c0d7 100644 --- a/SmartSystemMenuHook/SmartSystemMenuHook.cpp +++ b/SmartSystemMenuHook/SmartSystemMenuHook.cpp @@ -336,10 +336,18 @@ static LRESULT CALLBACK CallWndProcHookCallback(int code, WPARAM wparam, LPARAM if (code >= 0) { CWPSTRUCT* pCwpStruct = (CWPSTRUCT*)lparam; - if (pCwpStruct->message == WM_SYSCOMMAND) + switch (pCwpStruct->message) { - SendNotifyMessage(hwndMain, WM_SSM_HOOK_CALLWNDPROC, (WPARAM)pCwpStruct->hwnd, pCwpStruct->message); - SendNotifyMessage(hwndMain, WM_SSM_HOOK_CALLWNDPROC_PARAMS, pCwpStruct->wParam, pCwpStruct->lParam); + case WM_SYSCOMMAND: + { + SendNotifyMessage(hwndMain, WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND, (WPARAM)pCwpStruct->hwnd, pCwpStruct->message); + SendNotifyMessage(hwndMain, WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND_PARAMS, pCwpStruct->wParam, pCwpStruct->lParam); + } break; + + case WM_INITMENUPOPUP: + { + SendNotifyMessage(hwndMain, WM_SSM_HOOK_CALLWNDPROC_INITMENU, (WPARAM)pCwpStruct->hwnd, pCwpStruct->message); + } break; } } @@ -370,17 +378,21 @@ DLLEXPORT void __stdcall UninitializeGetMsgHook() static LRESULT CALLBACK GetMsgHookCallback(int code, WPARAM wparam, LPARAM lparam) { - if (code >= 0) + if (code >= 0 && wparam == PM_REMOVE) { MSG* pMsg = (MSG*)lparam; - if (pMsg->message == WM_SYSCOMMAND && wparam == PM_REMOVE) + switch (pMsg->message) { - //TCHAR buf[256]; - //int error = GetLastError(); - //wsprintf(buf, L"WM_SYSCOMMAND, Hook, WParam = %d", pMsg->wParam); - //OutputDebugString(buf); - SendNotifyMessage(hwndMain, WM_SSM_HOOK_GETMSG, (WPARAM)pMsg->hwnd, pMsg->message); - SendNotifyMessage(hwndMain, WM_SSM_HOOK_GETMSG_PARAMS, pMsg->wParam, pMsg->lParam); + case WM_SYSCOMMAND: + { + SendNotifyMessage(hwndMain, WM_SSM_HOOK_GETMSG_SYSCOMMAND, (WPARAM)pMsg->hwnd, pMsg->message); + SendNotifyMessage(hwndMain, WM_SSM_HOOK_GETMSG_SYSCOMMAND_PARAMS, pMsg->wParam, pMsg->lParam); + } break; + + case WM_INITMENUPOPUP: + { + SendNotifyMessage(hwndMain, WM_SSM_HOOK_GETMSG_INITMENU, (WPARAM)pMsg->hwnd, pMsg->message); + } break; } } diff --git a/SmartSystemMenuHook/SmartSystemMenuHook.h b/SmartSystemMenuHook/SmartSystemMenuHook.h index f7c6311..6355726 100644 --- a/SmartSystemMenuHook/SmartSystemMenuHook.h +++ b/SmartSystemMenuHook/SmartSystemMenuHook.h @@ -3,25 +3,27 @@ #include -#define WM_SSM_HOOK_HCBT_CREATEWND WM_APP + 0x0201 -#define WM_SSM_HOOK_HCBT_DESTROYWND WM_APP + 0x0202 -#define WM_SSM_HOOK_HCBT_MINMAX WM_APP + 0x0203 -#define WM_SSM_HOOK_HCBT_MOVESIZE WM_APP + 0x0204 -#define WM_SSM_HOOK_HCBT_ACTIVATE WM_APP + 0x0205 -#define WM_SSM_HOOK_CALLWNDPROC WM_APP + 0x0206 -#define WM_SSM_HOOK_CALLWNDPROC_PARAMS WM_APP + 0x0207 -#define WM_SSM_HOOK_GETMSG WM_APP + 0x0208 -#define WM_SSM_HOOK_GETMSG_PARAMS WM_APP + 0x0209 -#define WM_SSM_HOOK_KEYBOARD WM_APP + 0x020A -#define WM_SSM_HOOK_KEYBOARD_REPLACED WM_APP + 0x020B -#define WM_SSM_HOOK_KEYBOARDLL WM_APP + 0x020C -#define WM_SSM_HOOK_KEYBOARDLL_REPLACED WM_APP + 0x020D -#define WM_SSM_HOOK_MOUSE WM_APP + 0x020E -#define WM_SSM_HOOK_MOUSE_REPLACED WM_APP + 0x020F -#define WM_SSM_HOOK_MOUSELL WM_APP + 0x0210 -#define WM_SSM_HOOK_MOUSELL_REPLACED WM_APP + 0x0211 -#define WM_SSM_HOOK_HSHELL_WINDOWCREATED WM_APP + 0x0212 -#define WM_SSM_HOOK_HSHELL_WINDOWDESTROYED WM_APP + 0x0213 +#define WM_SSM_HOOK_HCBT_CREATEWND WM_APP + 0x0201 +#define WM_SSM_HOOK_HCBT_DESTROYWND WM_APP + 0x0202 +#define WM_SSM_HOOK_HCBT_MINMAX WM_APP + 0x0203 +#define WM_SSM_HOOK_HCBT_MOVESIZE WM_APP + 0x0204 +#define WM_SSM_HOOK_HCBT_ACTIVATE WM_APP + 0x0205 +#define WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND WM_APP + 0x0206 +#define WM_SSM_HOOK_CALLWNDPROC_SYSCOMMAND_PARAMS WM_APP + 0x0207 +#define WM_SSM_HOOK_CALLWNDPROC_INITMENU WM_APP + 0x0208 +#define WM_SSM_HOOK_GETMSG_SYSCOMMAND WM_APP + 0x0209 +#define WM_SSM_HOOK_GETMSG_SYSCOMMAND_PARAMS WM_APP + 0x020A +#define WM_SSM_HOOK_GETMSG_INITMENU WM_APP + 0x020B +#define WM_SSM_HOOK_KEYBOARD WM_APP + 0x020C +#define WM_SSM_HOOK_KEYBOARD_REPLACED WM_APP + 0x020D +#define WM_SSM_HOOK_KEYBOARDLL WM_APP + 0x020E +#define WM_SSM_HOOK_KEYBOARDLL_REPLACED WM_APP + 0x020F +#define WM_SSM_HOOK_MOUSE WM_APP + 0x0210 +#define WM_SSM_HOOK_MOUSE_REPLACED WM_APP + 0x0211 +#define WM_SSM_HOOK_MOUSELL WM_APP + 0x0212 +#define WM_SSM_HOOK_MOUSELL_REPLACED WM_APP + 0x0213 +#define WM_SSM_HOOK_HSHELL_WINDOWCREATED WM_APP + 0x0214 +#define WM_SSM_HOOK_HSHELL_WINDOWDESTROYED WM_APP + 0x0215 #define DLLEXPORT extern "C" __declspec(dllexport)