MFC编写notepade++ plugin 增加停靠窗口的要点

来源:互联网 发布:淘宝店铺模板怎么用 编辑:程序博客网 时间:2024/05/17 23:47

#

MFC编写notepade++ plugin 增加停靠窗口的要点

flyfish

将示例的7个文件拷贝到项目中

Docking.hDockingDlgInterface.hdockingResource.hGoToLineDlg.cppGoToLineDlg.hStaticDialog.cppStaticDialog.h

添加两个窗口 ID
#define IDD_PLUGINGOLINE_DEMO 101
#define IDD_DIALOG_MAIN 3000
对话框类
头文件 DlgMain.h

class CDlgMain : public CDialogEx{    DECLARE_DYNAMIC(CDlgMain)public:    CDlgMain(CWnd* pParent = NULL);   // 标准构造函数    virtual ~CDlgMain();// 对话框数据    enum { IDD = IDD_DIALOG_MAIN };protected:    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持    DECLARE_MESSAGE_MAP()    afx_msg void OnBnClickedButtonInsert();    virtual BOOL OnInitDialog();    void SetSize(int x, int y);};

实现文件 DlgMain.cpp

#include "stdafx.h"#include "RichDadRobot.h"#include "DlgMain.h"#include "afxdialogex.h"#include "PluginDefinition.h"extern NppData nppData;// CDlgMain 对话框IMPLEMENT_DYNAMIC(CDlgMain, CDialogEx)CDlgMain::CDlgMain(CWnd* pParent /*=NULL*/)    : CDialogEx(CDlgMain::IDD, pParent){}CDlgMain::~CDlgMain(){}void CDlgMain::DoDataExchange(CDataExchange* pDX){    CDialogEx::DoDataExchange(pDX);    DDX_Text(pDX, IDC_EDIT_X, m_Edit_strX);}BEGIN_MESSAGE_MAP(CDlgMain, CDialogEx)    ON_BN_CLICKED(IDC_BUTTON_INSERT, &CDlgMain::OnBnClickedButtonInsert)END_MESSAGE_MAP()// CDlgMain 消息处理程序void CDlgMain::OnBnClickedButtonInsert(){    // TODO:  在此添加控件通知处理程序代码    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)"ABC");}BOOL CDlgMain::OnInitDialog(){    CDialogEx::OnInitDialog();    // TODO:  在此添加额外的初始化    SetBackgroundColor(RGB(0, 0, 255));    return TRUE;  // return TRUE unless you set the focus to a control    // 异常:  OCX 属性页应返回 FALSE}void CDlgMain::SetSize(int x, int y){    MoveWindow(0, 0, x, y);}

在GoToLineDlg.h文件中 创建对话框

#ifndef GOTILINE_DLG_H#define GOTILINE_DLG_H#include "DockingDlgInterface.h"#include "resource.h"#include "DlgMain.h"class DemoDlg : public DockingDlgInterface{public :    DemoDlg() : DockingDlgInterface(IDD_PLUGINGOLINE_DEMO){};    virtual void display(bool toShow = true) const {        DockingDlgInterface::display(toShow);    };    void setParent(HWND parent2set){        _hParent = parent2set;    };    void create(tTbData * data, bool isRTL = false)    {        DockingDlgInterface::create(data, isRTL);        dlg = new CDlgMain;        dlg->Create(IDD_DIALOG_MAIN, CWnd::FromHandle(_hSelf));    };    CDlgMain* dlg;protected :    virtual BOOL CALLBACK run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam);};#endif //GOTILINE_DLG_H

GoToLineDlg.cpp中处理WM_SIZE消息

#include "stdafx.h"#include "GoToLineDlg.h"#include "PluginDefinition.h"extern NppData nppData;BOOL CALLBACK DemoDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam){    switch (message)     {        case WM_COMMAND :         {            switch (wParam)            {                case IDOK :                {                    return TRUE;                }            }                return FALSE;        }        case WM_SIZE:        {            if (dlg)            {                       int x = ((int)(short)LOWORD(lParam));                int y = ((int)(short)HIWORD(lParam));                dlg->SetSize(x, y);                dlg->ShowWindow(SW_SHOW);            }            return TRUE;        }        break;        default :            return DockingDlgInterface::run_dlgProc(message, wParam, lParam);    }}

PluginDefinition.h 中增加

void ShowDlg();

PluginDefinition.cpp 文件

#include "stdafx.h"#include "PluginDefinition.h"#include "menuCmdID.h"#include "GoToLineDlg.h"//// The plugin data that Notepad++ needs//FuncItem funcItem[nbFunc];//// The data of Notepad++ that you can use in your plugin commands//NppData nppData;DemoDlg g_dlg;//// Initialize your plugin data here// It will be called while plugin loading   void pluginInit(HANDLE hModule){    g_dlg.init((HINSTANCE)hModule, NULL);    commandMenuInit();}//// 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 herevoid 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"), hello, NULL, false);    setCommand(1, TEXT("RichDadRobot"), ShowDlg, 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 ShowDlg(){    g_dlg.setParent(nppData._nppHandle);    tTbData data = { 0 };    if (!g_dlg.isCreated())    {        g_dlg.create(&data);        data.uMask = DWS_DF_CONT_LEFT;        data.pszModuleName = g_dlg.getPluginFileName();        data.dlgID = IDD_PLUGINGOLINE_DEMO;        ::SendMessage(nppData._nppHandle, NPPM_DMMREGASDCKDLG, 0, (LPARAM)&data);    }    g_dlg.display();}
0 0
原创粉丝点击