Một số kỹ thuật trong lập trình trên .Net CF - ThS. Trần Minh Triết

ppt 16 trang phuongnguyen 5410
Bạn đang xem tài liệu "Một số kỹ thuật trong lập trình trên .Net CF - ThS. Trần Minh Triết", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptmot_so_ky_thuat_trong_lap_trinh_tren_net_cf_ths_tran_minh_tr.ppt

Nội dung text: Một số kỹ thuật trong lập trình trên .Net CF - ThS. Trần Minh Triết

  1. Đại học Khoa học Tự nhiên, ĐHQG-HCM Khoa Công Nghệ Thông Tin Chương 3: Một số kỹ thuật trong lập trình trên .Net CF ThS. Trần Minh Triết 1 1
  2. Xác định đường dẫn của Ứng dụng Lấy danh sách các Xác định tự động đường dẫn của ứng dụng (runtime) Assembly m_startuppath = System.Reflection.Assembly.GetExecutingAssemblyTrọn(). vẹn tên và GetModules()[0].FullyQualifiedName; đường dẫn của m_startuppath = m_startuppath.Replace( module System.Reflection.Assembly.GetExecutingAssembly(). GetModules()[0].Name, "" ); Xóa tên file, m_BmBanCo =new Bitmap chỉ giữ lại (m_startuppath+"BanCoPocketPC.jpg"); đường dẫn m_BmQuanCo=new Bitmap Tên của module (m_startuppath+"QuanCoPocKetPC.bmp"); m_BmChonCo=new Bitmap (m_startuppath+"ChonQuanPocketPC.bmp"); 2 2
  3. Xác định đường dẫn của Ứng dụng Sử 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 thi public 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\"; } 3 3
  4. Load ảnh từ file Có thể load các ảnh từ file vào đối tượng kiểu Bitmap Cá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"); 4 4
  5. Sử dụng Timer (1) Khai báo biến thuộc kiểu System.Windows.Forms.Timer private System.Windows.Forms.Timer MyTimer; Khởi tạo biến Timer 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ý Timer 5 5
  6. Sử dụng Timer (2) Hàm xử lý mỗi khi xảy ra biến cố timer private void MyTimer_Func (object sender, System.EventArgs e) { flag = 1 - flag; pictureBox1.Image = CompleteBmp[flag]; pictureBox1.Refresh(); } 6 6
  7. Sử dụng Timer (3) Kích hoạt timer MyTimer.Enabled = true; Tạm dừng timer MyTimer.Enabled = false; Hủy bỏ timer MyTimer.Dispose(); 7 7
  8. Sử dụng Graphics public 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); } 8 8
  9. Sử dụng Thread Khai báo biến kiểu Thread private Thread SoundThread; Tạo thread và khởi động thread Tên hàm xử lý chính private void PlaySound() của Thread { SoundThread = new Thread(new ThreadStart(PlaySoundFunc)); SoundThread.Priority = ThreadPriority.Highest; SoundThread.Start(); // Bắt đầu thread } Hàm xử lý chính của Thread private void PlaySoundFunc() { Sound.PlayMusic(Constant.AppPath + "music.wav"); } 9 9
  10. Xử 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) ; 10 10
  11. 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 */ 11 11
  12. 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 */ } 12 12
  13. 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 */ }; 13 13
  14. 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) ; } 14 14
  15. 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 ; 15 15
  16. 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 } 16 16