|
本帖最后由 只为设计 于 2016-2-29 19:40 编辑
有时候我们需要在PPT中自定义一些固定的参考线。比如二等分线、黄金分割线、三等分线等。借助编程可以很容易实现。
下面的代码是OneKeyTools中“分割线”→“二等分线”的源代码。
【基本代码】↓
- PowerPoint.Slide slide = app.ActiveWindow.View.Slide;
- PowerPoint.Selection sel = app.ActiveWindow.Selection;
- if (sel.Type == PowerPoint.PpSelectionType.ppSelectionNone)
- {
- float swidth = app.ActivePresentation.PageSetup.SlideWidth;
- float sheight = app.ActivePresentation.PageSetup.SlideHeight;
- PowerPoint.Shape line1 = slide.Shapes.AddLine(swidth * 0.5f, 0, swidth * 0.5f, sheight);
- PowerPoint.Shape line2 = slide.Shapes.AddLine(0, sheight * 0.5f, swidth, sheight * 0.5f);
- line1.Visible = Office.MsoTriState.msoTrue;
- line2.Visible = Office.MsoTriState.msoTrue;
- line1.Name = "line01";
- line2.Name = "line01";
- if (slide.Background.Fill.ForeColor.RGB > 255 + 200 * 256 + 200 * 256 * 256)
- {
- line1.Line.ForeColor.RGB = 0;
- line2.Line.ForeColor.RGB = 0;
- }
- else
- {
- line1.Line.ForeColor.RGB = 255 + 255 * 256 + 255 * 256 * 256;
- line2.Line.ForeColor.RGB = 255 + 255 * 256 + 255 * 256 * 256;
- }
- line1.Line.DashStyle = Office.MsoLineDashStyle.msoLineDash;
- line2.Line.DashStyle = Office.MsoLineDashStyle.msoLineDash;
- }
复制代码
【代码分析】↓
- PowerPoint.Slide slide = app.ActiveWindow.View.Slide;
- float swidth = app.ActivePresentation.PageSetup.SlideWidth;
- float sheight = app.ActivePresentation.PageSetup.SlideHeight;
复制代码 ↑这段代码是获取PPT幻灯片页面的宽度和高度,用于算二等分线的具体坐标。
- PowerPoint.Shape line1 = slide.Shapes.AddLine(swidth * 0.5f, 0, swidth * 0.5f, sheight);
- PowerPoint.Shape line2 = slide.Shapes.AddLine(0, sheight * 0.5f, swidth, sheight * 0.5f);
复制代码 ↑这段代码是往页面中添加二等分线。其中AddLine(起始x坐标,起始y坐标,终止x坐标,终止y坐标)。
- if (slide.Background.Fill.ForeColor.RGB > 255 + 200 * 256 + 200 * 256 * 256)
复制代码 ↑这个判断,是判断幻灯片背景的颜色,如果深色就用白色线;如果背景浅,就用黑色线
- line1.Name = "line01";
- line2.Name = "line01";
复制代码 ↑这是对横竖两条二等分线进行命名,方便后面做删除功能。
|
|