建立插件開發基本環境
1. 下載下傳Notepad++插件的最新發行版本;
2. 解壓檔案後,打開使用Visual Studio軟體打開NppPluginTemplate.vcproj檔案;
3. 在PluginDefinition.h檔案中定義插件名稱;
4. 在PluginDefinition.h檔案中定義插件指令數;
5. 在PluginDefinition.cpp中客制化插件指令名稱和相關的函數名;
6. 定義相關函數
如何實作代碼
開發者可根據
PluginDefinition.h
and
PluginDefinition.cpp
的注釋填充代碼:
//-- STEP 1. DEFINE YOUR PLUGIN NAME --//
定義插件名稱
//-- STEP 2. DEFINE YOUR PLUGIN COMMAND NUMBER --//
定義插件指令數
//-- STEP 3. CUSTOMIZE YOUR PLUGIN COMMANDS --//
客制化插件指令
//-- STEP 4. DEFINE YOUR ASSOCIATED FUNCTIONS --//
定義相關函數
開發複雜插件指令需要了解Notepad++插件系統的知識,如果遇到技術方面的問題,請登入插件開發論壇。
執行個體說明
下面的文字援引自官方文檔:
A good sample illustrates better the whole picture than a detailed documentation.
PluginDefinition.h
檔案說明
PluginDefinition.h
說明
在該檔案中需要改動三處:
- 定義插件名稱
const TCHAR NPP_PLUGIN_NAME[] = TEXT("Dummy"); //此處定義插件名稱,具體請參考下面的圖檔說明
- 定義插件指令數量
const int nbFunc = 3; //此處用于說明該插件共有三個指令
- 插件指令聲明
//
// Your plugin command functions
//
void hello();
void helloDlg();
void dummy(); //在官方模闆的基礎上添加自定義指令dummy
源代碼
#ifndef PLUGINDEFINITION_H
#define PLUGINDEFINITION_H
//
// All difinitions of plugin interface
//
#include "PluginInterface.h"
//-------------------------------------//
//-- STEP 1. DEFINE YOUR PLUGIN NAME --//
//-------------------------------------//
// Here define your plugin name
//
const TCHAR NPP_PLUGIN_NAME[] = TEXT("Dummy"); //此處定義插件名稱,具體請參考下面的圖檔說明
//-----------------------------------------------//
//-- STEP 2. DEFINE YOUR PLUGIN COMMAND NUMBER --//
//-----------------------------------------------//
//
// Here define the number of your plugin commands
//
const int nbFunc = 3; //此處用于說明該插件共有三個指令
//
// Initialization of your plugin data
// It will be called while plugin loading
//
void pluginInit(HANDLE hModule);
//
// Cleaning of your plugin
// It will be called while plugin unloading
//
void pluginCleanUp();
//
//Initialization of your plugin commands
//
void commandMenuInit();
//
//Clean up your plugin commands allocation (if any)
//
void commandMenuCleanUp();
//
// Function which sets your command
//
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk = NULL, bool check0nInit = false);
//
// Your plugin command functions
//
void hello();
void helloDlg();
void dummy(); //在官方模闆的基礎上添加自定義指令dummy
#endif //PLUGINDEFINITION_H
PluginDefinition.cpp
檔案說明
PluginDefinition.cpp
說明
在該檔案中需要改動兩處:
- 将插件指令與插件菜單綁定
setCommand(0, TEXT("Hello Notepad++"), hello, NULL, false);
setCommand(1, TEXT("Hello (with dialog)"), helloDlg, NULL, false);
setCommand(2, TEXT("Hello Dummy"), dummy, NULL, false); //該指令的作用類似于将插件指令與插件菜單進行綁定
從上述代碼可以看出,自定義插件指令
dummy
與插件
dummy
的第三個菜單項(Hello Dummy)進行綁定(即使用者單擊插件菜單時,執行對應的插件指令)
- 實作
中聲明的插件指令PluginDefinition.h
//實作自定義插件指令
void dummy()
{
::MessageBox(NULL, TEXT("Hello, Dummy!"), TEXT("Notepad++ Plugin Template"), MB_OK);
}
源代碼
#include "PluginDefinition.h"
#include "menuCmdID.h"
//
// The plugin data that Notepad++ needs
//
FuncItem funcItem[nbFunc];
//
// The data of Notepad++ that you can use in your plugin commands
//
NppData nppData;
//
// Initialize your plugin data here
// It will be called while plugin loading
void pluginInit(HANDLE /*hModule*/)
{
}
//
// Here you can do the clean up, save the parameters (if any) for the next session
//
void pluginCleanUp()
{
}
//
// Initialization of your plugin commands
// You should fill your plugins commands here
void commandMenuInit()
{
//--------------------------------------------//
//-- STEP 3. CUSTOMIZE YOUR PLUGIN COMMANDS --//
//--------------------------------------------//
// with function :
// setCommand(int index, // zero based number to indicate the order of command
// TCHAR *commandName, // the command name that you want to see in plugin menu
// PFUNCPLUGINCMD functionPointer, // the symbol of function (function pointer) associated with this command. The body should be defined below. See Step 4.
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
// bool check0nInit // optional. Make this menu item be checked visually
// );
setCommand(0, TEXT("Hello Notepad++"), hello, NULL, false);
setCommand(1, TEXT("Hello (with dialog)"), helloDlg, NULL, false);
setCommand(2, TEXT("Hello Sugon"), sugon, NULL, false); //添加自定義指令
}
//
// Here you can do the clean up (especially for the shortcut)
//
void commandMenuCleanUp()
{
// Don't forget to deallocate your shortcut here
}
//
// This function help you to initialize your plugin commands
//
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
if (index >= nbFunc)
return false;
if (!pFunc)
return false;
lstrcpy(funcItem[index]._itemName, cmdName);
funcItem[index]._pFunc = pFunc;
funcItem[index]._init2Check = check0nInit;
funcItem[index]._pShKey = sk;
return true;
}
//----------------------------------------------//
//-- STEP 4. DEFINE YOUR ASSOCIATED FUNCTIONS --//
//----------------------------------------------//
void hello()
{
// Open a new document
::SendMessage(nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_FILE_NEW);
// Get the current scintilla
int which = -1;
::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
if (which == -1)
return;
HWND curScintilla = (which == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
// Say hello now :
// Scintilla control has no Unicode mode, so we use (char *) here
::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)"Hello, Notepad++!");
}
void helloDlg()
{
::MessageBox(NULL, TEXT("Hello, Notepad++!"), TEXT("Notepad++ Plugin Template"), MB_OK);
}
//實作自定義插件指令
void sugon()
{
::MessageBox(NULL, TEXT("Hello, Sugon!"), TEXT("Notepad++ Plugin Template"), MB_OK);
}
驗證
代碼編譯
需要根據軟體版本和主機選擇生成32位和64位。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL31ERPd3ZE9UNFRUZrB3MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL2EjM3UzNxMTM0ATOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
在點選編譯後,如下圖生成輸出日志,提示生成成功,同時有紅框的提示(經驗證,紅框報的問題不影響功能,忽略)
添加自定義插件到Notepad++目錄下
- 在
的安裝目錄(Notepad+++
)下建立自定義插件的檔案夾C:\Program Files\Notepad++\plugins
;Dummy
Notepad++插件開發指南(翻譯)建立插件開發基本環境如何實作代碼執行個體說明驗證結語 - 将生成的
的名稱改為插件名稱NppPluginTemplate.dll
,拷貝檔案到1中建立的dummy.dll
檔案夾下面。(非必要步驟,名稱可不改,但是該名稱必須與第1步中的檔案夾名稱相同;如果不相同,插件無法正常加載);dummy
Notepad++插件開發指南(翻譯)建立插件開發基本環境如何實作代碼執行個體說明驗證結語 - 打開
軟體,驗證插件功能Notepad++
從上圖可以看出:插件已經正确加載,且自定義插件指令正如代碼的要求位于從上至下第三個位置。Notepad++插件開發指南(翻譯)建立插件開發基本環境如何實作代碼執行個體說明驗證結語 - 執行自定義插件指令
Notepad++插件開發指南(翻譯)建立插件開發基本環境如何實作代碼執行個體說明驗證結語