当前位置:   article > 正文

VisualStudio如何进行OneNote插件开发?

onenote插件开发

0.引言

  在OneNote做笔记很方便,但笔者用久后,就觉得OneNote缺少自己想要的一些功能,希望通过二次开发实现不断增长的需求。起初对OneNote二次开发是利用Onetastic通过宏实现较为简单的二次开发,但用久后,还是不满足日益增长的需求。偶然在网上找到一篇关于利用VisualStudio进行OneNote二次开发的文章[2],本文以笔者的电脑系统环境重现了该文章的方法,并补充了该文章开发过程中不详细的内容,得到了想要的二次开发的结果。

1.工具和数据准备

  (1)工具准备;
  本文使用VisualStudio2015(VS2015)进行OneNote2016的插件开发,VS2015没有自带的安装和部署工具,需要自行安装部署软件:Microsoft Visual Studio 2015 Installer Projects(下载地址: https://marketplace.visualstudio.com/items?itemName=visualstudioclient.MicrosoftVisualStudio2015InstallerProjects)。
  (2)数据准备。
  本文涉及一张用于插件显示的图片(HelloOneNote.png),如下所示:
  在这里插入图片描述

2.创建工程

  (1)创建类库;
  在这里插入图片描述
  (2)勾选使程序集COM可见;
  在这里插入图片描述
  (3)勾选为COM互操作注册。
  在这里插入图片描述

3.创建ribbon配置文件

  (1)添加ribbon.xml文件;
  在这里插入图片描述
  (2)将配置文件存入工程资源;
  在这里插入图片描述
  (3) 打开ribbon.xml文件,编写配置文件代码。
  在这里插入图片描述

<?xml version="1.0" encoding="utf-8" ?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" loadImage="GetImage">
  <ribbon>
    <tabs>
      <tab id="tabCustom" label="Custom">
        <group id="groupHello" label="Hello">
          <button id="buttonHello" label="HelloOneNote" size="large" screentip="Press this for a 'Hello World!' message" onAction="showHello" image="HelloOneNote.png" />
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4.编写功能代码

  (1)为工程添加引用:Extensibility、office、Microsoft OneNote 15.0 Object Library、System.Windows.Forms和System.Drawing;
  在这里插入图片描述
  详细添加步骤:
  在这里插入图片描述
  ①添加Extensibility;
  在这里插入图片描述
  ②添加office;
  在这里插入图片描述
  ③添加Microsoft OneNote 15.0 Object Library;
  在这里插入图片描述
  ④添加System.Windows.Forms;
  在这里插入图片描述
  ⑤添加System.Drawing。
  在这里插入图片描述
  (2)在Class1中添加using;

using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using System.Xml.Linq;
using Extensibility;
using Microsoft.Office.Core;
using OneNote = Microsoft.Office.Interop.OneNote;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

  (3)创建GUID;
  在这里插入图片描述
  本文GUID:{AD9144B5-6B8F-4C5D-8B5C-3A5E5C1C0FA1}
  (4)在Class1上添加标记,并继承接口;
  在这里插入图片描述

[Guid("AD9144B5-6B8F-4C5D-8B5C-3A5E5C1C0FA1"), ProgId("HelloOneNote.Class1")]
public class Class1 : IDTExtensibility2, IRibbonExtensibility
{

}
  • 1
  • 2
  • 3
  • 4
  • 5

  (5)在Class1中添加以下方法(实现接口IDTExtensibility2、实现接口IRibbonExtensibility);
  在这里插入图片描述

private OneNote.Application onApp = new OneNote.Application();
private object application;
public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
    application = Application;
}
public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
{
    application = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
}
public void OnAddInsUpdate(ref Array custom) { }
public void OnStartupComplete(ref Array custom) { }
public void OnBeginShutdown(ref Array custom)
{
    if (application != null)
    {
        application = null;
    }
}
public string GetCustomUI(string RibbonID)
{
    return Properties.Resources.ribbon;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

  注:以上属性和方法内容一般不用更改。
  (6)在Class1中添加以下方法(实现ribbon.xml中按钮的onAction事件调用的函数);
  在这里插入图片描述

public void showHello(IRibbonControl control)
{
    var app = application as OneNote.Application;
    var win = app.Windows;
    string id = (application as OneNote.Application).Windows.CurrentWindow.CurrentPageId;
    string title;
    app.GetPageContent(id, out title);
    var doc = XDocument.Parse(title);
    string pageTitle = doc.Descendants().FirstOrDefault().Attribute("ID").NextAttribute.Value;
    MessageBox.Show("Current Page ID = " + pageTitle, "Hello OneNote,I am cacrle.");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

  (7)添加按钮图片。
  ①将HelloOneNote.png图片拖进资源中;
  在这里插入图片描述
  ②在Class1中添加以下方法(实现ribbon.xml中加载图片的函数)。
  在这里插入图片描述

public IStream GetImage(string imageName)
{
    MemoryStream mem = new MemoryStream();
    Properties.Resources.HelloOneNote.Save(mem, ImageFormat.Png);
    return new CCOMStreamWrapper(mem);
}

class CCOMStreamWrapper : IStream
{
    public CCOMStreamWrapper(System.IO.Stream streamWrap)
    {
        m_stream = streamWrap;
    }

    public void Clone(out IStream ppstm)
    {
        ppstm = new CCOMStreamWrapper(m_stream);
    }

    public void Commit(int grfCommitFlags)
    {
        m_stream.Flush();
    }

    public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
    {
    }

    public void LockRegion(long libOffset, long cb, int dwLockType)
    {
        throw new System.NotImplementedException();
    }

    public void Read(byte[] pv, int cb, IntPtr pcbRead)
    {
        Marshal.WriteInt64(pcbRead, m_stream.Read(pv, 0, cb));
    }

    public void Revert()
    {
        throw new System.NotImplementedException();
    }

    public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
    {
        long posMoveTo = 0;
        Marshal.WriteInt64(plibNewPosition, m_stream.Position);
        switch (dwOrigin)
        {
            case 0:
                {
                    /* STREAM_SEEK_SET */
                    posMoveTo = dlibMove;
                }
                break;
            case 1:
                {
                    /* STREAM_SEEK_CUR */
                    posMoveTo = m_stream.Position + dlibMove;

                }
                break;
            case 2:
                {
                    /* STREAM_SEEK_END */
                    posMoveTo = m_stream.Length + dlibMove;
                }
                break;
            default:
                return;
        }
        if (posMoveTo >= 0 && posMoveTo < m_stream.Length)
        {
            m_stream.Position = posMoveTo;
            Marshal.WriteInt64(plibNewPosition, m_stream.Position);
        }
    }

    public void SetSize(long libNewSize)
    {
        m_stream.SetLength(libNewSize);
    }

    public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag)
    {
        pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG();
        pstatstg.cbSize = m_stream.Length;
        if ((grfStatFlag & 0x0001/* STATFLAG_NONAME */) != 0)
            return;
        pstatstg.pwcsName = m_stream.ToString();
    }

    public void UnlockRegion(long libOffset, long cb, int dwLockType)
    {
        throw new System.NotImplementedException();
    }

    public void Write(byte[] pv, int cb, IntPtr pcbWritten)
    {
        Marshal.WriteInt64(pcbWritten, 0);
        m_stream.Write(pv, 0, cb);
        Marshal.WriteInt64(pcbWritten, cb);
    }

    private System.IO.Stream m_stream;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106

5.安装和部署

  (1)在解决方案中添加安装和部署的项目;
  在这里插入图片描述
  (2)修改注册表;
  在这里插入图片描述
  ①HKEY_CLASSES_ROOT→AppID→{工程的GUID}:新建字符串值,Name:DllSurogate,Value为空;
  在这里插入图片描述
  ②HKEY_CLASSES_ROOT→CLSID→{工程的GUID}:新建字符串值,Name:AppID,Value:{工程的GUID};
  在这里插入图片描述
  ③HKEY_CURRENT_USER→Software→Microsoft→Office→OneNote→AddIns→工程的ProgId;
  工程的ProgId,一般为“命名空间.类名"(本为HelloOneNote.Class1)
  添加3个键:
  字符串值:Name:Description,Value:自定义的工程描述
  字符串值:Name:FriendlyName,Value:自定义的工程名称(本文为ShowFirstOneNotePlugin)
  DWORD值:Name:LoadBehavior,Value:3
  在这里插入图片描述
  ④HKEY_LOCAL_MACHINE→Software→Classes→AppID→{工程的GUID}:新建字符串值,Name:DllSurrogate,Value为空;
  在这里插入图片描述
  ⑤ HKEY_LOCAL_MACHINE→Software→Classes→CLSID→{工程的GUID}:新建字符串值,Name:AppID,Value:{工程的GUID}。
  在这里插入图片描述
  (3)设置项目输出;
  在这里插入图片描述
  (4)生成解决方案;
  在这里插入图片描述
  (5)安装。
  ①通过VS安装;
  在这里插入图片描述
  ②通过安装文件安装(可共享该文件)。
  在这里插入图片描述
  注:安装过程一般保持默认参数,一直下一步即可。

6.OneNote插件展示

  在这里插入图片描述

参考资料:
[1] cacrle. VisualStudio如何进行桌面软件开发?; 2023-03-18 [accessed 2023-04-06].
[2] johnhuang. 创建属于自己的OneNote插件; 2017-01-20 [accessed 2023-04-06].
[3] 微软官网. How to develop an OneNote 2010 ribbon add-in application; [accessed 2023-04-06].
[4] 微软官网. Deploying a Visual Studio 2010 Tools for Office Solution Using; [accessed 2023-04-06].

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/215035
推荐阅读
相关标签
  

闽ICP备14008679号