J'ai trouvé un fil sur MSDN qui montre comment ajouter un élément au menu contextuel d'une barre de titre des formulaires Windows.
Malheureusement, il ne montre pas comment enregistrer un événement avec l'élément de menu personnalisé et j'ai été incapable de déterminez comment le faire. Vous trouverez ci-dessous une application d'échantillon pouvant être copiée et collée dans une nouvelle application Windows Forms. Comment puis-je compléter l'échantillon? P>
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; } } }
3 Réponses :
Vous devez remplacer le WNDProc a> méthode et intercepter l'identifiant de votre nouveau menu. Essayez ceci: p>
pour un séparateur Il suffit d'ajouter: et dans form_load: p> InsertMenu(MenuHandle, 7, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu seperator
Je suis allé de l'avant et je viens d'ajouter les éléments nécessaires au code d'échantillon pour enregistrer le WNDProc. Cela répond à la question fondamentale de l'enregistrement du WNDProc sans modifier le code autant que la solution précédente. (Il conserve le menu ajouté en haut du menu Système). } P> P>