说明
本项目参考了 https://github.com/yangzhongke/PhoneAsPrompter 项目来完成实现,并对其进行了一些修改完善。
完整代码可以到 https://github.com/PuZhiweizuishuai/PPT-Remote-control 与 https://gitee.com/puzhiweizuishuai/PPT-Remote-control 查看。
软件下载地址: https://gitee.com/puzhiweizuishuai/PPT-Remote-control/releases/v1.0.0
另外,由于程序启动后会创建一个WEB服务器,用来显示PPT的操控界面,所以某些安全软件可能会报毒。但是程序本身是没有问题的。
截图
具体实现
通过在Win Form项目中内嵌一个Kestrel Web服务器,我们就可以通过浏览器向web服务器发送请求来接收远程操作指令。之后通过Late Binding的方式去操作PPT。
1、在 Win Form项目中内嵌HTTP服务器
在Form窗口启动时,我们新建一个Kestrel服务器
this.webHost = new WebHostBuilder() .UseKestrel() .Configure(ConfigureWebApp) .UseUrls("http://*:" + port) .Build(); // 异步运行服务器 this.webHost.RunAsync();
然后对其进行配置
private void ConfigureWebApp(IApplicationBuilder app) { app.UseDefaultFiles(); app.UseStaticFiles(); app.Run(async (context) => { // 处理非静态请求 var request = context.Request; var response = context.Response; string path = request.Path.Value; response.ContentType = "application/json; charset=UTF-8"; bool hasRun = true; if (path == "/report") { string value = request.Query["value"]; this.BeginInvoke(new Action(() => { this.PageLabel.Text = value; })); response.StatusCode = 200; await response.WriteAsync("ok"); } else { response.StatusCode = 404; } }); }
操作PPT
首先,由于涉及到了COM编程,我们需要注意内存回收与释放,所以需要用到COMReferenceTracker
类进行应用管理。
每一步用到COM的地方,都要用T方法进行资源回收。
private dynamic T(dynamic comObj) { return this.comReference.T(comObj); }
以下操作使用dynamic
进行操作,所有操作需要去查询VBA文档了解具体用法,以下仅演示部分操作
打开一个PPT的操作实现
private void button1_Click(object sender, EventArgs e) { // 文件选择框 openFileDialog.Filter = "ppt文件|*.ppt;*.pptx;*.pptm"; if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } string filename = openFileDialog.FileName; this.ClearComRefs(); // 创建 PPT 对象 dynamic pptApp = T(PowerPointHelper.CreatePowerPointApplication()); // 显示 PPT pptApp.Visible <b>本文来源gao@!dai!ma.com搞$$代^@码5网@</b>= true; dynamic presentations = T(pptApp.Presentations); // 打开 PPT this.presentation = T(presentations.Open(filename)); // 全屏显示 T(this.presentation.SlideShowSettings).Run(); }