/// <summary> /// wpf标题栏 右键菜单 中添加新项 /// </summary> public partial class MainWindow : Window { private const int WM_SYSCOMMAND = 0x112; private const int MF_STRING = 0x0; private const int MF_SEPARATOR = 0x800; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool AppendMenu(IntPtr hMenu, int uFlags, int uIDNewItem, string lpNewItem); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool InsertMenuItem(IntPtr hMenu, int uPosition, int uFlags, int uIDNewItem, String ipNewItem); private int SYSMENU_ABOUT_ID = 0x1; public MainWindow() { InitializeComponent(); SourceInitialized += MainWindow_SourceInitialized; } #region 给wpf标题栏的右键菜单栏 添加 “关于”菜单项 private void MainWindow_SourceInitialized(object sender, EventArgs e) { var handle = (new WindowInteropHelper(this)).Handle; IntPtr hSysMenu = GetSystemMenu(handle, false); AppendMenu(hSysMenu, MF_SEPARATOR, 0, String.Empty); AppendMenu(hSysMenu, MF_STRING, SYSMENU_ABOUT_ID, "关于"); } #endregion #region 模拟实现 winform程序中的WndProc方法 protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { // Handle messages... if ((msg == WM_SYSCOMMAND) && ((int)wParam == SYSMENU_ABOUT_ID)) { //表示监听到 关于菜单 按钮的 点击事件,并执行以下操作 System.Windows.MessageBox.Show("good"); } return IntPtr.Zero; } #endregion }
winform中的实现方式
private const int WM_SYSCOMMAND = 0x112; private const int MF_STRING = 0x0; private const int MF_SEPARATOR = 0x800; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool AppendMenu(IntPtr hMenu, int uFlags, int uIDNewItem, string lpNewItem); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool InsertMenuItem(IntPtr hMenu, int uPosition, int uFlags, int uIDNewItem, String ipNewItem); private int SYSMENU_ABOUT_ID = 0x1; protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); //TestForm tsf = new TestForm(); IntPtr hSysMenu = GetSystemMenu(this.Handle, false); AppendMenu(hSysMenu, MF_SEPARATOR, 0, String.Empty); AppendMenu(hSysMenu, MF_STRING, SYSMENU_ABOUT_ID, "关于 (&A) ..."); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if ((m.Msg==WM_SYSCOMMAND) && ((int)m.WParam==SYSMENU_ABOUT_ID)) { TestForm testform = new TestForm(); testform.ShowDialog(); } }
参考文章:
<a href='https://download.csdn.net/download/zxc000/8542729'>https://download.csdn.net/download/zxc000/8542729</a> <a href='https://blog.csdn.net/DoitPlayer/article/details/72832954'>https://blog.csdn.net/DoitPlayer/article/details/72832954</a> <a href='https://www.cnblogs.com/liunlls/p/wpf-WindowInteropHelper.html'>https://www.cnblogs.com/liunlls/p/wpf-WindowInteropHelper.html</a> <a href='https://blog.csdn.net/lucifinil_s/article/details/6384111'>https://blog.csdn.net/lucifinil_s/article/details/6384111</a>