[WM][C#][定時 morning call 我的老闆]
如果你想要定時播打電話給你的老闆或是親朋好友,你如果還在睡覺又不想起床拿著手機按下無聊的按鍵怎麼辦。我們可以透過驅動外部檔案 CoreDLL.dll 裡面 CeRunAppAtTime api 來達成,請參考http://msdn.microsoft.com/en-us/library/ms908103.aspx
Step1:首先開啟vs2008編輯器,新增一個c# 智慧型裝置專案挑選主控台應用程式
Step2:接著將滑鼠移到方案總管點選參考,在按下右鍵點選功能表上的加入參考
Step3:加入Microsoft.WindowsMobile.Telephony元件按下確定
Step4:撰寫程式碼到Program.cs
01 using ...Microsoft.WindowsMobile.Telephony;
02
03 namespace SmartDeviceProject1
04 ...{
05 class Program
06 ...{
07 static void Main(string[] args)
08 ...{
09 Phone phone = new Phone();
10 //設定你要打的電話號碼
11 phone.Talk("099123456");
12 }
13 }
14 } Step5:在專案裡面加入\新增專案為c#智慧型裝置專案
Step6:Form1.cs表單上面產生三個元件一個Button設定執行時間功能,二個dateTimePicker要用來設定時間
Step7:撰寫Form1.cs
01 using ...System;
02 using System.ComponentModel;
03 using System.Windows.Forms;
04 using System.Runtime.InteropServices;
05
06 namespace SmartDeviceProject2
07 ...{
08 public partial class Form1 : Form
09 ...{
10 [DllImport("CoreDLL.dll")]
11 public static extern bool CeRunAppAtTime(string pwszAppName, SystemTime lpTime);
12 [DllImport("CoreDLL.dll")]
13 public static extern int FileTimeToSystemTime(ref long lpFileTime, SystemTime lpSystemTime);
14 [DllImport("CoreDLL.dll")]
15 public static extern int FileTimeToLocalFileTime(ref long lpFileTime, ref long lpLocalFileTime);
16 [StructLayout(LayoutKind.Sequential)]
17 public class SystemTime
18 ...{
19 public ushort wYear;
20 public ushort wMonth;
21 public ushort wDayOfWeek;
22 public ushort wDay;
23 public ushort wHour;
24 public ushort wMinute;
25 public ushort wSecond;
26 public ushort wMilliseconds;
27 }
28 public Form1()
29 ...{
30 InitializeComponent();
31 }
32
33 private void button1_Click(object sender, EventArgs e)
34 ...{
35
36 int y=dateTimePicker1.Value.Year;
37 int M=dateTimePicker1.Value.Month;
38 int d=dateTimePicker1.Value.Day;
39 int h=dateTimePicker2.Value.Hour;
40 int m=dateTimePicker2.Value.Minute;
41 int s=dateTimePicker2.Value.Second;
42 DateTime dt = new DateTime(y, M, d, h, m, s);
43 long FileTime = dt.ToFileTime();
44 long LocalTime = 0;
45 FileTimeToLocalFileTime(ref FileTime, ref LocalTime);
46 SystemTime StartTime = new SystemTime();
47 FileTimeToSystemTime(ref LocalTime, StartTime);
48 String App = @"\Program Files\SmartDeviceProject1\SmartDeviceProject1.exe";
49 CeRunAppAtTime(App, StartTime);
50
51 }
52
53
54 }
55 }
Step8:來測一下設定執行時間後按下Set鍵,等一下看看手機是否會如設定時間自動播打電話
Step9:時間到了就自動啟動打電話,可以用Cellular Emulator來看是否有打電話
Step10:源碼下載
參考http://www.cnblogs.com/fox23/archive/2008/02/03/AtTime.html