如何使用VSTO在Powerpoint COM Addin中注册CTRL + V操作 [英] How to Register to CTRL+V opereation in Powerpoint COM Addin using VSTO

查看:78
本文介绍了如何使用VSTO在Powerpoint COM Addin中注册CTRL + V操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中使用VSTO开发的Powerpoint插件中捕获粘贴事件。

I am trying to capture Paste Event in Powerpoint addin being developed using VSTO in C#.

这是否可以实现与使用Windows
SetClipboardViewer
ChangeClipboardChain API,以监控
WM_DRAWCLIPBOARD 消息何时触发。

Is this possible to achieve the same with the use of the Windows SetClipboardViewer and ChangeClipboardChain APIs in order to monitor when the WM_DRAWCLIPBOARD message triggers.

但我不知道怎么做?

有人可以帮我吗?

Point5Nyble

Point5Nyble

推荐答案

为了在剪贴板内容发生变化时系统通知,应用程序必须使用SetClipboardViewer函数(user32.dll)将其窗口添加到剪贴板查看器链中。每当剪贴板的内容发生变化时,剪贴板查看器窗口都会收到WM_DRAWCLIPBOARD消息
。当从链中移除窗口时,WM_CHANGECBCHAIN消息被发送到剪贴板查看器链中的第一个窗口。所以你可以参考下面的代码:

In order to be notified by the system when the clipboard content changes, an application must use the SetClipboardViewer function (user32.dll) to add its window into the chain of clipboard viewers. Clipboard viewer windows receive a WM_DRAWCLIPBOARD message whenever the content of the clipboard changes. And the WM_CHANGECBCHAIN message is sent to the first window in the clipboard viewer chain when a window is being removed from the chain. So you can refer to below code:

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
 
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

// WM_DRAWCLIPBOARD message
private const int WM_DRAWCLIPBOARD = 0x0308;
private IntPtr _clipboardViewerNext;
 
public delegate void ClipboardHandler(object sender, ClipboardArgs e);
public event ClipboardHandler OnNewClipboard;
 
public ClipboardManager()
{
    InitializeComponent();
}
 
public void Start()
{
    _clipboardViewerNext = SetClipboardViewer(this.Handle);
}
 
public void Stop()
{
    ChangeClipboardChain(this.Handle, _clipboardViewerNext);
}
 
protected override void WndProc(ref Message m)
{
   base.WndProc(ref m);    
   if (m.Msg == WM_DRAWCLIPBOARD)
   {
    IDataObject iData = Clipboard.GetDataObject();      
 
    if (iData.GetDataPresent(DataFormats.Bitmap))
    {
        Bitmap image = (Bitmap)iData.GetData(DataFormats.Bitmap);
        
        if (OnNewClipboard != null)
        {                        
            OnNewClipboard(this, new ClipboardArgs(image));
        }
    }
    }
}
 
public class ClipboardArgs
{
   public ClipboardArgs(Bitmap image)
   {
    this.Image = image;
   }
   
   public Bitmap Image { get; set; }
 
}


注意:在VSTO解决方案中使用Windows API,可能会使Excel无法进行。

Note:using Windows API in VSTO solution, it may make the Excel unstatble.


这篇关于如何使用VSTO在Powerpoint COM Addin中注册CTRL + V操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆