整体思路
在电脑中建立手机服务端接听手机并且根据命令控制PPT进行演示。
通过开发 Powerpoint 插件,建立服务端和PPT之间联系,再通过WCF来打通服务器和手机之间通讯。
1、我们先开发PPT 插件,在Visual Studio 中建立 PowerPoint 外接程序项目(对于office 版本,自己根据需要而定)
2、并在项目添加WCF 服务契约,以及实现服务契约类。大致代码如下
(服务接口)
[ServiceContract]
public interface IPPTPhoneControl
{
[OperationContract(Name = "GetServerStateJson")]
[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetServerState", BodyStyle = WebMessageBodyStyle.Bare)]
String GetServerState();
[OperationContract(Name = "SendMessageJson")]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "SendMessageJson", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
String SendMessage(string Message);
}
public class PPTPhoneControl : IPPTPhoneControl
{
public static Func<String> QueryServerState
{
get;
set;
}
public static Func<String, String> ClientSendMessage
{
get;
set;
}
public string GetServerState()
{
return QueryServerState();
}
public string SendMessage(string Message)
{
return ClientSendMessage(Message);
}
}
3、插件代码
public partial class ThisAddIn
{
PowerPoint.SlideShowWindow Wpn;
ServiceHost host;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
this.Application.SlideShowBegin += new PowerPoint.EApplication_SlideShowBeginEventHandler(Application_SlideShowBegin);
this.Application.SlideShowEnd += new PowerPoint.EApplication_SlideShowEndEventHandler(Application_SlideShowEnd);
}
void Application_SlideShowEnd(PowerPoint.Presentation Pres)
{
host.Close();
}
void Application_SlideShowBegin(PowerPoint.SlideShowWindow Wn)
{
Wpn = Wn;
if (MessageBox.Show("是否开启手机控制?","消息",MessageBoxButtons.YesNo,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
host = new ServiceHost(typeof(PPTPhoneControl));
PPTPhoneControl.QueryServerState = this.ClientQueryServerState;
PPTPhoneControl.ClientSendMessage = this.ClientSendCommand;
try
{
host.Open();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
host.Close();
}
}
}
public String ClientQueryServerState()
{
try
{
return "已和服务建立联系";
}
catch
{
return "服务器解析错误!";
}
}
public String ClientSendCommand(String Command)
{
String ReturnString = "";
try
{
switch (Command)
{
case "Next":
Wpn.View.Next();
break;
case "First":
Wpn.View.First();
break;
case "Last":
Wpn.View.Last();
break;
case "Perv":
Wpn.View.Previous();
break;
default:
break;
}
}
catch(Exception e)
{
return "服务端已关闭";
}
return ReturnString;
}
#endregion
}
插件代码很好读懂,主要就是WCF 服务器HOST创建,以及接收手机的消息,处理等。
4、WCF 配置文件
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetUrl="mex" httpGetEnabled="true"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebHttpBindingBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="LivePPT.Service.PPTPhoneControl">
<!--<endpoint address="xml" behaviorConfiguration="WebHttpBindingBehavior"
binding="webHttpBinding" contract="WCFServiceLib.IHomePlay" />-->
<endpoint address="json" binding="webHttpBinding" contract="LivePPT.Service.IPPTPhoneControl" behaviorConfiguration="WebHttpBindingBehavior"/>
<host>
<baseAddresses>
<add baseAddress="http://Localhost:8089" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
到此服务端编码完毕,在VS中运行项目即可启动PowerPoint,当运行幻灯片时候就会提示是否开启手机控制。
开启手机控制,也就开启了8089(配置中)端口,手机也就可以根据此端口进行通讯了。