Một số kỹ thuật trong lập trình trên .Net Compact FrameWork (CF)

Cung cấp một máy tính ảo chạy trên một máy tính cá nhân thông thường

Tạo ra các phần cứng (ảo) chạy Mircosoft Windows CE trên máy tính

Sử dụng hệ điều hành Windows CE và các component Pocket PC

Đảm bảo sự tương đồng giữa thiết bị PocketPC thật và thiết bị giả lập

 

ppt101 trang | Chia sẻ: Mr Hưng | Lượt xem: 808 | Lượt tải: 0download
Bạn đang xem trước 20 trang nội dung tài liệu Một số kỹ thuật trong lập trình trên .Net Compact FrameWork (CF), để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Một số kỹ thuật trong lập trình trên .Net Compact FrameWork (CF)Tổng quan về .NET Compact FrameWorkCác thư viện lớp đối tượng được hỗ trợNhững khác biệt chính với .Net FrameWorkCác thư viện lớp đối tượng được hỗ trợNhững khác biệt chính với .Net FrameWorkGiới thiệu về Smart Device ExtensionsLựa chọn target platformVai trò của trình giả lập Pocket PCVai trò của trình giả lập Pocket PCCung cấp một máy tính ảo chạy trên một máy tính cá nhân thông thườngTạo ra các phần cứng (ảo) chạy Mircosoft Windows CE trên máy tínhSử dụng hệ điều hành Windows CE và các component Pocket PCĐảm bảo sự tương đồng giữa thiết bị PocketPC thật và thiết bị giả lậpXác định đường dẫn của Ứng dụngXác định tự động đường dẫn của ứng dụng (runtime)m_startuppath = System.Reflection.Assembly.GetExecutingAssembly(). GetModules()[0].FullyQualifiedName;m_startuppath = m_startuppath.Replace( System.Reflection.Assembly.GetExecutingAssembly(). GetModules()[0].Name, "" );m_BmBanCo =new Bitmap (m_startuppath+"BanCoPocketPC.jpg");m_BmQuanCo=new Bitmap (m_startuppath+"QuanCoPocKetPC.bmp");m_BmChonCo=new Bitmap (m_startuppath+"ChonQuanPocketPC.bmp");Xóa tên file, chỉ giữ lại đường dẫnTrọn vẹn tên và đường dẫn của moduleTên của moduleXác định đường dẫn của Ứng dụngSử dụng đường dẫn tuyệt đối (hard-code)!!!Phải biết trước đường dẫn (tuyệt đối) sẽ chứa chương trình thực thipublic class Constant{ public static int LEFT = 24; public static int TOP = 24; public static string AppPath = @"\Program Files\MummyMaze\"; public static string ImagesPath = @"\Program Files\MummyMaze\";}Load ảnh từ fileCó thể load các ảnh từ file vào đối tượng kiểu BitmapCác định dạng ảnh thông dụng mà WinCE hỗ trợ (BMP, JPG, PNG)Bitmap RedMummyBmp = new Bitmap(Constant.ImagesPath+"redmummy.bmp");Bitmap HelloBmp = new Bitmap(Constant.ImagesPath+"hello.jpg");Sử dụng Timer (1)Khai báo biến thuộc kiểu System.Windows.Forms.TimerKhởi tạo biến Timerprivate System.Windows.Forms.Timer MyTimer;private void InitializeComponent(){ this.MyTimer = new System.Windows.Forms.Timer(); this.MyTimer.Interval = 300; // 300 ms this.MyTimer.Tick += new System.EventHandler(this.MyTimer_Func);}Tên hàm xử lýTimerSử dụng Timer (2)Hàm xử lý mỗi khi xảy ra biến cố timerprivate void MyTimer_Func (object sender, System.EventArgs e){ flag = 1 - flag; pictureBox1.Image = CompleteBmp[flag]; pictureBox1.Refresh();}Sử dụng Timer (3)Kích hoạt timerTạm dừng timerHủy bỏ timerMyTimer.Enabled = true;MyTimer.Enabled = false;MyTimer.Dispose();Sử dụng Graphicspublic void Draw(Graphics g){ ImageAttributes imgatt = new ImageAttributes(); imgatt.SetColorKey (Constant.BkColor, Constant.BkColor); g.DrawImage(HumanBmp, new Rectangle(left, top, width, height), Bmp_x*Constant.WidthSquare_pixel, Bmp_y*Constant.WidthSquare_pixel, Constant.WidthSquare_pixel, Constant.WidthSquare_pixel, GraphicsUnit.Pixel, imgatt);}Sử dụng ThreadKhai báo biến kiểu ThreadTạo thread và khởi động threadHàm xử lý chính của Threadprivate void PlaySound(){ SoundThread = new Thread(new ThreadStart(PlaySoundFunc)); SoundThread.Priority = ThreadPriority.Highest; SoundThread.Start(); // Bắt đầu thread}private Thread SoundThread;private void PlaySoundFunc(){ Sound.PlayMusic(Constant.AppPath + "music.wav");}Tên hàm xử lý chính của ThreadXử lý Âm thanh (1)public class Sound { [DllImport("WinMM.dll", EntryPoint="PlaySound",CharSet=CharSet.Auto)] private static extern int PlaySoundWin32 (string pszSound, int hmod, int fdwSound) ; [DllImport("CoreDll.dll", EntryPoint="PlaySound",CharSet=CharSet.Auto)] private static extern int PlaySoundWinCE (string pszSound, int hmod, int fdwSound) ;.........Xử lý Âm thanh (2)private enum SND { SND_SYNC = 0x0000, /* play synchronously (default) */ SND_ASYNC = 0x0001, /* play asynchronously */ SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */ SND_MEMORY = 0x0004, /* pszSound points to a memory file */ SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */Xử lý Âm thanh (3)private enum SND {...... SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */ SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */ SND_ALIAS = 0x00010000, /* name is a registry alias */ SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */......}Xử lý Âm thanh (4)private enum SND {...... SND_FILENAME = 0x00020000, /* name is file name */ SND_RESOURCE = 0x00040004, /* name is resource name or atom */ SND_PURGE = 0x0040, /* purge non-static events for task */ SND_APPLICATION = 0x0080 /* look for application specific association */};Xử lý Âm thanh (5) private const int Win32 = 0 ; private const int WinCE = 1 ; private static int Windows = -1 ; public static void PlayMusic(string pszMusic) { int flags = (int)(SND.SND_ASYNC|SND.SND_FILENAME| SND.SND_NOWAIT|SND.SND_LOOP) ; sndPlaySound(pszMusic, flags) ; }Xử lý Âm thanh (6)private static void sndPlaySound (string pszSound, int flags) { switch ( Windows ) { case Win32 : PlaySoundWin32(pszSound, 0, flags) ; break ; case WinCE : PlaySoundWinCE(pszSound, 0, flags) ; break ; ......Xử lý Âm thanh (7) default : try // Play if in Win32 platform { PlaySoundWin32(pszSound, 0, flags) ; Windows = Win32 ; } catch ( Exception ) { try // Play if in WinCE platform { PlaySoundWinCE(pszSound, 0, flags) ; Windows = WinCE ; } catch ( Exception ) { } } break ; } // switch}Thao tác đồ họa trên .Net Compact FrameworkTham khảo .NET Compact Framework Programming with C#, Paul Yao, David Durant (2004), Prentice Hall PTRChương 15 - .Net Compact Framework GraphicsChương 16 – Text and FontsĐối tượng GraphicsCách 1: Sử dụng đối tượng Graphics được truyền vào trong tham số của hàm xử lý sự kiện Paintprivate void FormMain_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; // draw}Cách 2: Tự tạo ra đối tượng Graphics (lưu ý: cần giải phóng vùng nhớ sau khi dùng xong)Graphics g = CreateGraphics();// Drawg.Dispose();Cách 3: sử dụng phương thức tĩnh Graphics.FromImage để nhận được đối tượng graphics của ảnhXác định màu3 cách để xác định màuDùng màu hệ thống (System.Drawing.SystemColors) Dùng màu được định nghĩa sẵnDùng bộ giá trị RGBDanh sách các màu được dùng trong hệ thốngActiveBorderActiveCaptionActiveCaptionTextAppWorkspaceControlControlDarkControlDarkDarkControlLightControlLightLightControlTextDesktopGrayTextHighlightHighlightTextHotTrackInactiveBorderInactiveCaptionInactiveCaptionTextInfoInfoTextMenuMenuTextScrollBarWindowWindowFrameWindowTextMàu cụ thể tương ứng với mỗi hằng số sẽ thay đổi tùy theo từng hệ thống cụ thểVí dụprivate void FormMain_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; g.Clear(SystemColors.Window);}Ví dụHàm FromArgb (không có thành phần alpha trên .Net CF)public static Color FromArgb( int red, int green, int blue); Ví dụ:private void FormMain_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; g.Clear(Color.FromArgb(204,204,204)); }Tạo BrushTrên .Net CF chỉ hỗ trợ solid brush và bitmap brushClass: System.Drawing.SolidBrush Constructor: public SolidBrush( Color color);Ví dụ: Brush brRed = new SolidBrush(Color.Red); Brush brGreen = new SolidBrush(Color.FromArgb(0, 255, 0)); Brush brWindowText = new SolidBrush(SystemColors.WindowText); Tạo bitmapConstructor tạo bitmap rỗng public Bitmap ( int width, int height); Vẽ lên Bitmapprivate void CreateAndDraw(int x, int y){ // Create a bitmap and a Graphics object for the bitmap. Bitmap bmpNew = new Bitmap(100,100); Graphics gbmp = Graphics.FromImage(bmpNew); // Clear the bitmap background. gbmp.Clear(Color.LightGray); // Get a Graphics object for the form. Graphics g = CreateGraphics(); // Copy the bitmap to the window at (x,y) location. g.DrawImage(bmpNew, x, y); // Clean up when we are done. g.Dispose(); gbmp.Dispose(); bmpNew.Dispose();}Tạo Bitmap từ fileConstructor tạo Bitmap từ file public Bitmap ( string filename); Các dạng file được hỗ trợBitmap (.bmp) (1, 4, 8, hay 24 bit màu)JPEG (.jpg)GIF (.gif)PNG (.png)Ví dụ:try{ bmpNew = new Bitmap(strFileName);}catch{ MessageBox.Show("Cannot create bitmap from File: " + strFileName);}Tạo Bitmap từ Resource private Bitmap LoadBitmapResource(string strName){ Assembly assembly = Assembly.GetExecutingAssembly(); string strRes = "ShowBitmap." + strName; Stream stream = assembly.GetManifestResourceStream(strRes); Bitmap bmp = null; try { bmp = new Bitmap(stream); } catch { } stream.Close(); return bmp;}Tạo Bitmap từ Resource (tt)private void DisposeBitmap(ref Bitmap bmp){ if (bmp != null) { bmp.Dispose(); } bmp = null;}private void mitemResourceCup_Click(object sender, EventArgs e){ DisposeBitmap(ref bmpDraw); bmpDraw = LoadBitmapResource(“abc.BMP"); Invalidate();}Hiển thị Bitmap Các trường hợp sử dụng:Hiển thị toàn bộ bitmap với kích thước gốcHiển thị một phần bitmap với kích thước gốcHiển thị một phần bitmap với kích thước được thay đổiHiển thị một phần bitmap với kích thước được thay đổi và có vùng trong suốtHiển thị toàn bộ bitmap với kích thước gốcHàmpublic void DrawImage( Image image, int x, int y); Ví dụ:private void FormMain_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; int x = 10; int y = 10; g.DrawImage(bmpDraw, x, y);}Hiển thị một phần bitmap với kích thước gốc Hàm public void DrawImage( Image image, int x, int y, Rectangle srcRect, GraphicsUnit srcUnit); Trên .NetCF, tham số srcUnit chỉ có một chọn lựa là PixelHiển thị một phần bitmap với kích thước được thay đổiHàm: public void DrawImage( Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit); Hiển thị một phần bitmap với kích thước được thay đổi và có vùng trong suốtHàmpublic void DrawImage ( Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr); Lưu ý: tham số imgatt cho phép xác định dải màu sẽ được vẽ trong suốt khi hiển thị ảnh imgatt.SetColorKey (Color.FromArgb(r1, g1, b1), Color.FromArgb(r2, g2, b2)); Những màu có giá trị (r, g, b) thỏa r1rr2, g1gg2, r1rr3 sẽ không được vẽ (tức là transparent)Thao tác đồ họa vectorDrawEllipseVẽ đường viền ellipse (với viết được chọn)DrawLineVẽ đoạn thẳng (với viết được chọn)DrawPolygonVẽ đường biên đa giác (với viết được chọn)DrawRectangleVẽ đường biên hình chữ nhật (với viết được chọn)FillEllipseTô ellipse (với brush được chọn)FillPolygonTô đa giác (với brush được chọn)FillRectangleTô hình chữ nhật (với brush được chọn)Tạo bút vẽClass: System.Drawing.Pen Constructor: public Pen( Color color); Ví dụ: // Pen from a system color Pen penCtrl = new Pen(SystemColors.ControlDark); // Pen from a named color Pen penRed = new Pen(Color.Red); // Pen from an RGB value Pen penBlue = new Pen(Color.FromArgb(0, 0, 255));Sử dụng bút vẽ và brushprivate void FormMain_Paint(object sender, PaintEventArgs e){ Brush brText = new SolidBrush(SystemColors.WindowText); e.Graphics.DrawString("Simple Draw String", Font, brText, xDraw, yDraw); // Highlight origin. int x = (int)xDraw; int y = (int)yDraw; Pen penBlack = new Pen(Color.Black); e.Graphics.DrawLine(penBlack, x, y, x-8, y); e.Graphics.DrawLine(penBlack, x, y, x, y-8);}Xuất chuỗi ký tựHàm public void DrawString( string str, Font font, Brush brText, float x, float y); Ví dụprivate void FormMain_Paint(object sender, PaintEventArgs e){ Brush brText = new SolidBrush(SystemColors.WindowText); e.Graphics.DrawString("Simple Draw String", Font, brText, xDraw, yDraw); // Highlight origin. int x = (int)xDraw; int y = (int)yDraw; Pen penBlack = new Pen(Color.Black); e.Graphics.DrawLine(penBlack, x, y, x-8, y); e.Graphics.DrawLine(penBlack, x, y, x, y-8);}Thay đổi thuộc tính FontKhác với .Net Framework, trong .Net Compact Framework, việc thay đổi thuộc tính BackColor, Cursor, Font, và ForeColor trong control cha không làm thay đổi các thuộc tính này trong các control con. Do đó, cần tự thay đổi các thuộc tính này cho các control cần thiếtGeneric Font Hàm khởi tạo Generic Fontpublic Font ( FontFamily family, float emSize, FontStyle style); Ví dụFont font = new Font (FontFamily.GenericSansSerif, 10, FontStyle.Regular); Generic FontFontFamily.GenericMonospace: sử dụng fixed-pitch font (ví dụ Courier New)FontFamily.GenericSansSerif: sử dụng variable-pitch font không có chân (ví dụ: Arial trên Windows, Tahoma trên Pocket PC)FontFamily.GenericSerif: sử dụng variable-pitch font có chân (ví dụ Times New Roman trên Windows)Generic FontFontStyle.BoldFontStyle.ItalicFontStyle.RegularFontStyle.StrikeoutFontStyle.UnderlineGeneric Font// Create monospace bold 10-point font.Font fontMono = new Font(FontFamily.GenericMonospace, 10, FontStyle.Bold);// Create sans serif italic 10-point font.Font fontSans = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Italic);// Create serif italic and underlined 10-point font.Font fontSerif = new Font(FontFamily.GenericSerif, 10, FontStyle.Italic | FontStyle.Underline);Tạo font từ tên font Hàm khởi tạo Named Fontpublic Font( string familyName, float emSize, FontStyle style); Tạo font từ tên fontprivate void FormMain_Paint(object sender, PaintEventArgs e){ Graphics g = e.Graphics; float x = 10; float y = 10; Font font1 = new Font("Tahoma", 14, FontStyle.Regular); Font font2 = new Font("Courier New", 10, FontStyle.Regular); Font font3 = new Font("Bookdings", 12, FontStyle.Regular); Brush brText = new SolidBrush(SystemColors.WindowText); g.DrawString("14 Point Tahoma", font1, brText, x, y); SizeF sizeX = g.MeasureString("X", font1); y += sizeX.Height; g.DrawString("10 Point Courier New", font2, brText, x, y); sizeX = g.MeasureString("X", font2); y += sizeX.Height; g.DrawString("12 Point Bookdings", font3, brText, x, y);}Tạo font từ tên fontprivate void FormMain_Load(object sender, System.EventArgs e){ Font fontBookDings = new Font("BookDings", 14, FontStyle.Bold); // [ ] button cmdNext.Font = fontBookDings; cmdNext.Text = "4"; // [ >> ] button cmdForward.Font = fontBookDings; cmdForward.Text = "7";}Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)public struct CHOOSECOLOR{ public int lStructSize; public IntPtr hwndOwner; public IntPtr hInstance; public int rgbResult; public IntPtr lpCustColors; public int Flags; public int lCustData; public IntPtr lpfnHook; public IntPtr lpTemplateName;};Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)public const int CC_RGBINIT = 0x00000001;public const int CC_FULLOPEN = 0x00000002;public const int CC_PREVENTFULLOPEN = 0x00000004;public const int CC_ENABLEHOOK = 0x00000010;public const int CC_ENABLETEMPLATE = 0x00000020;public const int CC_ENABLETEMPLATEHANDLE = 0x00000040;public const int CC_SOLIDCOLOR = 0x00000080;public const int CC_ANYCOLOR = 0x00000100;public static int INVALID_HANDLE_VALUE = -1;public const int LMEM_FIXED = 0x0000;Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)[DllImport("coredll.dll")]public static extern IntPtr LocalAlloc (int uFlags, int uBytes);[DllImport("coredll.dll")]public static extern IntPtr LocalFree (IntPtr hMem);[DllImport("commdlg.dll")]public static extern int ChooseColor (ref CHOOSECOLOR lpcc);Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)public bool Init(Control ctrlParent){ // Allocate the array for initial colors. int cbColorData = 16 * 4; IntPtr ipColors = LocalAlloc(LMEM_FIXED, cbColorData); if (ipColors == IntPtr.Zero) return false; m_cc = new CHOOSECOLOR(); m_cc.lStructSize = Marshal.SizeOf(m_cc); m_cc.hwndOwner = GetHwndFromControl(ctrlParent); m_cc.hInstance = IntPtr.Zero; m_cc.rgbResult = 0; m_cc.lpCustColors = ipColors; m_cc.Flags = CC_RGBINIT; m_cc.lCustData = 0; m_cc.lpfnHook = IntPtr.Zero; m_cc.lpTemplateName = IntPtr.Zero; return true;}Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)public bool ShowDialog (ref Color clrValue){ int iRet = 0; byte Red = clrValue.R; byte Green = clrValue.G; byte Blue = clrValue.B; m_cc.rgbResult = (Blue > 8); Blue = (byte)((m_cc.rgbResult & 0xff0000) >> 16); clrValue = Color.FromArgb(Red, Green, Blue); return true; } else return false;}Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)//// Focus functions//[DllImport("coredll.dll")]public static extern IntPtr GetFocus ();[DllImport("coredll.dll")]public static extern IntPtr SetFocus (IntPtr hWnd);Phụ lục: Hộp thoại chọn màu (gọi WinAPI bằng P/Invoke)public IntPtr GetHwndFromControl (Control ctrl){ IntPtr hwndControl; // Check whether the control has focus. if (ctrl.Focused) { hwndControl = GetFocus(); } else { IntPtr ipFocus = GetFocus(); ctrl.Focus(); hwndControl = GetFocus(); SetFocus(ipFocus); } return hwndControl;}Lập trình mạng trên .Net Compact FrameworkTham khảoMicrosoft® .NET Compact Framework Kick Start, Erik Rubin, Ronnie Yates(2003), Sams Publishing Chapter 5. Network Connectivity with the .NET Compact Framework Nội dungSử dụng SocketSử dụng kết nối UDPSử dụng HttpWebRequestSử dụng kết nối hồng ngoại Một số nhận xét ban đầuCác port có giá trị dưới 1024 thường được HĐH dành riêng cho các dịch vụ chuẩnNên chọn port có giá trị từ 2000 đến 60000Chọn lựa giữa TCP/IP và UDPTCP: đảm bảo “error-free delivery”, chậm hơn UDPUDP: không đảm bảo “error-free delivery”, nhanh hơn TCPTạo kết nối tại ClientSử dụng địa chỉ IPEndPoint l_EndPoint = new IPEndPoint (IPAddress.Parse( "172.68.25.34“), Convert.ToInt16(9981)); Sử dụng tên của server (dùng DNS)IPHostEntry l_IPHostEntry =Dns.Resolve("www.mycomputer.net"); EndPoint l_EndPoint = new IPEndpoint(l_IPHostEntry.AddressList[0], 9981); Tạo kết nối tại Clienttry{ Socket l_Socket = new Socket(Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); l_Socket.Connect(l_EndPoint); if (l_Socket.Connected) { // l_Socket is now ready to send and receive data }}catch (SocketException e){ /* do something about it */ }Nhận yêu cầu kết nối tại Hostm_listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);m_listenSocket.Bind(new IPEndPoint(IPAddress.Any, 8758));m_listenSocket.Listen((int)SocketOptionName.MaxConnections);m_connectedSocket = m_listenSocket.Accept();if (m_connectedSocket != null){ if (m_connectedSocket.Connected) { // Someone has connected to us. } }Truyền tin bằng SocketTruyền dữ liệu: Socket.Send Send (Byte[] buffer) Send (Byte[] buffer, SocketFlags socketFlags) Send (Byte[] buffer, Int32 size, SocketFlags socketFlags) Send (Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) Ví dụ: l_Socket.Send (l_buffer, 16, SocketFlags.None); Truyền tối đa 16 byte dữ liệu trong l_buffer l_Socket.Send (l_buffer, 2, 6, SocketFlags.None); Truyền tối đa 6 byte dữ liệu trong l_buffer từ byte thứ 3Truyền tin bằng Socketl_Socket.Send(Encoding.ASCII.GetBytes("Send me") );l_Socket.Send(Encoding.Unicode.GetBytes("Send me") );l_Socket.Send(Encoding.ASCII.GetBytes(Convert.ToString(2003)) );l_Socket.Send(Encoding.ASCII.GetBytes(Convert.ToString(2.71)) );Nhận tin bằng SocketNhận dữ liệu: Socket.ReceiveReceive (Byte[] buffer) Receive (Byte[] buffer, SocketFlags socketFlags) Receive (Byte[] buffer, Int32 size, SocketFlags socketFlags) Receive (Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) Ví dụ: l_Socket.Receive (l_buffer, 16, SocketFlags.None); Nhận tối đa 16 byte dữ liệu vào l_buffer l_Socket.Receive (l_buffer, 2, 6, SocketFlags.None); Nhận tối đa 6 byte dữ liệu ghi vào l_buffer từ byte thứ 3Nhận tin bằng Socketstring l_ASCII = Encoding.ASCII.GetString(l_Buffer); string l_ASCII = Encoding.ASCII.GetString(l_Buffer); int l_Integer = Convert.ToInt32(Encoding.ASCII.GetString(l_Buffer)); Double l_Double = Convert.ToDouble(Encoding.ASCII.GetString(l_Double)); Sử dụng kết nối UDPUDP và TCPUDP: connectionless protocol TCP: connection-oriented protocol Sử dụng class UdpClient void Connect(String hostname, Int32 port);void Send(Byte[] dgram, Int32 bytes);void Receive(ref IPEndPoint remoteEP); Ví dụ: l_UdpClient.Connect(IPAddress.Parse("172.68.25.34"), 9981); Send(aBuffer, aBuffer.Length, "www.mycomputer.net", 9981);Sử dụng UdpClient IPEndPoint senderIP = new IPEndPoint(IPAddress.Parse("192.168.0.200"), Convert.ToInt32(8758));UdpClient l_UdpClient = new UdpClient();l_UdpClient.Connect(senderIP);for (int i = 0; i string query = "[Location] Ball State University"; Những appointment item không có thông tin Location cũng bị loại bỏ!!!Gửi SMSOutlookSession aSession = new OutlookSession();SmsMessage sendMsg = new SmsMessage("18664365702", "Vote to Idol #2");aSession.SmsAccount.Send(sendMsg);aSession.Dispose();Account nào???Gửi SMSSmsMessage sendMsg = new SmsMessage();Recipient recv = new Recipient("18664365702");sendMsg.Body = "Vote to Idol #2";sendMsg.To.Add (recv);MessagingApplication.DisplayComposeForm(sendMsg) Nhận SMSusing Microsoft.WindowsMobile.PocketOutlook.MessageInterception; msginterceptor = new MessageInterceptor();msginterceptor.InterceptionAction = InterceptionAction.NotifyAndDelete; // Notifymsginterceptor.MessageReceived += newMessageInterceptorEventHandler(msginterceptor_MessageReceived);Nhận SMS //Handling received messagevoid msginterceptor_MessageReceived(object sender,MessageInterceptorEventArgs e){ SmsMessage smsMsg = (SmsMessage)e.Message; string fullText = "Message From: “+smsMsg.From.Name; fullText += (" at "+ smsMsg.Received.TimeOfDay.ToString()); fullText += (" and the message is: "+smsMsg.Body); MessageBox.Show(fullText, "New Text Message !");}

Các file đính kèm theo tài liệu này:

  • pptnet_compactframework_8526.ppt
Tài liệu liên quan