背景介绍
一个ppt的小插件,可以通过右侧自定义的taskpane,拖动显示资源进行插入。
实现步骤
- 资源可拖动设置
- 拖动目标区域设置
- 拖动释放动作及坐标
- 坐标转换
关键点
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坐标
暂留
注意点
- 控件picturebox没有allowdrop属性,但是在代码里面添加是可以的,而且能编译通过。看vs的解释好像是重写了control的allowdrop。
- DoDragDrop函数要配合allowdrop使用。
- 拖动控件本身的allowdrop属性也要打开,不然不能识别。