如何拖动&;使用PowerPoint(2010或2013)加载项删除多个形状
本文关键字:加载项 2013 删除 2010 使用 拖动 何拖动 amp PowerPoint | 更新日期: 2023-09-27 17:59:46
我想实现一个带有自定义任务窗格的PowerPoint加载项,用户可以从中将复杂对象对象拖动到幻灯片上(这些对象由多个形状组成,例如圆形中的两个三角形或类似的形状)。
通过在用户控件中调用DoDragDrop
并将所需的文本作为方法的第一个参数传递,我成功地将简单的文本框放到了幻灯片上。然而,我不知道是否可以传递一个更像多个形状的copmlex对象作为DoDragDrop
方法的数据参数。
我尝试的另一种方法是用空字符串调用DoDragDrop
(这样就不会有任何东西掉到幻灯片上),在DoDragDrop
返回后,我可以使用Globals.ThisAddIn.Application.ActiveWindow.View.Slide.AddShape
方法向幻灯片添加形状,但我找不到获取鼠标指针当前位置的方法。
那么,是否可以调用DoDragDrop并将多个形状传递给它,或者在DoDragDrop返回后获取光标位置?
更新:我为PowerPoint 2013找到了一个解决方案,它可以通过新的AfterDragDropOnSlide
事件来完成(但这并不微不足道,详细信息请点击此处:http://social.msdn.microsoft.com/Forums/en-US/724f1737-afa5-4762-8740-5b3745e06f8a/afterdragdroponslide-event-in-ppt-2013?forum=isvvba)。
所以问题是,在PowerPoint 2010中,同样的事情可能发生吗?
使用String.Empty
第一个参数调用DoDragDrop
后,可以获得当前光标坐标
获取坐标需要一些Win32样板:
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
class Win32API
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT lpPoint);
}
private POINT GetCursorPosition()
{
POINT point = new POINT();
Win32API.GetCursorPos(out point);
Win32API.ScreenToClient(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.HWND), ref point);
return point;
}
然后,必须将这些坐标转换为幻灯片的坐标系(幻灯片以点表示的大小与以>像素显示在屏幕上的大小不同)。
private POINT ConvertScreenPointToSlideCoordinates(POINT point)
{
// Get the screen coordinates of the upper-left hand corner of the slide.
POINT refPointUpperLeft = new POINT(
Globals.ThisAddIn.Application.ActiveWindow.PointsToScreenPixelsX(0),
Globals.ThisAddIn.Application.ActiveWindow.PointsToScreenPixelsY(0));
// Get the size of the slide (in points of the slide's coordinate system).
var slide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide;
var slideWidth = slide.CustomLayout.Width;
var slideHeight = slide.CustomLayout.Height;
// Get the screen coordinates of the bottom-right hand corner of the slide.
POINT refPointBottomRight = new POINT(
Globals.ThisAddIn.Application.ActiveWindow.PointsToScreenPixelsX(slideWidth),
Globals.ThisAddIn.Application.ActiveWindow.PointsToScreenPixelsY(slideHeight));
// Both reference points have to be converted to the PowerPoint window's coordinate system.
Win32API.ScreenToClient(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.HWND), ref refPointUpperLeft);
Win32API.ScreenToClient(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.HWND), ref refPointBottomRight);
// Convert the point to the slide's coordinate system (convert the pixel coordinate inside the slide into a 0..1 interval, then scale it up by the slide's point size).
return new POINT(
(int)Math.Round((double)(point.X - refPointUpperLeft.X) / (refPointBottomRight.X - refPointUpperLeft.X) * slideWidth),
(int)Math.Round((double)(point.Y - refPointUpperLeft.Y) / (refPointBottomRight.Y - refPointUpperLeft.Y) * slideHeight));
}
执行拖动的代码&下降:
DoDragDrop(String.Empty, DragDropEffects.Copy);
var point = GetCursorPosition();
var convertedPoint = this.ConvertScreenPointToSlideCoordinates(point);
// Insert something at the cursor's location:
var slide = Globals.ThisAddIn.Application.ActiveWindow.View.Slide;
slide.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeCloud, convertedPoint.X, convertedPoint.Y, 100, 100);