NppCSharpPluginPack: how to add toolbar buttons?
-
I am developing plugin with 2 commands
Settings
FormatThe plugin is added to the Notepad++ and I see it in the
plugins->
PMF ->
Settings
Format
And click on these menus call the corresponding function. It is ok.
Now I want to add 2 toolbar buttons.
They were added with icons. But no tooltips and click has no reaction. What I do wrong?
Here is my 2 functions
public static void CommandMenuInit()
{
IsCmdMenuInitCalled=true;
int index = 0;
PMFPanelId = index++;
PMFSettingsId = PMFSettingsId;
PluginBase.SetCommand(PMFPanelId = index++, “PMF Format”, ExecuteFormatting);
PluginBase.SetCommand(PMFPanelId = index++, “PMF Settings”, ShowSettingsWindow);SetToolbarImage(XPoorTSQLFormatter.NPP.Resources.Resource.pmf_format, 1); SetToolbarImage(XPoorTSQLFormatter.NPP.Resources.Resource.pmf_settings, 2);}
static public void SetToolbarImage(Bitmap image, int pluginId)
{
var tbIcons = new toolbarIcons();
tbIcons.hToolbarBmp = image.GetHbitmap();
tbIcons.hToolbarIcon = tbIcons.hToolbarBmp; // same icon for light mode
tbIcons.hToolbarIconDarkMode = tbIcons.hToolbarBmp; // same icon for dark modevar iz = Marshal.SizeOf(tbIcons); var iz2 = Marshal.SizeOf(tbIcons.hToolbarIcon); // MessageBox.Show($"SetToolbarImage: {pluginId}"); //MessageBox.Show($"{pluginId}: Size of toolbarIcons struct: {iz2} bytes"); IntPtr pTbIcons = Marshal.AllocHGlobal(iz); Marshal.StructureToPtr(tbIcons, pTbIcons, false); Win32.SendMessage( PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, (IntPtr)PluginBase._funcItems.Items[pluginId-1]._cmdID, pTbIcons); Marshal.FreeHGlobal(pTbIcons);}
-
Core maintainer of NppCSharpPluginPack here.
It’s pretty hard for me to diagnose your problem if you don’t share a link to your full codebase with me. However, it looks to me like you’re calling
SetToolbarImagein theCommandMenuInitmethod, which I don’t think you should do. In the current version of the plugin pack, you may notice that in the beNotified method ofUnmanagedExports.cs, theSetToolbarIconsmethod is called, which initializes all the toolbar icons for the plugin.Truth be told, I have never studied the relevant sections of the Notepad++ codebase well enough to fully understand what’s going on with toolbar icons. I’ve just mostly gone with what worked in the old NotepadPlusPlusPluginPack.Net in this instance.