Search code examples
c#.netadd-inpowerpointoffice-interop

Pen and marker using PowerPoint 2010 interop


I'm new to PowerPoint interop and I'm trying to draw red pen and yellow text marker (not shapes!!!) lines while in presentation mode.

UPDATE:

I can draw a line like this:

settings = presentation.SlideShowSettings; 
window = settings.Run(); 
window.View.DrawLine(startX, startY, endX, endY); 

But the line will always be black and thin. How can I select the red pen or yellow text marker for it?

Aside from the DrawLine method, I can select the pen for the user (mouse cursor becomes a pen instead of arrow) by setting:

window.View.PointerType = PpSlideShowPointerType.ppSlideShowPointerPen;
window.View.PointerColor.RGB = 255;

But how can I set it to text marker? yellow would be 65535, how do I get the text marker style (bigger pen, transparency) instead of the tiny solid pen?


Solution

    1. Create a WPF window that is transparent and topmost (EDIT: do not maximize window)

      <Window ... Background="#00000000" Topmost="True" ShowInTaskbar="False" WindowStyle="None" AllowsTransparency="True" ResizeMode="NoResize">
      
    2. (NEW) Use GetWindowRect to get the location and size of the slide show window

      [DllImport("user32.dll", SetLastError = true)]
      [return: MarshalAs(UnmanagedType.Bool)]
      static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
      [StructLayout(LayoutKind.Sequential)]
      private struct RECT
      {
          public int Left;
          public int Top;
          public int Right;
          public int Bottom;
      }
      
    3. Place the transparent window over the PowerPoint slideshow window

      settings = presentation.SlideShowSettings;
      slideshowWindow = settings.Run();
      
      RECT rect = new RECT();
      GetWindowRect(new IntPtr(slideshowWindow.HWND), ref rect);        
      overlayWindow.Left = rect.Left; // EDIT: do not use slideshowWindow.Left, etc.
      overlayWindow.Top = rect.Top;
      overlayWindow.Width = rect.Width;
      overlayWindow.Height = rect.Height;
      
    4. Put a Canvas into the WPF window and add Polyline objects to it as needed. Text marker lines could be like this:

      line = new Polyline
      {
          Opacity = 0.8,
          Stroke = new SolidColorBrush(Colors.Yellow),
          StrokeThickness = 20
      };
      this.canvas.Children.Add(line);
      

      Add points to line.Points as needed. Call this.canvas.Children.Clear() to clear all your drawings.

    It's a workaround but I'd say your best shot.