65.9K
CodeProject 正在变化。 阅读更多。
Home

Word 插件 - 简单粘贴 PrintScreen

用于 PrintScreen 的 Word 插件

引言

本文的目的是创建一个用于 Word 的插件,以自动记录屏幕截图。

背景

该插件旨在方便创建手册、注册测试和其他需要大量使用屏幕截图的任务。 

创建代码 

首先创建一个 Word 2007 插件解决方案,如下所示: 

创建代码插件   

解决方案创建后,让我们创建一个按钮:  

//
// Any source code blocks look like this
//
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
   CheckIfMenuBarExists();
   AddMenuBar();
}
 
//
// Checks whether there is a button with id equal to 200
//
private void CheckIfMenuBarExists()
{
   try
   {
    Office.CommandBarButton foundMenu = (Office.CommandBarButton)
        this.Application.CommandBars.ActiveMenuBar.FindControl(
          Office.MsoControlType.msoControlButton, System.Type.Missing, 200, true, true);
 
    if (foundMenu != null)
    {
        foundMenu.Delete(true);
    }
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
}
 
//
// Add button with caption 'Monitor PrintScreen'
//
private void AddMenuBar()
{
   try
   {
    Office.CommandBar menubar = (Office.CommandBar)Application.CommandBars.ActiveMenuBar;
 
    // Add the menu.                
    menuCommand = (Office.CommandBarButton)menubar.Controls.Add(
           Office.MsoControlType.msoControlButton, missing, missing, missing, true);
    menuCommand.Style = MsoButtonStyle.msoButtonIconAndWrapCaptionBelow;
    menuCommand.Caption = "Monitor PrintScreen";
    menuCommand.Tag = "200";
    menuCommand.FaceId = 65;
    menuCommand.Click += new _CommandBarButtonEvents_ClickEventHandler(menuCommand_Click);                        
   }
   catch (Exception e)
   {
    MessageBox.Show(e.Message);
   }
}

剪贴板监视器

[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);
 
private const int WM_DRAWCLIPBOARD = 0x0308;// WM_DRAWCLIPBOARD message
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; }
 
}

将新图像包含在文档中

void manager_OnNewClipboard(object sender, ClipboardArgs e)
{
        Application.ActiveDocument.Content.Paragraphs.Add(
          Globals.ThisAddIn.Application.Selection.Range);
 
    Globals.ThisAddIn.Application.Selection.Paste();
    Application.ActiveDocument.Content.Paragraphs.Add(
      Globals.ThisAddIn.Application.Selection.Range);

    Globals.ThisAddIn.Application.Selection.MoveDown(WdUnits.wdParagraph, 2, Type.Missing);
}

此代码的结果

单击按钮后,所有屏幕截图都将包含在 Word 文档中。 

关注点

在本文中学习如何创建

  • 一个用于 Word 2007 的插件。
  • 一个监视区域传输。

历史

  • 2013年3月12日:文章发布。
© . All rights reserved.