如何在 Powerpoint 2013 插件(用 C# 开发)中获取“ctrl c”或鼠标复制事件?
Posted
技术标签:
【中文标题】如何在 Powerpoint 2013 插件(用 C# 开发)中获取“ctrl c”或鼠标复制事件?【英文标题】:How to get the "ctrl c" or mouse copy event in a Powerpoint 2013 Addin (developed in C#)? 【发布时间】:2017-01-11 16:24:39 【问题描述】:下面的代码不是在 MS Powerpoint 中捕获字符类型,它只是在 Powerpoint 之外,我如何才能在此代码中捕获“控制副本”或“鼠标右键单击副本”?
下面的代码不是在 MS Powerpoint 中捕获字符类型,它只是在 Powerpoint 之外,我如何才能在此代码中捕获“控制副本”或“鼠标右键单击副本”?
下面的代码不是在 MS Powerpoint 中捕获字符类型,它只是在 Powerpoint 之外,我如何才能在此代码中捕获“控制副本”或“鼠标右键单击副本”?
public partial class ThisAddIn
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static IntPtr hookId = IntPtr.Zero;
private delegate IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam);
private static HookProcedure procedure = HookCallback;
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
private void ThisAddIn_Startup(object sender, System.EventArgs e)
hookId = SetHook(procedure);
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
UnhookWindowsHookEx(hookId);
private static IntPtr SetHook(HookProcedure procedure)
using (Process process = Process.GetCurrentProcess())
using (ProcessModule module = process.MainModule)
return SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
int pointerCode = Marshal.ReadInt32(lParam);
string pressedKey = ((Keys)pointerCode).ToString();
//Do some sort of processing on key press
var thread = new Thread(() => if (pressedKey.ToLower() == "LControlKey") MessageBox.Show(pressedKey); );
thread.Start();
return CallNextHookEx(hookId, nCode, wParam, lParam);
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
return new Ribbon();
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
#endregion
【问题讨论】:
【参考方案1】:使用自定义功能区。
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
<commands>
<command idMso="Cut" onAction="CopyIntercept" />
<command idMso="Copy" onAction="CopyIntercept" />
</commands>
</customUI>
public void CopyIntercept(IRibbonControl control, ref bool CancelDefault)
您也许可以使用功能区设计器来完成此操作,但上面的示例适用于 XML 功能区。
【讨论】:
以上是关于如何在 Powerpoint 2013 插件(用 C# 开发)中获取“ctrl c”或鼠标复制事件?的主要内容,如果未能解决你的问题,请参考以下文章
VSTO之PowerPoint(PPT)插件开发常用API汇总