這篇文章給大家分享的是有關怎么使用C#實現一個PPT遙控器的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
本項目參考了 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。
在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;
}
});
}首先,由于涉及到了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 = true;
dynamic presentations = T(pptApp.Presentations);
// 打開 PPT
this.presentation = T(presentations.Open(filename));
// 全屏顯示
T(this.presentation.SlideShowSettings).Run();
}PPT上一個動畫操作實現
T(T(presentation.SlideShowWindow).View).Previous();
下一步,與上一個操作類似,只需更換Previous()方法為Next()即可。
首先我們需要一個方法去解析注釋
private string GetInnerText(dynamic part)
{
StringBuilder sb = new StringBuilder();
dynamic shapes = T(T(part).Shapes);
int shapesCount = shapes.Count;
for (int i = 0; i < shapesCount; i++)
{
dynamic shape = T(shapes[i + 1]);
var textFrame = T(shape.TextFrame);
// MsoTriState.msoTrue==-1
if (textFrame.HasText == -1)
{
string text = T(textFrame.TextRange).Text;
sb.AppendLine(text);
}
sb.AppendLine();
}
return sb.ToString();
}之后通過
dynamic notesPage = T(T(T(T(presentation.SlideShowWindow).View).Slide).NotesPage); string notesText = GetInnerText(notesPage);
我們就可以獲取具體每頁的注釋信息。
了解了以上的PPT操作之后,我們就需要去完善我們的Web服務器端配置。
用戶訪問相應的地址,然后去執行上面PPT操作部分的代碼即可。
else if (path == "/getNote")
{
string notesText = null;
this.Invoke(new Action(() => {
if (this.presentation == null)
{
return;
}
try
{
dynamic notesPage = T(T(T(T(presentation.SlideShowWindow).View).Slide).NotesPage);
notesText = GetInnerText(notesPage);
}
catch (COMException ex)
{
notesText = "";
}
}));
await response.WriteAsync(notesText);
}
else if (path == "/next")
{
response.StatusCode = 200;
this.Invoke(new Action(() => {
if (this.presentation == null)
{
return;
}
try
{
T(T(this.presentation.SlideShowWindow).View).Next();
hasRun = true;
} catch (COMException e)
{
hasRun = false;
}
}));
if (hasRun)
{
await response.WriteAsync("OK");
}
else
{
await response.WriteAsync("NO");
}
}
else if (path == "/previous")
{
response.StatusCode = 200;
this.Invoke(new Action(() => {
if (this.presentation == null)
{
return;
}
try
{
T(T(this.presentation.SlideShowWindow).View).Previous();
hasRun = true;
}
catch (COMException e)
{
hasRun = false;
}
}));
if (hasRun)
{
await response.WriteAsync("OK");
}
else
{
await response.WriteAsync("NO");
}通過輪詢的方式,不斷的向服務端發送請求,獲取最新的消息,這樣我們就可以實現通過瀏覽器去操作PPT了。
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit" />
<title>操作你的PPT</title>
<link rel="icon" href="/logo.ico" rel="external nofollow" >
<style>
div {
font-size: 25px
}
</style>
</head>
<body>
<div id="main" >
<p id="note"></p>
</div>
<script src="hammer.min.js"></script>
<script>
function httpGet(url, cb) {
fetch(url, {
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
method: 'GET'
}).then(response => response.text())
.then(text => {
cb(text)
})
.catch(e => {
return null
})
}
const note = document.querySelector("#note");
let hasRun = true
let getNotes = setInterval(() => {
httpGet('/getNote', (text) => {
note.innerText = text
})
}, 500)
function nextPage() {
httpGet('/next', (text) => {
if (text == 'NO') {
clearInterval(getNotes)
note.innerText = "幻燈片播放完畢!"
hasRun = false
} else {
if (!hasRun) {
getNotes = setInterval(() => {
httpGet('/getNote', (text) => {
note.innerText = text
})
}, 500)
hasRun = true
}
}
})
}
function previousPage() {
httpGet('/previous', (text) => {
if (text == 'NO') {
clearInterval(getNotes)
note.innerText = "幻燈片播放完畢!"
hasRun = false
} else {
if (!hasRun) {
getNotes = setInterval(() => {
httpGet('/getNote', (text) => {
note.innerText = text
})
}, 500)
hasRun = true
}
}
})
}
var hammer = new Hammer(document.querySelector("#main"));
hammer.on("swipeleft", function () {
nextPage();
});
hammer.on("swiperight", function () {
previousPage();
});
</script>
</body>
</html>感謝各位的閱讀!關于“怎么使用C#實現一個PPT遙控器”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。