Laien's

成功 自在 健康 好状态

【原创】WIFI手机实现的PPT翻页器(2)——手机端(C#实现)

without comments

接上文,WIFI手机实现PPT翻页器,手机端程序的开发及流程如下:
1、使用VS2008创建智能设备项目,.NET FrameWork选择3.5版本;
2、编程语言使用C#;
3、手机上的客户端程序在检查IP地址和端口合法性后,启用翻页按钮和相关菜单;
4、程序根据用户通过菜单选择的命令向手机上的服务端程序发送指令;
5、手机上必须安装.NET Compact FrameWork 3.5才能运行改程序。
6、对于HTC Touch2 T3333型号的手机,支持通过音量控制键翻页。

关键技术
1.C# Windows Mobile编程
2.C# Socket编程
3.C# 调用Win32API对键盘进行Hook
4.C# 正则表达式使用
相关文件WirelessPresenter.cs代码如下:

?Download download.txt
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
 
 
namespace Wireless_Presenter
{
    public partial class WirelessPresenter_WM : Form
    {
        private bool pptInited = false; // 准备好了,可以翻页
        private bool inputValidate = false; // 文本框内字符校验通过
        public WirelessPresenter_WM()
        {
            InitializeComponent();
        }
 
        // 退出
        private void mnuExit_Click(object sender, EventArgs e)
        {
            if (0 != this.hHookKey)
            {
                this.HookStop(); // 停止Hook
            }
            this.Close();
        }
 
        // 参数环境准备
        private void mnuStart_Click(object sender, EventArgs e)
        {
            inputValidate = checkInput(); // 检查IP地址和端口的合法性
            if (inputValidate)
            {
                pptInited = sendCommand("TEST"); // 测试连接可用性
                if (pptInited)
                {
                    PrepareParameters(); // 准备参数
                    this.HookStart(); // 启动Hook
                }
            }
            else
            {
                MessageBox.Show("请检查参数格式:)");
            }
        }
 
        // IP地址和端口的合法性检查
        private bool checkInput()
        {
            Regex regexIPAddress = new Regex(@"^\d+\.\d+\.\d+\.\d+$");
            Regex regexPort = new Regex(@"^\d+$");
 
            if (regexIPAddress.IsMatch(txtServerIP.Text) && regexPort.IsMatch(txtServerPort.Text))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        private void mnuReset_Click(object sender, EventArgs e)
        {
            resetParameters(); // 重置程序参数
        }
 
        // 准备参数,可以操作PPT了
        private void PrepareParameters()
        {
            txtServerIP.Enabled = false;
            txtServerPort.Enabled = false;
            btnPageDown.Enabled = true;
            btnPageUp.Enabled = true;
            mnuHome.Enabled = true;
            mnuEnd.Enabled = true;
            mnuBlack.Enabled = true;
            mnuWhite.Enabled = true;
            mnuQuit.Enabled = true;
            //pptInited = false;
        }
 
 
        // 重置程序参数
        private void resetParameters()
        {
            txtServerIP.Enabled = true;
            txtServerPort.Enabled = true;
            btnPageDown.Enabled = false;
            btnPageUp.Enabled = false;
            mnuHome.Enabled = false;
            mnuEnd.Enabled = false;
            mnuBlack.Enabled = false;
            mnuWhite.Enabled = false;
            mnuQuit.Enabled = false;
 
            pptInited = false;
            if (0 != this.hHookKey)
            {
                this.HookStop(); // 停止Hook
            }
        }
 
 
 
 
        // PPT前一页
        private void btnPageUp_Click(object sender, EventArgs e)
        {
            sendCommand("UP");
            //sendCommand("GET /\r\n\r\n");
        }
 
        // PPT后一页
        private void btnPageDown_Click(object sender, EventArgs e)
        {
            sendCommand("DOWN");
        }
 
        // 切换到幻灯片首页
        private void mnuHome_Click(object sender, EventArgs e)
        {
            sendCommand("HOME");
        }
 
        // 切换到幻灯片尾页
        private void mnuEnd_Click(object sender, EventArgs e)
        {
            sendCommand("END");
        }
 
        // 黑屏切换
        private void mnuBlack_Click(object sender, EventArgs e)
        {
            sendCommand("BLACK");
        }
 
        // 白屏切换
        private void mnuWhite_Click(object sender, EventArgs e)
        {
            sendCommand("WHITE");
        }
 
        // 退出放映
        private void mnuQuit_Click(object sender, EventArgs e)
        {
            sendCommand("QUIT");
        }
 
        // 显示帮助
        private void mnuHelp_Click(object sender, EventArgs e)
        {
            ShowHelp();
        }
 
        // 帮助信息
        private void ShowHelp()
        {
            StringBuilder strHelp = new StringBuilder();
            strHelp.Append("WIFI手机PPT翻译器\n");
            strHelp.Append("For Windows Mobile 6.5\n");
            strHelp.Append(".NET CF 3.5\n\n");
            strHelp.Append("Version:1.0\n");
            strHelp.Append("Author:i@isclab.org\n");
            strHelp.Append("Site:http://i.isclab.org\n");
            strHelp.Append("Date:2010-02-23");
 
            MessageBox.Show(strHelp.ToString(), "Wireless Presenter");
        }
 
 
        // 创建Socket发送翻页指令
        private bool sendCommand(string txtCommand)
        {
            IPAddress address = IPAddress.Parse(txtServerIP.Text);
            int port = Convert.ToInt32(txtServerPort.Text);
            IPEndPoint myIPEndPoit = new IPEndPoint(address, port);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                s.Connect(myIPEndPoit);
                if (s.Connected)
                {
                    byte[] buffer = Encoding.ASCII.GetBytes(txtCommand);
                    s.Send(buffer, buffer.Length, 0);
                    //byte[] buffer_read = new byte[100] ;
                    //s.Receive(buffer_read);
                    //MessageBox.Show(Encoding.UTF8.GetString(buffer_read,0,buffer_read.Length).ToString());
                    s.Close();
                    return true;
 
                }
                else
                {
                    MessageBox.Show("连接PC端失败!\n请确认网络连接参数!");
                    return false;
                }
            }
            catch
            {
                MessageBox.Show("Socket初始化失败!\n请确认网络连接参数");
                return false;
            }
 
        }
 
 
        public delegate int HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
 
        private HookKeyProc hookKeyDeleg;
        private int hHookKey = 1;
 
        private int key_VOLUMEDOWN = 0; // 音量增加键被按下的次数
        private int key_T_VOLUMEUP = 0; // 音量降低键被按下的次数
        #region public methods
 
 
        //安装钩子
        public void HookStart()
        {
 
            if (hHookKey != 0)
            {
 
                //  如果已经安装则卸载
 
                this.HookStop();
 
            }
 
            hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
 
 
            hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
 
 
            if (hHookKey == 0)
            {
 
                throw new SystemException("Failed acquiring of the hook.");
 
            }
 
        }
 
        //拆除钩子
        public void HookStop()
        {
            UnhookWindowsHookEx(hHookKey);
        }
 
        #endregion
 
        #region protected and private methods
 
        private int HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
        {
 
            KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
 
            if (code < 0)
 
                return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
 
            if (hookStruct.vkCode == 0x5B)
            {
                //如果捕捉到VK_LWIN按键
                //......处理......             
 
                return -1; //返回-1表示已经处理了,不再往下传递
            }
 
            if (hookStruct.vkCode == 0x26)
            {
                //如果捕捉到VK_LEFT按键
                //......处理......
                return -1;
            }
 
            if (hookStruct.vkCode == 0x26)
            {
                //如果捕捉到VK_LEFT按键
                //......处理......
 
                return -1;
            }
 
            // 117和118 是HTC Touch2 T3333系统中获取到的按键数值,其他系统可能稍作调整
            if (hookStruct.vkCode == 0x75)
            {
                // VOLUMEUP
                if (1 == (this.key_T_VOLUMEUP++ % 2) && pptInited)
                {
                    this.sendCommand("UP");
                }
                return -1;
            }
 
            if (hookStruct.vkCode == 0x76)
            {
                //V UP
                if (1 == (this.key_VOLUMEDOWN++ % 2) && pptInited)
                {
                    this.sendCommand("DOWN");
                }
                return -1;
            }
 
            //如果你想知道该键的值
            //MessageBox.Show(hookStruct.vkCode.ToString());
            return -1;
 
            //测试发现会死机
            //int iCall = CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
            // 没处理的键的消息往下传递
            //return iCall;
 
        }
 
 
        #endregion
 
        #region P/Invoke declarations
 
        [DllImport("coredll.dll")]
 
        private static extern int SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);
        //private static extern int SetWindowsHookEx(int type, HookMouseProc HookMouseProc, IntPtr hInstance, int m);
 
 
        [DllImport("coredll.dll")]
 
        private static extern IntPtr GetModuleHandle(string mod);
 
        [DllImport("coredll.dll")]
 
        private static extern int CallNextHookEx(
 
                HookKeyProc hhk,
 
                int nCode,
 
                IntPtr wParam,
 
                IntPtr lParam
 
                );
 
        [DllImport("coredll.dll")]
 
        private static extern int GetCurrentThreadId();
 
        [DllImport("coredll.dll", SetLastError = true)]
 
        private static extern int UnhookWindowsHookEx(int idHook);
 
        private struct KBDLLHOOKSTRUCT
        {
 
            public int vkCode;
 
            public int scanCode;
 
            public int flags;
 
            public int time;
 
            public IntPtr dwExtraInfo;
 
        }
 
 
        const int WH_KEYBOARD_LL = 20;
 
        public class KeyBoardInfo
        {
 
            public int vkCode;
 
            public int scanCode;
 
            public int flags;
 
            public int time;
 
        }
 
 
        #endregion
 
 
 
    }
}

完整项目代码将在后续文档中发布,请关注本站最新更新内容。
WIFI手机的PPT翻页器界面预览:

【原创】WIFI手机实现的PPT翻页器(1b)——PC端(Python实现)

【原创】WIFI手机实现的PPT翻页器(1a)——PC端(C#实现)

Written by Laien

February 24th, 2010 at 1:51 am

Posted in C#, Programming