Add a custom menu item in the system menu
The following is my code. I saw an article that seems to be MSDN .. I've seen it. I am very unfortunate ... I don't know if this article is too arbitrary .. Still. Net wings have not been long. .. (My E Wen is very poor, if you translate the wrong place, everyone doesn't know, give some face ...) above: "If you want to add new to the Control Box (SYSTEM) MENU Items, using .NET itself, there is no way to implement. Very lucky, in any case, use a little Windowsapi to get the same result. ".... (complaint: Rely ..... this is also lucky ?? ???????????? If the advanced function uses the API, it is necessary to C # .Winform Do it ????????????) First, You NEED TO INCLUDE THE SYSTEM. Runtime.InteropServices Namespace: (Step 1, you need to include System.Runtime.InteropServices namespace.) Next, Define the functions Necessary to add items to the system menu: (Next, definition ... Add item to the system menu ... The function ....) is: [C # .NET] [DLLIMPORT ("User32.dll")] private static extern int getSystemMenu (int hWnd, int creation); [DLLIMPORT ("User32.dll ")] private static extern int AppendMenu (int hMenu, int Flagsw, int IDNewItem, string lpNewItem); [Visual Basic .NET] Private Declare Function GetSystemMenu Lib" user32.dll "(ByVal hwnd As Integer, _ByVal bRevert As Integer) As IntegerPrivate Declare Function appendmenu lib "user32.dll" alias "appendmenua" (Byval HMenu As INTEGER, _BYVAL FLAGSW AS INTEGER, IDNEWITEM AS INT Eger, byval lpnewItem As String) AS INTEGER (the author said .. He has no way to understand why 'alias' alias "appendmenua" is required in VB.NET, if you don't include it, .NET will throw Out of an abnormality, when calling appendmenu, you can't find Entry Point in user32.dll ...) My entry point is awkward. . forget. . Entry point? I don't know. Anyway, there is wrong. .
Adding the items to the menu
Adding items to the menu is now pretty simple. To add a separator, you simply pass the arguments 0xA00, 0 and null (Nothing in Visual Basic) into the AppendMenu function. To create a menu item, you pass in 0, the ID to Assign to the New The Menu Item (EG, 100) And A String Containing The Text To Display for the Menu Item. for example: Adding a project is easy to add a project to the menu. To add a separator, your simple pass parameters 0xA00, 0 and NULL (in VB are Nothing) to the appendmenu function. To create a menu item, you pass 0, assigned to the ID of the new menu item, and a string of the text that is displayed on the menu item. . . For example: [C # .NET] // Get Handle to System Menuint SystemMenu = GetSystemMenu (this.Handle.Toint32 (), 0); // Create a SeparatorAppendmenu (SystemMenu, 0xA00, 0, Null); // Add The About Menu itemAppendMenu (systemMenu, 0, 100, "& About ..."); [Visual Basic .NET] 'Get handle to system menuDim systemMenu As Integer = GetSystemMenu (Me.Handle.ToInt32 (), 0)' Create a separatorAppendMenu (systemMenu , & Ha000, 0, nothing) 'add the About menu itemAppendmenu (SystemMenu, 0, 100, "& About ...") ======================= ===== The above is his code. But I tried it myself. When the separator is added, the parameter 2 can be used in 0. . everything is normal. I haven't figured out why I want to specify 0xA00. . . . Here is my code: use system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.runtime.interopservices;
Namespace contains a summary description of the program {///
///
#REGION Windows Form Designer The code ///
} #Endregion
///
Private Void Form1_Load (Object Sender, System.EventArgs E) {INT M = GetSystemMenu (this.handle.Toint32 (), 0); appendmenu (m, 0, 0, null); appendmenu (m, 0, 901, "c # menu" );} Protected override void WndProc (ref Message M) {base.wndProc (Ref m); if (m.msg == 0x112) {if (m.wpaam.toint32 () == 901) {MessageBox.show (" Click! ");}}}}}