weixin_45840746 2024-04-15 12:48 采纳率: 35.6%
浏览 9
已结题

C#:vsto powerpoint的外接程序

在powerpoint的外接程序中,我有一个wpf,我希望这个wpf嵌入到powerpoint中,也就是说,wpf国定在powerpoint的中下方,并且拖动powerpoint窗口,wpf也跟随着动,这个该如何实现哎,运行成功才会采纳,请不要答非所问

        private void ShowToolBar()
        {
            try
            {
                toolBar = new ToolBar(app);
                SetPowerPointWindowOwner();
                toolBar.Show();
                app.SlideShowBegin += App_SlideShowBegin;
                app.SlideShowEnd += App_SlideShowEnd;
                // 监听 PowerPoint 窗口的激活事件
                app.WindowActivate += Application_WindowActivate;
                app.PresentationClose += Application_PresentationClose;

            }
            catch (Exception)
            {
                MessageBox.Show("未打开工具栏");
            }
        }
        private void SetPowerPointWindowOwner()
        {
            IntPtr pptWindowHandle = new IntPtr(app.HWND);
            new WindowInteropHelper(toolBar).Owner = pptWindowHandle;
        }

  • 写回答

14条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-15 12:48
    关注

    引自免费微信小程序:皆我百晓生

    在您的C# VSTO PowerPoint外接程序中,要将一个WPF窗体(ToolBar)嵌入到PowerPoint窗口中并使其与PowerPoint窗口同步移动,您已正确地设置了WPF窗体的Owner属性。然而,为了确保WPF窗体始终保持在PowerPoint窗口的特定位置(例如中下方),并在PowerPoint窗口移动时自动跟随,您还需要进行一些额外的设置和处理。

    以下是在现有代码基础上添加所需功能的详细步骤及代码示例:

    1. 调整WPF窗体的位置: 在SetPowerPointWindowOwner()方法之后,添加一个方法来计算并设置ToolBar在PowerPoint窗口中的初始位置。确保它位于PowerPoint窗口的中下方。

      private void SetToolBarInitialPosition()
      {
          RECT pptWindowRect;
          GetWindowRect(new HandleRef(null, app.HWND), out pptWindowRect);
      
          double pptWidth = pptWindowRect.Right - pptWindowRect.Left;
          double pptHeight = pptWindowRect.Bottom - pptWindowRect.Top;
      
          // 以下计算将使ToolBar位于PowerPoint窗口的中下方
          double toolBarLeft = pptWindowRect.Left + (pptWidth / 2) - (toolBar.Width / 2);
          double toolBarTop = pptWindowRect.Bottom - toolBar.Height;
      
          toolBar.Left = toolBarLeft;
          toolBar.Top = toolBarTop;
      }
      

      上述代码使用了GetWindowRect函数(需要引入User32.dll库)来获取PowerPoint窗口的大小和位置。然后根据这些信息计算出ToolBar应放置的坐标。

    2. 监听PowerPoint窗口移动事件: 要让ToolBar随PowerPoint窗口移动而实时更新其位置,需要监听PowerPoint窗口的大小更改和移动事件。由于VSTO API并未直接提供此类事件,您可以利用Windows消息处理机制来监听WM_SIZEWM_MOVE消息。为此,向您的外接程序类添加一个WndProc方法,并注册一个隐藏的辅助窗口来接收这些消息。

      [DllImport("user32.dll", SetLastError = true)]
      static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
      
      [DllImport("user32.dll", SetLastError = true)]
      static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
      
      [DllImport("user32.dll")]
      static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
      
      const int GWL_WNDPROC = -4;
      IntPtr prevWndProcPtr = IntPtr.Zero;
      
      protected override void OnStartup()
      {
          base.OnStartup();
      
          prevWndProcPtr = SetWindowLong(new HandleRef(null, app.HWND), GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(WndProc));
      }
      
      private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
      {
          if (msg == 0x0005 /* WM_SIZE */ || msg == 0x0003 /* WM_MOVE */)
          {
              UpdateToolBarPosition();
          }
      
          return CallWindowProc(prevWndProcPtr, hWnd, msg, wParam, lParam);
      }
      

      这段代码通过SetWindowLong函数为PowerPoint窗口设置一个新的窗口过程回调(即WndProc方法),以便捕获WM_SIZEWM_MOVE消息。当接收到这些消息时,调用UpdateToolBarPosition方法来重新计算并更新ToolBar的位置。

    3. 实现UpdateToolBarPosition方法: 创建一个方法来根据当前PowerPoint窗口的位置和大小重新计算并设置ToolBar的位置。

      private void UpdateToolBarPosition()
      {
          RECT pptWindowRect;
          GetWindowRect(new HandleRef(null, app.HWND), out pptWindowRect);
      
          double pptWidth = pptWindowRect.Right - pptWindowRect.Left;
          double pptHeight = pptWindowRect.Bottom - pptWindowRect.Top;
      
          double toolBarLeft = pptWindowRect.Left + (pptWidth / 2) - (toolBar.Width / 2);
          double toolBarTop = pptWindowRect.Bottom - toolBar.Height;
      
          MoveWindow(toolBar.Handle, (int)toolBarLeft, (int)toolBarTop, (int)toolBar.Width, (int)toolBar.Height, true);
      }
      

      此方法与SetToolBarInitialPosition类似,但它是动态响应PowerPoint窗口变化时调用的。使用MoveWindow函数直接设置ToolBar的屏幕坐标。

    现在,将上述代码片段整合到您的外接程序中,ToolBar应该会始终固定在PowerPoint窗口的中下方,并随PowerPoint窗口的移动而自动跟随。记得在ShowToolBar方法中调用SetToolBarInitialPosition方法:

    private void ShowToolBar()
    {
        try
        {
            toolBar = new ToolBar(app);
            SetPowerPointWindowOwner();
            SetToolBarInitialPosition();
            toolBar.Show();
            // ...其他事件注册代码...
        }
        // ...异常处理代码...
    }
    

    这样,您就实现了所需的WPF窗体嵌入PowerPoint并跟随其移动的功能。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(13条)

报告相同问题?

问题事件

  • 系统已结题 4月24日
  • 已采纳回答 4月16日
  • 创建了问题 4月15日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价