日期:2014-05-18  浏览次数:20916 次

【C#技术】向窗体系统菜单添加自定义菜单
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        [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 const Int32 WM_SYSCOMMAND = 0x112;
        public const Int32 MF_SEPARATOR = 0x800;
        public const Int32 MF_BYPOSITION = 0x400;
        public const Int32 MF_STRING = 0x0;
        public const Int32 MF_REMOVE = 0x1000;
        public const Int32 IDM_EDITFUNDS = 1000;
        public const Int32 IDM_ANALYZE = 1001;
        //public const Int32 MF_CLOSE = &HF060;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IntPtr sysMenuHandle = GetSystemMenu(this.Handle, false);
            InsertMenu(sysMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);
            InsertMenu(sysMenuHandle, 6, MF_BYPOSITION, IDM_EDITFUNDS, "测试1");
            InsertMenu(sysMenuHandle, 7, MF_BYPOSITION, IDM_ANALYZE, "测试2");
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                switch (m.WParam.ToInt32())
                {
                    case IDM_EDITFUNDS:
                        MessageBox.Show("您单击了 测试1");
                        return;
                    case IDM_ANALYZE:
                        MessageBox.Show("您单击了 测试2");
                        return;
                    default:
                        break;
                }
            }
            base.WndProc(ref m);
        }
    }
}



另附一张我用C#高仿Adobe Photoshop CS6界面,项目还在完善中……



------解决方案--------------------
没人顶吗?
------解决方案--------------------

图看不到