ppt资源的拖动插入方法

背景介绍

一个ppt的小插件,可以通过右侧自定义的taskpane,拖动显示资源进行插入。

实现步骤

  1. 资源可拖动设置
  2. 拖动目标区域设置
  3. 拖动释放动作及坐标
  4. 坐标转换

关键点

1.确定目标区域

假设taskpane处于右侧,则目标区域就是ppt文档的可以编辑区域。由于ppt开发文档中没有现成的方法来获取,所以必须手动获取。
红框内为目标区域
整体思路就是创建一个透明的窗体,覆盖在目标区域上,里面的textbox控件接受拖动内容,从而模拟鼠标拖动,拖动释放的时候再隐藏就好了。
透明窗体的大小和位置设置通过窗口句柄来实现:

  public static void OverlayForm()
        {
            if (form == null)
            {
                form = new OverlayForm();
                form.AllowDrop = true;
                IntPtr parent = GetCurrentWindowHandle();
                System.Drawing.Rectangle rect = new System.Drawing.Rectangle();

                WinAPI.GetWindowRect(parent, ref rect);
                uint uFlags = WinAPI.SWP_NOACTIVATE | WinAPI.SWP_NOZORDER;

                WinAPI.SetWindowPos((uint)form.Handle.ToInt32(), parent.ToInt32(), 0, 0, rect.Right, rect.Bottom, uFlags);
                WinAPI.SetParent(form.Handle, parent);
                form.AllowTransparency = true;

                Globals.ThisAddIn.OverlayForm = form;
                form.Show();
            }
            else
            {
                if (form.Visible == false)
                {
                    form.Show();
                }

            }
        }

获取ppt目标区域句柄

   private static IntPtr GetCurrentWindowHandle()
        {
            IntPtr windowHandle = new IntPtr(0);

            if (mainBar.IsWPS())
            {
                windowHandle = WinAPI.FindWindow("PP11FrameClass", null);
                windowHandle = WinAPI.FindWindowEx(windowHandle, IntPtr.Zero, "MDIClient", null);
                windowHandle = WinAPI.FindWindowEx(windowHandle, IntPtr.Zero, "mdiClass", null);
                windowHandle = WinAPI.FindWindowEx(windowHandle, IntPtr.Zero, null, "Normal1");

            }
            else
            {
                windowHandle = WinAPI.FindWindow("PP12FrameClass", null);
                windowHandle = WinAPI.FindWindowEx(windowHandle, IntPtr.Zero, "MDIClient", null);
                windowHandle = WinAPI.FindWindowEx(windowHandle, IntPtr.Zero, "mdiClass", null);
                windowHandle = WinAPI.FindWindowEx(windowHandle, IntPtr.Zero, null, "Slide");


            }
            return windowHandle;
        }
2.屏幕坐标转ppt坐标

暂留

注意点

  1. 控件picturebox没有allowdrop属性,但是在代码里面添加是可以的,而且能编译通过。看vs的解释好像是重写了control的allowdrop。
    这里写图片描述
  2. DoDragDrop函数要配合allowdrop使用。
  3. 拖动控件本身的allowdrop属性也要打开,不然不能识别。

引用

msdn上对word的一个类似案例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值