Creating Your First Notepad++ Plugin Using Visual Studio 2019 And C#
Hello,
Call the SetFirstCharAllWordsCap function in the CommandMenuInit() through the PluginBase.SetCommand() method.
That's it!
In this blog post, I'll demonstrate on how to develop a Notepad++ Plugin (64 Bit) using Visual Studio 2019 and C# assuming that you have installed a 64 Bit version of the latest Notepad++ Editor. This tutorial is based from kblisted Notepad++ Plugin Package in GitHub. The plugin's architecture can communicate with the Notepad++ or the underlying Scintilla engine using NotepadPlusPlusGateway and ScintillaGateWay and Win32 API. To start with, download the Notepad++ Plugin Pack from the GitHub page and copy the zip file to the Project Templates folder of your Visual Studio 2019 IDE. In my laptop, the path is "C:\Users\my_username\Documents\Visual Studio 2019\Templates\ProjectTemplates\Visual C#".
Open your Visual Studio 2019 IDE and create a project using the Notepad++ Plugin template.
Change the Platform Target to x64. (Our OS is Windows 10 64 bit)
Create a function called SetFirstCharAllWordsCap inside Main.cs that get's the entire string content of a Notepad++ active window and convert each word's first letter to uppercase.
internal static void SetFirstCharAllWordsCap() { string modifiedString = string.Empty; IntPtr currentScint = PluginBase.GetCurrentScintilla(); ScintillaGateway scintillaGateway = new ScintillaGateway(currentScint); try { int length = scintillaGateway.GetLength(); string allText = scintillaGateway.GetText(length + 1); modifiedString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(allText); scintillaGateway.SelectAll(); scintillaGateway.ReplaceSel(modifiedString); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
internal static void CommandMenuInit() { StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH); Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath); iniFilePath = sbIniFilePath.ToString(); if (!Directory.Exists(iniFilePath)) Directory.CreateDirectory(iniFilePath); iniFilePath = Path.Combine(iniFilePath, PluginName + ".ini"); someSetting = (Win32.GetPrivateProfileInt("SomeSection", "SomeKey", 0, iniFilePath) != 0); PluginBase.SetCommand(0, "Capitalize First Letter", SetFirstCharAllWordsCap, new ShortcutKey(false, false, false, Keys.None)); }
Before compiling your project, make sure to close Notepad++ editor and that you have write permissions in "C:\Program Files\Notepad++\plugins\" folder it's because the project will create a subfolder using the plugin name and the dll will be published to that subfolder. If successful, open your Notepad++ editor and open a new window. Enter dummy text information with lowercase characters. To run the plugin, go to Plugins menu -> Select the name of your plugin -> click Capitalize First Letter.
Each word's first character are all converted to uppercase.
To debug the application if there are logic errors, open the Attach To Process dialog in Visual Studio and select notepad++.exe
That's it!
Comments
Post a Comment