c# - 将自定义上下文菜单项添加到 Windows 窗体标题栏

标签 c# winforms winapi winforms-interop

我找到了 a thread在 MSDN 上展示了如何将项目添加到 Windows 窗体标题栏的上下文菜单。

不幸的是,它没有显示如何使用自定义菜单项注册事件,我一直无法弄清楚如何去做。下面是一个示例应用程序,可以将其复制并粘贴到新的 Windows 窗体应用程序中。如何完成示例?

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            IntPtr hMenu = GetSystemMenu(Handle, false);
            if (hMenu != IntPtr.Zero)
            {
                var menuInfo = new MENUITEMINFO
                   {
                       cbSize = (uint) Marshal.SizeOf(typeof (MENUITEMINFO)),
                       cch = 255,
                       dwTypeData = "Test Item",
                       fMask = 0x1 | 0x2 | 0x10,
                       fState = 0,
                       fType = 0x0
                   };

                InsertMenuItem(hMenu, 0, true, ref menuInfo);
                DrawMenuBar(Handle);
            }
        }

        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("user32.dll")]
        static extern bool DrawMenuBar(IntPtr hWnd);
        [DllImport("user32.dll")]
        static extern bool InsertMenuItem(IntPtr hMenu, uint uItem,
                          bool fByPosition, [In] ref MENUITEMINFO lpmii);


        [StructLayout(LayoutKind.Sequential)]
        public struct MENUITEMINFO
        {
            public uint cbSize;
            public uint fMask;
            public uint fType;
            public uint fState;
            public uint wID;
            public IntPtr hSubMenu;
            public IntPtr hbmpChecked;
            public IntPtr hbmpUnchecked;
            public IntPtr dwItemData;
            public string dwTypeData;
            public uint cch;
            public IntPtr hbmpItem;
        }
    }
}

最佳答案

您必须覆盖 WndProc方法并拦截新菜单的 ID。

试试这个:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication11
{
  public partial class Form1 : Form
  {
    public const Int32 WM_SYSCOMMAND = 0x112;
    public const Int32 MF_BYPOSITION = 0x400;
    public const Int32 MYMENU1 = 1000;
    public const Int32 MUMENU2 = 1001;

    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    [DllImport("user32.dll")]
    private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);

    public Form1()
    {
      InitializeComponent();
    }

    protected override void WndProc(ref Message msg)
    {
      if (msg.Msg == WM_SYSCOMMAND)
      {
        switch (msg.WParam.ToInt32())
        {
          case MYMENU1:
            MessageBox.Show("Hi from My Menu 1¡¡¡¡");
            return;
          case MUMENU2:
            MessageBox.Show("Hi from My Menu 2¡¡¡¡");
            return;
          default:
            break;
        }
      }
      base.WndProc(ref msg);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      IntPtr MenuHandle = GetSystemMenu(this.Handle, false);
      InsertMenu(MenuHandle, 5, MF_BYPOSITION, MYMENU1, "My Menu 1");
      InsertMenu(MenuHandle, 6, MF_BYPOSITION, MUMENU2, "My Menu 2");
    }
  }
}

关于c# - 将自定义上下文菜单项添加到 Windows 窗体标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1557904/

相关文章:

c# - 资源文件中的 WinForms 字符串,在设计器中连接

windows - 阻止非管理员在特定时间段内登录 Windows

c# - 并行或异步 ASP.NET Core C#

c# - 同一类上的两组序列化属性

c# - Windows 窗体应用程序中的内存泄漏

c# - 如何更改 winforms 标签中行与行之间的空格?

windows - DefWindowProc WinApi 的行为

c# - SetWindowPos 不适用于浏览器 - 没有 MainWindowHandle?

c# - 您如何通过其 "name"调用方法?

c# - 什么是与 ExcelDna 一起使用的好的 .Net RefEdit 控件?