Word加载项拖放到文档上

本文关键字:文档 拖放 加载项 Word | 更新日期: 2023-09-27 18:28:03

我正在创建一个Word加载项,为了允许将某些内容从自定义任务窗格拖动到文档中,我遵循了以下指南:http://msdn.microsoft.com/en-us/library/office/hh780901(v=office.14).aspx

使用这种方法确实存在一些缺点。

首先,捕获drop事件的透明Windows窗体(在我的情况下是WPF)是Window的大小,而不是文档的大小,并且RangeFromPoint总是返回一个值,即使我们没有在文档上(例如,如果我们在Ribbon上)。因此,一旦你拖动了一些东西并创建了这个表单,无论你把它放在哪里,它都会被放在文档中。一旦你开始,没有什么优雅的方式可以取消。

我的问题是:

是否有人在Word加载项中使用拖放操作,并找到了比Microsoft提供的示例更好的方法来处理它

最好使用当前的解决方案,但要知道用户何时不在文档上拖动,或者让透明窗口只显示在文档区域上。

Word加载项拖放到文档上

希望你已经有了答案。

我有自己的解决方案。

因此,我的要求

我有一个自定义窗格,其中包含一个列表框,每个项目都是一个普通字符串。当我将项目从列表框拖动到文档的特定位置时,我希望在该位置插入一个合并字段。合并字段的名称是项的文本。

一开始很简单,然后我遇到了一个问题,就像你在问题中描述的那样。

关于代码

所以,有一个列表框,你需要处理mouseDown和mouseMove,不要担心mouseUp。

在mouseDown处理程序中,我记录边界,如果鼠标移出该边界,拖动就会开始。

然后,在listBox_MouseMoveHandler中,我检查鼠标的位置以启动拖放。对于DoDragDrop方法,我必须使用DragDropEffects.Copy

DoDragDrop((sender as ListControl).SelectedValue, DragDropEffects.Copy);

使用该选项,SelectedValue将被插入到放置位置,插入后,它也将被选中。

然后,我只检查所选内容是否为空,并用合并字段替换所选文本。当然,我在DoDragDrop之前折叠了选择。这就是全部技巧。

    private int _selectedItemIndex;
    private Rectangle dragBoxFromMouseDown;
    private void CustomizationForListBox(ListBox listBox)
    {
        listBox.ItemHeight = 25;
        listBox.DrawMode = DrawMode.OwnerDrawFixed;
        listBox.DrawItem += ListBox_DrawItem;
        listBox.MouseDoubleClick += listBox_MouseDoubleClick;
        listBox.MouseMove += listBox_MouseMoveHandler;
        listBox.MouseUp += listBox_MouseUp;
        listBox.MouseDown += (sender, e) =>
        {
            // Handle drag/drop
            if (e.Button == MouseButtons.Left)
            {
                _selectedItemIndex = listBox.IndexFromPoint(e.Location);
                // Remember the point where the mouse down occurred. The DragSize indicates
                // the size that the mouse can move before a drag event should be started.                
                Size dragSize = SystemInformation.DragSize;
                // Create a rectangle using the DragSize, with the mouse position being
                // at the center of the rectangle.
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
                                                               e.Y - (dragSize.Height / 2)), dragSize);
            }
        };
    }
    private void listBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            // Reset the drag rectangle when the mouse button is raised.
            dragBoxFromMouseDown = Rectangle.Empty;
        }
    }
    private void listBox_MouseMoveHandler(object sender, MouseEventArgs e)
    {
        // Handle drag and drop
        // To check if the Mouse left button is clicked
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
        {
            // If the mouse moves outside the rectangle, start the drag.
            if (dragBoxFromMouseDown != Rectangle.Empty &&
                !dragBoxFromMouseDown.Contains(e.X, e.Y))
            {
                // Collapse current selection, now we know nothing is selected
                Globals.ThisAddIn.Application.Selection.Collapse(WdCollapseDirection.wdCollapseEnd);
                //Start Drag Drop
                DoDragDrop((sender as ListControl).SelectedValue, DragDropEffects.Copy);
                if (_selectedItemIndex != -1)
                {
                    // If the drag/drop was successful, there dropped text must be selected
                    if (!String.IsNullOrWhiteSpace(Globals.ThisAddIn.Application.Selection.Text))
                    {

// Replace the selected text with a merge field MergeFieldHelper.InsertSingleMergeField(mergeFieldInfos[_selectedItemIndex].Name); } } } } }