Community
    • Login

    NppCSharpPluginPack: how to add toolbar buttons?

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    2 Posts 2 Posters 80 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Z
      ZedZipDev
      last edited by

      I am developing plugin with 2 commands
      Settings
      Format

      The 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 mode

       var 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);
      

      }

      1 Reply Last reply Reply Quote 0
      • Mark OlsonM
        Mark Olson
        last edited by

        @ZedZipDev

        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 SetToolbarImage in the CommandMenuInit method, which I don’t think you should do. In the current version of the plugin pack, you may notice that in the beNotified method of UnmanagedExports.cs, the SetToolbarIcons method 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.

        1 Reply Last reply Reply Quote 1
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors