2021-04-24 02:59:39 +02:00
|
|
|
|
/******************************************************************************/
|
|
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
|
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
|
|
|
|
namespace com.muldersoft.slunkcrypt.gui.utils
|
|
|
|
|
{
|
|
|
|
|
class SystemMenu : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private const int WM_SYSCOMMAND = 0x112;
|
|
|
|
|
public delegate void SystemMenuActionHandler(SystemMenu sender, uint menuId);
|
|
|
|
|
|
2021-11-11 21:36:13 +01:00
|
|
|
|
private readonly SystemMenuActionHandler m_handler;
|
2021-04-24 02:59:39 +02:00
|
|
|
|
private readonly HwndSource m_hwndSource;
|
2021-11-11 21:36:13 +01:00
|
|
|
|
private readonly Dictionary<uint, string> m_menuItems = new Dictionary<uint, string>();
|
2021-04-24 02:59:39 +02:00
|
|
|
|
|
|
|
|
|
private bool m_firstItem = true;
|
|
|
|
|
private uint m_nextMenuId = 100;
|
|
|
|
|
|
|
|
|
|
private volatile bool m_disposed = false;
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Constructor
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
2021-11-11 21:36:13 +01:00
|
|
|
|
public SystemMenu(Window window, SystemMenuActionHandler handler)
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
|
|
|
|
if (ReferenceEquals(window, null))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Window must not be null!");
|
|
|
|
|
}
|
2021-11-11 21:36:13 +01:00
|
|
|
|
if (ReferenceEquals(m_handler = handler, null))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("SystemMenuActionHandler must not be null!");
|
|
|
|
|
}
|
2021-04-24 02:59:39 +02:00
|
|
|
|
window.Dispatcher.VerifyAccess();
|
|
|
|
|
m_hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle);
|
|
|
|
|
m_hwndSource.AddHook(WndProcHook);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Public methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
2021-11-11 21:36:13 +01:00
|
|
|
|
public uint AppendMenu(string captionStr)
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
|
|
|
|
if (m_disposed)
|
|
|
|
|
{
|
|
|
|
|
throw new ObjectDisposedException("Already disposed!");
|
|
|
|
|
}
|
|
|
|
|
m_hwndSource.Dispatcher.VerifyAccess();
|
|
|
|
|
IntPtr hSysMenu = GetSystemMenu(m_hwndSource.Handle, false);
|
|
|
|
|
if (hSysMenu != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
if (m_firstItem)
|
|
|
|
|
{
|
|
|
|
|
if (AppendMenu(hSysMenu, Native.MenuFlags.MF_SEPARATOR, 0, string.Empty))
|
|
|
|
|
{
|
|
|
|
|
m_firstItem = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (AppendMenu(hSysMenu, Native.MenuFlags.MF_STRING, m_nextMenuId, captionStr))
|
|
|
|
|
{
|
|
|
|
|
uint menuItemId = m_nextMenuId++;
|
2021-11-11 21:36:13 +01:00
|
|
|
|
m_menuItems.Add(menuItemId, captionStr);
|
2021-04-24 02:59:39 +02:00
|
|
|
|
return menuItemId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ModifyMenu(uint menuId, bool isChecked)
|
|
|
|
|
{
|
|
|
|
|
if (m_disposed)
|
|
|
|
|
{
|
|
|
|
|
throw new ObjectDisposedException("Already disposed!");
|
|
|
|
|
}
|
|
|
|
|
m_hwndSource.Dispatcher.VerifyAccess();
|
2021-11-11 21:36:13 +01:00
|
|
|
|
string captionStr;
|
|
|
|
|
if (m_menuItems.TryGetValue(menuId, out captionStr))
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
|
|
|
|
IntPtr hSysMenu = GetSystemMenu(m_hwndSource.Handle, false);
|
|
|
|
|
if (hSysMenu != IntPtr.Zero)
|
|
|
|
|
{
|
2021-11-11 21:36:13 +01:00
|
|
|
|
return ModifyMenu(hSysMenu, menuId, Native.MenuFlags.MF_BYCOMMAND | (isChecked ? Native.MenuFlags.MF_CHECKED : Native.MenuFlags.MF_UNCHECKED), menuId, captionStr);
|
2021-04-24 02:59:39 +02:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Menu item ID is unknown!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Internal methods
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
private IntPtr WndProcHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
|
|
|
|
{
|
|
|
|
|
if (msg == WM_SYSCOMMAND)
|
|
|
|
|
{
|
2021-11-11 21:36:13 +01:00
|
|
|
|
uint menuId;
|
|
|
|
|
if (m_menuItems.ContainsKey(menuId = (uint) wParam.ToInt32()))
|
2021-04-24 02:59:39 +02:00
|
|
|
|
{
|
2021-11-11 21:36:13 +01:00
|
|
|
|
m_handler(this, menuId);
|
2021-04-24 02:59:39 +02:00
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Native.GetSystemMenu(hWnd, bRevert);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool AppendMenu(IntPtr hMenu, Native.MenuFlags uFlags, uint uIDNewItem, string lpNewItem)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Native.AppendMenu(hMenu, uFlags, new UIntPtr(uIDNewItem), lpNewItem);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ModifyMenu(IntPtr hMenu, uint uPosition, Native.MenuFlags uFlags, uint uIDNewItem, string lpNewItem)
|
|
|
|
|
{
|
|
|
|
|
return Native.ModifyMenu(hMenu, uPosition, uFlags, new UIntPtr(uIDNewItem), lpNewItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!m_disposed)
|
|
|
|
|
{
|
|
|
|
|
m_disposed = true;
|
|
|
|
|
m_hwndSource.RemoveHook(WndProcHook);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Event handlers
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
private class Native
|
|
|
|
|
{
|
2022-11-10 00:09:23 +01:00
|
|
|
|
private const string DLL_USER32 = "user32.dll";
|
|
|
|
|
|
2021-04-24 02:59:39 +02:00
|
|
|
|
[Flags]
|
|
|
|
|
public enum MenuFlags : uint
|
|
|
|
|
{
|
|
|
|
|
MF_STRING = 0x0000,
|
|
|
|
|
MF_UNCHECKED = 0x0000,
|
|
|
|
|
MF_BYCOMMAND = 0x0000,
|
|
|
|
|
MF_CHECKED = 0x0008,
|
|
|
|
|
MF_BYPOSITION = 0x0400,
|
|
|
|
|
MF_SEPARATOR = 0x0800
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 00:09:23 +01:00
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "GetSystemMenu", ExactSpelling = true)]
|
2021-04-24 02:59:39 +02:00
|
|
|
|
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
|
|
|
|
|
|
2022-11-10 00:09:23 +01:00
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "AppendMenuW", ExactSpelling = true)]
|
2021-04-24 02:59:39 +02:00
|
|
|
|
public static extern bool AppendMenu(IntPtr hMenu, MenuFlags uFlags, UIntPtr uIDNewItem, string lpNewItem);
|
|
|
|
|
|
2022-11-10 00:09:23 +01:00
|
|
|
|
[DllImport(DLL_USER32, CharSet = CharSet.Unicode, EntryPoint = "ModifyMenuW", ExactSpelling = true)]
|
2021-04-24 02:59:39 +02:00
|
|
|
|
public static extern bool ModifyMenu(IntPtr hMenu, uint uPosition, MenuFlags uFlags, UIntPtr uIDNewItem, string lpNewItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|