Programming Notepad++ plugins part 2

In this part we will look at messages and notifications using the example of NPPM_ADDTOOLBARICON_FORDARKMODE. This can be used to create a button in the taskbar with its own icon. We use again the same starter template as in the first part. It can be downloaded under the following link:

In the PluginDefinition.h file we add the addToolbarIcon function.

void addToolbarIcon();

In the PluginDefinition.cpp file we include the resource.h file and define the _gModule variable.

#include "resource.h" HINSTANCE _gModule;

Then the pluginInit function is processed here the previously defined variable is initialized:

void pluginInit(HANDLE hModule) { _gModule = (HINSTANCE)hModule; }

After this the new function addToobarIcon is programmed:

void addToolbarIcon() { toolbarIconsWithDarkMode tbIcon{}; tbIcon.hToolbarBmp = LoadBitmap(_gModule, MAKEINTRESOURCE(IDB_BITMAP1)); tbIcon.hToolbarIcon = LoadIcon(_gModule, MAKEINTRESOURCE(IDI_ICON1)); tbIcon.hToolbarIconDarkMode = LoadIcon(_gModule, MAKEINTRESOURCE(IDI_ICON2)); SendMessage(nppData._nppHandle, NPPM_ADDTOOLBARICON_FORDARKMODE, funcItem[0]._cmdID, (LPARAM)&tbIcon); }

Here first the image files of the icon for the normal view as well as for the dark mode are loaded. Here the handle of the added variable is needed. Then the message is sent to Notepad++. The parameter funcItem[0]._cmdId means that the first command function of the plugin is activated when clicking on the button, i.e. the hello function. If you want to activate the second function with one click you change it to funcItem[1]._cmdId.

The last file to be edited is NppPuginDemo.cpp.

The function external "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode) is modified as follows:

extern "C" __declspec(dllexport) void beNotified(SCNotification *notifyCode) { switch (notifyCode->nmhdr.code) { case NPPN_TBMODIFICATION: { addToolbarIcon(); break; } case NPPN_SHUTDOWN: { commandMenuCleanUp(); } break; default: return; } }

Here at the notification NPPN_TBMODIFICATION the message to create the toolbar button with the previously created function is sent.

Now we have to import the image files. To do this, double-click on the NppPluginDem.rc file in the Solution Explorer in Visual Studio. Now right-click on NppPluginDem.rc and click on Add Resource. Import the three image files. One bitmap file and two ICO files. Afterwards it should look like this:

Screenshot 1

If you now compile the project and load the plugin in Notepad++ the new button appears in the taskbar.

Screenshot 2

You are welcome to upload your programmed plugins to our platform.