Kri*_*ams 6 c# wpf add-in drag-and-drop ms-word
我正在创建一个Word加载项,为了允许从自定义任务窗格拖动到文档,我遵循以下指南:http: //msdn.microsoft.com/en-us/library/office/hh780901 (v = office.14)的.aspx
使用这种方法存在一些真正的缺点.
首先,捕获drop事件的透明Windows窗体(或我的案例中的WPF)是Window的大小,而不是文档的大小,而RangeFromPoint总是返回一个值,即使我们没有覆盖文档(例如,如果我们在功能区上.因此,一旦你拖动某个东西并创建了这个表单,无论你放在哪里,它都会被放置在文档中.一旦开始,就没有优雅的方法可以取消.
我的问题是:
有没有人在Word Add in中使用Drag and Drop做任何工作,并且找到了比Microsoft提供的示例更好的方法来处理它?
使用当前解决方案会很好,但知道用户何时没有拖动文档或者只有文档区域显示透明窗口.
希望你已经有了答案。
我有自己的解决方案。
所以,我的要求:
我有一个自定义窗格,其中包含一个列表框,每个项目都是一个普通字符串。当我将项目从列表框拖到文档中的特定位置时,我想在该位置插入合并字段。合并字段的名称是项目的文本。
一开始很简单,然后我遇到了一个问题,就像你在问题中描述的那样。
关于代码
所以,有一个列表框,你需要处理mouseDown和mouseMove,不用担心mouseUp。
在 mouseDown 处理程序中,我记录边界,如果鼠标移出该边界,则拖动将开始。
然后,在 listBox_MouseMoveHandler 中,我检查鼠标的位置以启动拖放。我必须使用DragDropEffects.Copy该DoDragDrop方法。
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))
{
Run Code Online (Sandbox Code Playgroud)
// Replace the selected text with a merge field MergeFieldHelper.InsertSingleMergeField(mergeFieldInfos[_selectedItemIndex].Name);
}
}
}
}
}
| 归档时间: |
|
| 查看次数: |
1047 次 |
| 最近记录: |