【问题标题】:Pen and marker using PowerPoint 2010 interop使用 PowerPoint 2010 互操作的笔和标记
【发布时间】:2023-03-30 09:43:01
【问题描述】:

我是 PowerPoint 互操作的新手,我正在尝试在演示模式下绘制 红笔黄色文本标记(不是形状!!!)线条。

更新:

我可以这样画一条线:

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

但是这条线总是黑而细的。如何为其选择红色笔或黄色文本标记?

除了 DrawLine 方法,我可以通过设置为用户选择笔(鼠标光标变成笔而不是箭头):

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

但是如何将其设置为文本标记?黄色是 65535,如何获得文本标记样式(大笔、透明度)而不是小实心笔?

【问题讨论】:

  • 如果只有一个幻灯片窗口,那不就是SlideShowWindows[0]吗?
  • 我试过 [0] 但它导致一个异常告诉我我应该选择一个介于 1 和 1 之间的索引......所以显然它不是从零开始的......
  • Ppl,赏金快结束了,真的没有办法吗?

标签: c# .net add-in powerpoint office-interop


【解决方案1】:
  1. 创建一个透明且最顶层的 WPF 窗口(编辑:不要最大化窗口)

    <Window ... Background="#00000000" Topmost="True" ShowInTaskbar="False" WindowStyle="None" AllowsTransparency="True" ResizeMode="NoResize">
    
  2. (NEW) 使用 GetWindowRect 获取幻灯片放映窗口的位置和大小

    [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. 将透明窗口放在 PowerPoint 幻灯片窗口上方

    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. 将 Canvas 放入 WPF 窗口并根据需要向其中添加 Polyline 对象。文本标记线可能是这样的:

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

    根据需要向 line.Points 添加点。致电this.canvas.Children.Clear() 清除所有绘图。

这是一种解决方法,但我会说你最好的选择。

【讨论】:

  • 我试过了,但覆盖窗口总是出现在主屏幕上......我错过了什么?
  • 对不起,最大化的窗口好像有问题,slideshowWindow的位置好像不对。我编辑了我的答案,这样它可以在任何屏幕上工作
【解决方案2】:

我的示例应用程序从创建PowerPoint.Application 类的实例开始;

PowerPoint.Application PowerPointApplication = new PowerPoint.Application();

然后我将Visible属性设置为msoTrue

PowerPointApplication.Visible = Core.MsoTriState.msoTrue;

然后创建PresentationSlide

PowerPoint.Presentations PowerPointPresentationSet = PowerPointApplication.Presentations;

PowerPoint._Presentation PowerPointPresentation = PowerPointPresentationSet.Add();
PowerPoint.Slides PowerPointSlideSet = PowerPointPresentation.Slides;

PowerPoint._Slide PowerPointSlide = PowerPointSlideSet.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);

在我的代码中,我创建了一个Shape 对象;

PowerPoint.Shape PowerPointShape = PowerPointSlide.Shapes.AddLine(100, 100, 500, 500);

然后,我这样格式化;

PowerPointShape.Line.Weight = 10;
PowerPointShape.Line.ForeColor.RGB = 65535;

PowerPointShape.Line.Transparency = 0.8f;

关键是,Opacity 会随着 Transparency 属性的减少而增长。

你可以设置Line.Weight属性为thinnerticker线,你可以设置Foreground.RGB属性的值来改变线的颜色。

PS:我添加了这些namespaces代码文件usings区;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Core = Microsoft.Office.Core;

您可以从此链接找到有效的解决方案; http://snipt.org/nsgk7

【讨论】:

  • 是的,我也这样做是为了快速解决问题,但我有 3 个问题: 1. 在大小合适的演示文稿中绘制形状而不是标记真的很慢(你可以看到创建的形状,重量调整,颜色设置等) 2. 在此之后,我的演示文稿中有真实的形状,如果您想保留或松开注释,我更喜欢标记和问题。 3. 如果我在两个不同主题的演示文稿中使用它,形状看起来会有所不同(例如圆形笔或矩形笔)...
  • 我编辑了问题以强调我正在寻找标记,而不是形状。不过谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多