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>
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;
(3)创建GUID;
本文GUID:{AD9144B5-6B8F-4C5D-8B5C-3A5E5C1C0FA1}
(4)在Class1上添加标记,并继承接口;
[Guid("AD9144B5-6B8F-4C5D-8B5C-3A5E5C1C0FA1"), ProgId("HelloOneNote.Class1")]
public class Class1 : IDTExtensibility2, IRibbonExtensibility
{
}
(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;
}
注:以上属性和方法内容一般不用更改。
(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.");
}
(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;
}
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插件展示
文章来源:https://www.toymoban.com/news/detail-479487.html
参考资料:
[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].文章来源地址https://www.toymoban.com/news/detail-479487.html
到了这里,关于VisualStudio如何进行OneNote插件开发?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!