-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial
It is recommended to install it using the nuget package
Open the nuget package manager for the corresponding project in your IDE, search for "Win32Menu", select it and install it
If you are using vscode or other editors or ides that do not support the nuget package manager, please install it using the dotnet command. You can see the installation command on the nuget package website.
using Win32Menu; This project has only one namespace, "Win32Menu".
If you can download this project, there are examples of two types of projects: "Win32MenuWinFormsDemo" and "Win32MenuWpfDemo".
The following is the specific usage process:
// Please declare a field of type NativeMenu in your window class
NativeMenu menu;
// Instantiate the menu at the appropriate time.
menu = new NativeMenu()
{
Uid = 888,
};Every object in NativeMenu should have a unique Uid.
There are some configurable attributes on the menu, such as "Text", "Type", "Status", "Checked", "IsRadioCheck", and "RightJustify".
var file = new NativeMenu()
{
Uid = 1,
Text = "File"
};
var exit = new NativeMenu()
{
Uid = 2,
Text = "Exit"
}
exit.Click += (NativeMenu _, ref bool _) =>
{
Close();
}; // Click events of the menu can be registered
file.AppendMenu(exit);
menu.AppendMenu(file,true); // The second parameter is set to true because this menu contains submenusWhat is the use of clicking on the second parameter of the event?
It indicates whether synchronization is enabled. Synchronization refers to synchronizing the state of the menu item object to the state of the underlying hosted menu. If you change the status of a menu item and find no change, it's because you didn't set it to true.
If there is no status change involved, please do not modify it, especially when the window is closed. The menu behind it will also be destroyed, and at this time, synchronization will cause an exception!
If you want to use the Click event, you must take this step.
protected override void WndProc(ref Message m)
{
// If you use menu.SetupForSystemMenu(Handle); So, please change conditions to (int)WndProcMsgType.WmSystemCommand
if (m.Msg is (int)WndProcMsgType.WmCommand)
{
if (menu is not null)
{
menu.ProcessWndProcParams(m.WParam)
}
}
base.WndProc(ref m);
}// Call the following method at the appropriate time
var hs = PresentationSource.FromVisual(this) as HwndSource;
if (hs is null) throw new NullReferenceException(nameof(hs));
hs.AddHook(WndProc);
// Customize WndProc
private nint WndProc(nint hWnd, int msg, nint wParam, nint lParam, ref bool handled)
{
// If you use menu.SetupForSystemMenu(Handle); So, please change conditions to (int)WndProcMsgType.WmSystemCommand
if (msg is (int)WndProcMsgType.WmCommand)
{
menu.ProcessWndProcParams(wParam);
}
return 0;
}menu.SetMenu(Handle);
SetMenu should only be used on NativeMenu objects that serve as window menu bars.
menu.SetupForSystemMenu(Handle);
Use menu.SetupForSystemMenu(Handle); With menu.SetMenu(Handle); They conflict with each other. Please do not try to mix them up, or strange problems will arise.
NativeMenu.SetNullMenu(Handle);
After providing the handle, the menu will be removed.
Please note that it is not destroyed. You can reuse it in the original way you set the menu.
menu.Dispose()
Usually, there is no need to destroy it, unless you prefer to manage it independently.