UMLÈí¼þ¹¤³Ì×éÖ¯

ʹÓà Microsoft .NET ¿ò¼ÜѹËõ°æ±àдÒƶ¯ÓÎÏ·
Ravi Krishnaswamy Microsoft Corporation

ÕªÒª£ºÁ˽âÈçºÎ´´½¨»ùÓÚ .NET ¿ò¼ÜѹËõ°æµÄÓÎÏ·¡£Á˽â±àдÃæÏòСÐÍÉ豸µÄÓÎÏ·µÄÖ÷ÒªÒªÇ󣬲¢ÇÒÀí½â .NET ¿ò¼ÜѹËõ°æÄܹ»ÇáËɵØÂú×ãÕâЩҪÇó¡£±¾ÎÄ°üÀ¨Ò»Ð©¸ß¼¶ÐÔÄÜÓÅ»¯¼¼Êõ£¬Äú¿ÉÒÔÓÃÀ´Í»ÆÆÓÎÏ·µÄ¼«ÏÞ¡£

¼ò½é

Microsoft?.NET ¿ò¼ÜѹËõ°æÊÇÍêÕû Microsoft .NET ¿ò¼ÜµÄ×Ó¼¯¡£ËüÊǶÔÍêÕûµÄ .NET ¿ò¼Ü½øÐÐѹËõºóµÃµ½µÄ°æ±¾£¬Ä¿µÄÊÇÔÚ²»Ó°ÏìÓû§Õý³£Ê¹ÓõÄÇ°ÌáÏ£¬¸üºÃµØÊÊÓ¦×ÊÔ´Êܵ½ÏÞÖƵÄÉ豸¡£ËäÈ»Æä¹æÄ£´ó´ó¼õС£¬µ«¶àÊý¹¦ÄÜÈÔÈ»±£³ÖÍêÕû£¬´Ó¶øʹ¿ª·¢ÈËÔ±Äܹ»»ñµÃÔöÇ¿µÄÐÔÄÜ¡£

ÃæÏò .NET ¿ò¼ÜѹËõ°æµÄºÃ´¦ºÜ¶à£¬ÆäÖаüÀ¨£º¿ÉÒÔÕë¶Ô Pocket PC ºÍÆäËû Windows CE .NET É豸½øÐе¥Ò»µÄ¶þ½øÖƲ¿Êð£¬Ìá¸ß¿ª·¢ÈËÔ±µÄ¹¤×÷ЧÂÊ£¬ÒÔ¼°¼Ó¿ì²úƷͶ·ÅÊг¡µÄËٶȡ£

ÔÚ±¾°×ƤÊéÖУ¬ÎÒ½«ÌÖÂÛ±àдÃæÏòСÐÍÉ豸µÄÓÎÏ·µÄÖ÷ÒªÒªÇ󣬲¢ÇÒ˵Ã÷ .NET ¿ò¼ÜѹËõ°æÄܹ»ÇáËɵØÂú×ãÕâЩҪÇó¡£ÎÒ»¹½«ÌÖÂÛһЩ¸ß¼¶ÐÔÄÜÓÅ»¯¼¼Êõ£¬Äú¿ÉÒÔʹÓÃÕâЩ¼¼ÊõÀ´Í»ÆÆÓÎÏ·µÄ¼«ÏÞ¡£×ÜÖ®£¬Äú½«Á˽⵽ʹÓà .NET ¿ò¼ÜѹËõ°æÀ´¿ª·¢ºÍÓÅ»¯ÓÎÏ·ÊǶàôÈÝÒס£Õ⽫ÊÇÒ»´ÎÓÐȤµÄÂÃÐУ¬ÇëϵºÃ°²È«´ø²¢³ä·ÖÏíÊÜÆäÖеÄÀÖȤ°É¡£

±¾ÎļÙÉè¶ÁÕß´óÌåÉÏ¶Ô .NET ¿ò¼ÜѹËõ°æºÍÓÎÏ·¿ª·¢ÓÐÁËÒ»¶¨³Ì¶ÈµÄÁ˽⡣

È«ÆÁÓÎÏ·´°Ìå

ÔÚÓÎÏ·Ó¦ÓóÌÐòÖУ¬Ê¹ÓÃÉ豸µÄÈ«ÆÁÏÔʾ¹¦ÄÜͨ³£ÊÇ¿ÉÈ¡µÄ¡£Õ¼¾ÝÕû¸öÆÁÄ»ÇøÓòµÄ´°Ìå³ÆΪȫÆÁ´°Ì壨Ҳ³ÆΪÓÎÏ·´°Ì壩¡£»»¾ä»°Ëµ£¬È«ÆÁ ´°ÌåÕ¼¾Ý×ÀÃ棨¼´¹¤×÷Çø£©ÒÔ¼°·Ç¹¤×÷Çø£¬È綥²¿µÄ±êÌâ/µ¼º½À¸¡¢±ß¿òºÍµ×²¿µÄ²Ëµ¥À¸¡£

Ó¦ÓóÌÐòͨ¹ý½«Æä WindowState ÉèÖÃΪ Maximized À´´´½¨È«ÆÁ ´°Ì壬ÈçÏÂËùʾ£º

form.WindowState = FormWindowState.Maximized;

Èç¹û¸Ã´°Ì帽¼ÓÁ˲˵¥À¸£¨ºÍ/»ò Pocket PC ÖеŤ¾ßÀ¸£©£¬Ôò²»ÄÜʹÆä³ÉΪȫÆÁ ´°Ìå¡£

ÔÚ Pocket PC µÄ .NET ¿ò¼ÜѹËõ°æ 1.0 ÖУ¬Òª´´½¨È«ÆÁÓ¦ÓóÌÐò£¬±ØÐëÔÚ´°ÌåµÄ OnLoad ÄÚ²¿ÉèÖà WindowState ÊôÐÔ¡£

ÏÂÃæµÄͼ 1 ºÍͼ 2 ²ûÃ÷ÁË Pocket PC ÖеÄÈ«ÆÁ ´°ÌåºÍ·ÇÈ«ÆÁ ´°ÌåÖ®¼äµÄÇø±ð¡£

ͼ 1. ·ÇÈ«ÆÁ´°Ìå

ͼ 2. È«ÆÁ´°Ìå]

ÓµÓÐÈ«ÆÁ ´°ÌåµÄÖ÷Òªº¬ÒåÊÇûÓбêÌâ/µ¼º½À¸»ò²Ëµ¥À¸¡£Ó¦ÓóÌÐò±ØÐ뿼ÂÇÕâЩÒòËØ£¬²¢ÇÒÔÚ±ØҪʱÌṩ±Ü¿ªÕâЩ¹¦ÄܵÄÊֶΡ£

Èç¹ûÄúÖ»ÊÇÏ£ÍûÄúµÄ´°Ìå½öÌî³ä¿ÉÓõÄ×ÀÃæÇøÓò£¨¶ø²»ÊÇÈ«ÆÁ£©£¬ÔòÎÞÐë×öÈκÎÊÂÇ顣ĬÈÏÇé¿öÏ£¬.NET ¿ò¼ÜѹËõ°æ»á×Ô¶¯µ÷Õû´°ÌåµÄ´óСÒÔÌî³ä Pocket PC µÄÆÁÄ»¡£

ÊÂʵÉÏ£¬Äú×îºÃ²»ÒªÃ÷È·ÉèÖô°ÌåµÄ ClientSize ÒÔ´ïµ½ÕâÖÖЧ¹û£¬ÒòΪÕâ¿ÉÄÜ»á·Á°­Ó¦ÓóÌÐòÔÚ¸÷ÖÖ Windows CE .NET É豸֮¼äµÄ»¥²Ù×÷ÐÔ¡£ÀýÈ磬Èç¹ûÄúÃ÷È·µ÷ÕûÓ¦ÓóÌÐòµÄ´óСÒÔÆ¥ÅäÆäÖÐÒ»¸öÉ豸µÄ´°ÌåÖ¸Êý£¬Ôò¸Ã´óСÔÚÆäËûÉ豸ÉÏ¿ÉÄܲ¢²»ÀíÏë¡£Ã÷ÖǵÄ×ö·¨ÊDzÉÓô°ÌåµÄĬÈÏ´óС¡£

ÖØд OnPaint ºÍ OnPaintBackground

µäÐ͵ÄÓÎÏ·Ó¦ÓóÌÐò»á¶Ô´°ÌåÄÚÈݽøÐÐ×Ô¶¨Òå»æÖÆ¡£Ëü²ÉÈ¡µÄ°ì·¨ÊÇÖØд¿Ø¼þµÄ OnPaint() ʼþ²¢¶Ô´°ÌåµÄ»æÖƽøÐÐ×Ô¶¨Òå´¦Àí¡£

protected override void OnPaint(PaintEventArgs paintg)
{
Graphics gx = paintg.Graphics;
// Custom draw using the graphics object
}

ÿµ±¿Ø¼þ¿ªÊ¼»æÖÆʱ£¬¶¼»áÊ×ÏÈ×Ô¶¯Ë¢ÐÂÆä±³¾°¡£ÀýÈ磬ÔÚ´¦Àí OnPaint() ÒÔ»æÖƿؼþµÄÄÚÈÝʱ£¬½«Ê×ÏÈÓà this.Backcolor ÖÐÖ¸¶¨µÄÑÕÉ«À´»æÖƱ³¾°¡£ÔÚÉÏÊöÓÉÓ¦ÓóÌÐò×÷ΪËùÓÐÕß»æÖÆ´°ÌåµÄÇéÐÎÏ£¬Õâ¿ÉÄܲ¢²»ÀíÏ룻×Ô¶¯»æÖƱ³¾°»áµ¼Ö±³¾°ÔÚÓ¦ÓóÌÐòÓлú»áÍê³ÉÇ°¾°µÄ»æÖÆ֮ǰ³öÏÖ˲¼äµÄÉÁ˸¡£

Òª±ÜÃâÕâһĬÈÏÐÐΪ£¬Ç¿ÁÒ½¨Òéÿµ±Ó¦ÓóÌÐòÖØд OnPaint() ·½·¨Ê±£¬¶¼ÒªÖØд OnPaintBackground() ·½·¨²¢×Ô¼ºÀ´»æÖƱ³¾°¡£Ó¦ÓóÌÐò¿ÉÒÔÑ¡ÔñÔÚ OnPaint() ÄÚ²¿´¦ÀíËùÓлæÖƹ¤×÷£¬²¢½« OnPaintBackground() ±£ÁôΪ¿Õ£¬ÈçÏÂÃæµÄʾÀýËùʾ¡£

protected override void OnPaintBackground(PaintEventArgs paintg)
{
// Left empty, avoids undesirable flickering
}

ÓÃÓڻ滭µÄÀëÆÁλͼ¼¼Êõ

ͨ¹ý¿Ø¼þµÄ this.CreateGraphics() À´»ñÈ¡ÆÁÄ»µÄͼÐζÔÏó²¢Ö±½ÓÏòÆä½øÐлæÖÆ£¬Äú¿ÉÒÔ½øÐÐÆÁÉÏ»æÖÆ¡£Äú±ØÐë¼ÇסÔÚ²»ÔÙÐèÒªÆÁÉÏͼÐζÔÏóʱ½«Æä´¦Öá£Èç¹û²»ÕâÑù×ö£¬¿ÉÄܵ¼ÖÂÓÐÏÞµÄÏÔʾÉ豸×ÊÔ´³öÏÖ²»×ã¡£

ÈçÉÏÒ»½ÚËùʾ£¬Äú¿ÉÒÔÔÚ OnPaint() ºÍ OnPaintBackground() ·½·¨ÄÚͨ¹ý PaintEventArgs.Graphics À´·ÃÎÊÆÁÄ»µÄͼÐζÔÏó¡£ÔÚÖ´Ðлæͼ·½·¨ÒÔºó£¬ÕâЩͼÐζÔÏ󽫱»×Ô¶¯´¦Öá£

¶ÔÓÚÖ±½ÓÔÚÆÁÄ»ÉÏ»æÖƵÄÓÎÏ·Ó¦ÓóÌÐò¶øÑÔ£¬Çé¿öͨ³£²»Ì«ÀíÏë¡£ÕâÊÇÒòΪ£¬µ±ÄúÔÚÆÁÄ»ÉÏ»æÖƶà¸ö¶ÔÏóʱ£¬½«»á¿ªÊ¼¿´µ½ÆÁÄ»ÉÁ˸¡£Îª±ÜÃâÕâÖÖÏÖÏó£¬ÓÎÏ·¿ª·¢ÈËԱͨ³£»á²ÉÓÃÀëÆÁ »æÖƼ¼Êõ¡£

ÕâÒ»¼¼ÊõµÄ˼ÏëÊÇ´´½¨ÀëÆÁλͼ£¬ÎªÆä»ñȡͼÐζÔÏó£¬Õë¶ÔËü£¨ÔÚÄÚ´æÖУ©Ö´ÐÐËùÓлæͼ²Ù×÷£¬È»ºó½«µÃµ½µÄÀëÆÁλͼ¸´ÖƵ½ÆÁÄ»ÉÏ¡£

// Create off-screen graphics
Bitmap bmpOff = new Bitmap(this.ClientRectangle.Width,
this.ClientRectangle.Height);
Graphics gxOff = Graphics.FromImage(bmpOff);

ÔÚ¸ÃʾÀýÖУ¬ÎÒ½«´´½¨Ò»¸ö´óСǡºÃÓëÓÎÏ·´°Ìå µÄ¹¤×÷ÇøÏàͬµÄÀëÆÁλͼ¡£Õâ¿ÉÒÔËæÐèÒªµÄ²»Í¬¶ø²»Í¬¡£È»¶ø£¬ÔÚÀëÆÁºÍÆÁÉÏ»æͼ½çÏÞÖ®¼ä±£³Ö 1:1 µÄ´óС¹Øϵ½«ÓкܴóºÃ´¦£¬ÓÈÆäÊÇÔÚÆÁÉÏ ºÍÀëÆÁ Ö®¼ä±ä»»×ÓͼÐεÄ×ø±êʱ¡£

// Draw to off-screen graphics, using Graphics.Draw APIs
// Create on-screen graphics
Graphics gxOn = this.CreateGraphics();
// Copy off-screen image onto on-screen
gxOn.DrawImage(bmpOff, 0, 0, this.ClientRectangle, GraphicsUnit.Pixel);
// Destroy on-screen graphics
gxOn.Dispose();

ÕâÒ»¼¼Êõ¿ÉÒÔ±ÜÃâÆÁÄ»ÉÁ˸£¬²¢ÇÒËٶȸü¿ì£¬ÒòΪËùÓÐÀëÆÁ »æͼ²Ù×÷¶¼·¢ÉúÔÚÄÚ´æÖС£

×ÓͼÐÎ

Ïñλͼ ÕâÑùµÄ¹âդͼÐζ¼ÒÔ¾ØÐαíʾ£¬¶ø´ó¶àÊýʵ¼ÊµÄ×ÓͼÐζ¼¾ßÓв»¹æÔòµÄÐÎ×´£¨²»ÊǾØÐΣ©¡£Òò´Ë£¬ÎÒÃÇÐèÒªÕÒµ½ÏàÓ¦µÄ·½·¨£¬ÒÔ±ã´Ó¾ØÐιâդͼÐαíʾÖÐÌáÈ¡ÐÎ×´²»¹æÔòµÄ×ÓͼÐΡ£

ÑÕÉ«¼ü͸Ã÷

ÓÎÏ·¿ª·¢ÈËÔ±³£ÓõÄÒ»ÖÖ¼¼ÊõÊÇÑÕÉ«¼ü ¼¼Êõ£¬¼´ÔÚäÖȾʱºöÂÔλͼÖеÄÖ¸¶¨ÑÕÉ«¼ü¡£ÕâÒ»¼¼ÊõÒ²³ÆΪɫ¶È¼üÆÁ±Î¡¢ÑÕÉ«È¡ÏûºÍ͸Ã÷»ìºÏ¡£

ͼ 3. ×ÓͼÐΡ£

ͼ 4. ´øÓÐÑÕÉ«¼ü͸Ã÷

ͼ 5. ²»´øÑÕÉ«¼ü͸Ã÷

ÔÚ×ÓͼÐÎλͼ£¨Í¼ 3£©ÖУ¬·Ç¶ÔÏó ÇøÓò±»Ìî³äÁËÆ·ºì É«£¬¸ÃÑÕÉ«±»ÓÃ×÷ÑÕÉ«¼ü¡£Ê¹ÓÃÒÔ¼°²»Ê¹Óøü¼ÊõʱµÃµ½µÄ»ìºÏЧ¹û·Ö±ðÔÚͼ 4 ºÍ ͼ 5 ÖнøÐÐÁË˵Ã÷¡£

͸Ã÷»ìºÏµÄµÚÒ»²½ÊÇÉèÖÃÐèÒªÔÚäÖȾʱÆÁ±ÎµÄÑÕÉ«¼ü£¨É«¶È¼ü£©¡£ÎÒÃÇÐèÒªÖ¸¶¨¾«È·µÄÑÕÉ«¼üÖµ£»Ö¸¶¨·¶Î§²»ÊÜÖ§³Ö¡£

ImageAttributes imgattr = new ImageAttributes();
imgattr.SetColorKey(Color.Magenta, Color.Magenta);

ÓëʹÓà Color ÀàÌṩµÄ±ê×¼ÑÕÉ«¼¯²»Í¬£¬Äú¿ÉÒÔͨ¹ýÖ¸¶¨ºìÉ«¡¢ÂÌÉ«ºÍÀ¶É« (RGB) ÖµÀ´Ö±½Ó¹¹½¨×Ô¼ºµÄÑÕÉ«£¬ÈçÏÂËùʾ£º

imgattr.SetColorKey(Color.FromArgb(255, 0, 255),
Color.FromArgb(255, 0, 255));

ÎÒ¾­³£ÓÃÀ´Ö¸¶¨ÑÕÉ«¼üµÄÁíÍâÒ»ÖÖ¼¼ÊõÊÇÖ±½ÓʹÓÃÏñËØÖµ¡£Õâ¿ÉÒÔ±ÜÃâ´¦Àí RGB ÖµµÄÐèÒª¡£¶øÇÒ£¬ÑÕÉ«¼ü ²»ÊÇÓ²±àÂëµÄ£¬¿ÉÒÔÔÚλͼÖжÀÁ¢½øÐиü¸Ä¡£

imgattr.SetColorKey(bmpSprite.GetPixel(0,0), bmpSprite.GetPixel(0,0));

ÏÖÔÚ£¬ÈÃÎÒÃÇ¿´Ò»ÏÂÈçºÎʹÓÃÎÒÃÇÒѾ­ÉèÖõÄÑÕÉ«¼ü À´Í¸Ã÷µØ»æÖÆ×ÓͼÐΡ£

gxOff.DrawImage(bmpSprite, new Rectangle(x, y, bmpSprite.Width,
bmpSprite.Height), 0, 0, bmpSprite.Width, bmpSprite.Height,
GraphicsUnit.Pixel, imgattr);

ÔÚÉÏÊö´úÂëƬ¶ÎÖУ¬Ä¿±ê¾ØÐα»Ö¸¶¨Îª new Rectangle(x, y, bmpSprite.Width, bmpSprite.Height)£¬ÆäÖÐ x ºÍ y ÊÇ×ÓͼÐεÄÔ¤ÆÚ×ø±ê¡£

ͨ³££¬ÔÚ»æÖÆʱ¿ÉÄÜÐèÒªÉìÕ¹»òËõС×ÓͼÐΡ£Äú¿ÉÒÔͨ¹ýµ÷ÕûÄ¿±ê¾ØÐεĿí¶ÈºÍ¸ß¶ÈÀ´×öµ½ÕâÒ»µã¡£Í¬Ñù£¬Äú»¹¿ÉÒÔµ÷ÕûÔ´¾ØÐÎÒÔ±ã½ö»æÖÆ×ÓͼÐεÄÒ»²¿·Ö£¬¾¡¹ÜÕâ¿ÉÄܲ»ÊǺÜÓÐÓá£

ÔÚÉèÖÃ×ÓͼÐÎλͼʱ£¬×îºÃ¿¼ÂÇÒ»ÏÂÄ¿±êÉ豸µÄÑÕÉ«·Ö±æÂÊ¡£ÀýÈ磬24 λÑÕɫλͼÔÚ 12 λÑÕÉ«·Ö±æÂÊÉ豸ÉÏ¿ÉÄܲ»»á°´Ô¤ÆÚ·½Ê½äÖȾ£»¸ù¾ÝËùÓÃÑÕÉ«µÄ²»Í¬£¬ÕâÁ½ÕßÖ®¼äµÄÑÕÉ«ÌݶȲîÒì¿ÉÄܷdz£Ã÷ÏÔ¡£¶øÇÒ£¬ÔÚÑ¡ÔñÒªÆÁ±ÎµÄ ColorKey ʱ£¬ÇëÈ·±£ÑÕÉ«ÖµÔÚÄ¿±êÏÔʾÆ÷ËùÖ§³ÖµÄ·¶Î§Ö®ÄÚ¡£Çë¼Çס£¬Ö»Óо«È·µÄ ColorKey Æ¥Åä²Å»áÊܵ½Ö§³Ö¡£

ÑÕÉ«¼ü ͸Ã÷ÊÇ .NET ¿ò¼ÜѹËõ°æÖÐΨһÊÜÖ§³ÖµÄ»ìºÏ¼¼Êõ¡£

×÷ΪǶÈëʽ×ÊÔ´µÄͼÏñ

Äú¿ÉÒÔ½«Í¼ÐÎ×ÊԴǶÈëµ½ÄúµÄ³ÌÐò¼¯ÖУ¬·½·¨Êǽ«¸ÃͼÐÎÌí¼Óµ½ÏîÄ¿ÖУ¬²¢½«Æä Build Action ÊôÐÔÉèÖÃΪ¡°Embedded Resource¡±¡£ÓйØÏêϸÐÅÏ¢£¬Çë²ÎÔÄÁª»ú°ïÖúÖ÷Ìâ¡°ÔÚÓ¦ÓóÌÐòÖÐǶÈë×ÊÔ´Îļþ¡±¡£

ÕâÑù£¬ÎÒÃǾͿÉÒÔ°´ÈçÏ·½Ê½Ê¹ÓÃǶÈëµÄ bmp ×ÊÔ´£º

Assembly asm = Assembly.GetExecutingAssembly();
Bitmap bmpSprite = new Bitmap(asm.GetManifestResourceStream("Sprite"));

BMP¡¢JPG¡¢GIF ºÍ PNG ͼÐθñʽÊܵ½ Bitmap ÀàµÄÖ§³Ö¡£

ÓÅ»¯»æͼ·½·¨

ÓÎÏ·ÖеĻæͼÀý³ÌÐèÒª½øÐÐÑϸñÓÅ»¯ÒÔ±ãµÃµ½×î¼ÑµÄÐÔÄÜ¡£±È½Ï±¿×¾µÄÆÁÄ»»æͼ·½·¨ÊÇÒÔÀëÆÁ ·½Ê½²Á³ý²¢ÖØ»æËùÓÐ×ÓͼÐΣ¬È»ºóÓÃÒÔÀëÆÁ ·½Ê½µÃµ½µÄͼÐνøÐÐÆÁÉÏ Ë¢Ð¡£ÕâÑù×öЧÂʺܵͣ¬ÒòΪÎÒÃÇÿ´Î¶¼²»±ØÒªµØÖØ»æÕû¸öÆÁÄ»¡£Õâʱ£¬ÎÒÃǵÄÖ¡ËÙÂʽ«È¡¾öÓÚ¿ò¼ÜѹËõ°æË¢ÐÂÕû¸öÆÁÄ»µÄËÙÂÊ¡£

Ò»ÖÖ¸üºÃµÄ°ì·¨ÊǼÆËãÓÎÏ·ÖÐ×ÓͼÐεÄÔàÇø£¬²¢ÇÒֻˢÐÂÆÁÄ»±äÔàµÄ²¿·Ö¡£×ÓͼÐοÉÄÜÒòΪ¶àÖÖÔ­Òò¶ø±äÔ࣬ÀýÈ磬Òƶ¯¡¢Í¼ÐÎ/ÑÕÉ«·¢Éú¸Ä±ä»òÕßÓëÆäËû×ÓͼÐη¢Éú³åÍ»£¬µÈµÈ¡£ÔÚ±¾ÕÂÖУ¬ÎÒÃǽ«ÌÖÂÛ¿ÉÓÃÓÚÓÐЧ¼ÆËãÔàÇøµÄ¸÷ÖÖ¼¼Êõ¡£

ÔàÇø¼ÆËã

ÈÃÎÒÃÇ¿¼ÂÇÒ»¸öÒƶ¯µÄ×ÓͼÐΣ¬Ò»ÖÖ¼ÆËãÔàÇøµÄ¼òµ¥·½·¨ÊÇ»ñÈ¡¾É½çÏÞºÍнçÏ޵IJ¢¼¯¡£

RefreshScreen(Rectangle.Union(sprite.PreviousBounds, sprite.Bounds));

ÆäÖУ¬RefreshScreen() ÊÇÒ»¸öÒÔÆÁÉÏ ·½Ê½Ë¢ÐÂÖ¸¶¨¾ØÐÎÇøÓòµÄ·½·¨¡£

ͼ 6. ÔàÇø£º¾É½çÏÞºÍнçÏ޵IJ¢¼¯

ͼ 7. ¾Þ´óµÄÔöÁ¿²úÉú¾Þ´óµÄÔàÇø

×¢ Èçͼ 7 Ëùʾ£¬Èç¹û¾É×ø±êºÍÐÂ×ø±êÖ®¼äµÄÔöÁ¿ºÜ¸ß²¢ÇÒ/»òÕß×ÓͼÐκܴó£¬ÔòÕâÖÖ¼¼Êõ»á²úÉú¹ý´óµÄÔà¾ØÐΣ¨Îª±ãÓÚ˵Ã÷£¬¾ÉµÄ½çÏÞÓò»Í¬ÑÕÉ«ÏÔʾ£©¡£

ÕâÖÖÇé¿öÏ£¬¸üÓÐЧµÄ·½·¨Êǽ«×ÓͼÐεÄÔàÇø¼ÆËãΪ¶à¸öµ¥Ôª¾ØÐΣ¬ÕâЩ¾ØÐηŵ½Ò»Æðʱ±íʾÔàÇø¡£

Ê×ÏÈ£¬ÈÃÎÒÃÇŪÇå³þ¾ÉµÄ½çÏÞºÍеĽçÏÞÓÐûÓÐÏ໥Öصþ¡£Èç¹ûûÓУ¬Ôò¿ÉÒÔ¼òµ¥µØ½«ÔàÇø¼ÆËãΪÁ½¸öµ¥¶ÀµÄ¾ØÐΣ¬·Ö±ð±íʾ¾ÉµÄ½çÏÞºÍеĽçÏÞ¡£Òò´Ë£¬¶ÔÓÚͼ 7 ˵Ã÷µÄÇéÐΣ¬Ã÷ÖǵÄ×ö·¨Êǽ«¾ÉµÄ½çÏÞºÍеĽçÏÞÊÓΪÁ½¸öµ¥¶ÀµÄÔàÇø¡£

if (Rectangle.Intersection(sprite.PreviousBounds,
sprite.Bounds).IsEmpty)
{
// Dirty rectangle representing old bounds
RefreshScreen(sprite.PreviousBounds);

// Dirty rectangle representing current bounds
RefreshScreen(sprite.Bounds);
}

µ±ÎÒÃDz¢²»½éÒ⽫ÖصþÇøÓòÖØ»æÁ½´Îʱ£¬ÉÏÊö¼¼ÊõÒ²½«ÓÐЧ£¬ÈçÏÂÃæµÄͼ 8 Ëùʾ¡£

ͼ 8. ½«ÔàÇø²ð·ÖΪ¾ÉµÄ½çÏÞºÍеĽçÏÞ

ÏÖÔÚ£¬ÈÃÎÒÃÇ¿´Ò»ÏÂÈçºÎ½«ÔàÇø¼ÆËãΪ¶à¸öµ¥Ôª¾ØÐΣ¨ÕâЩ¾ØÐι²Í¬±íʾ²¿·ÖÖصþµÄ¾É½çÏÞºÍнçÏÞ£©£¬ÒԱ㲻»áÖØ»æÈκÎÔàÇø£¬Ò²¾ÍÊÇ˵ËùÓе¥Ôª¾ØÐζ¼ÊÇ»¥³âµÄ¡£

Ê×ÏÈ£¬°üº¬ÐµĽçÏÞ×÷Ϊһ¸öÍêÕûµ¥Ôª¡£Çë×¢Ò⣬Õâ°üÀ¨¾É½çÏÞºÍнçÏÞÖ®¼äµÄÖصþ ÇøÓò¡£

µ¥ÔªÔàÇø 1£º±íʾµ±Ç°½çÏÞµÄÔà¾ØÐÎ

RefreshScreen(sprite.Bounds);

ͼ 9. ÔàÇø²ð·ÖΪ¶à¸öµ¥Ôª

ÏÖÔÚ£¬½«¾É½çÏ޵ķÇÖصþ ²¿·Ö²ð·ÖΪÁ½¸ö¶ÀÁ¢µÄµ¥Ôª£¬ÈçÏÂÃæµÄ´úÂëËùʾ£º

Rectangle rcIx, rcNew;
// Calculate the overlapping intersection
rcIx = Rectangle.Intersection(sprite.PreviousBounds, sprite.Bounds);

µ¥ÔªÔàÇø 2£º

rcNew = new Rectangle();
if (sprite.PreviousBounds.X < rcIx.X)
{
rcNew.X = sprite.PreviousBounds.X;
rcNew.Width = rcIx.X - sprite.PreviousBounds.X;
rcNew.Y = rcIx.Y;
rcNew.Height = rcIx.Height;
}
else
{
// Means sprite.PreviousBounds.X should equal to rcIx.X
rcNew.X = rcIx.X + rcIx.Width;
rcNew.Width = (sprite.PreviousBounds.X +
sprite.PreviousBounds.Width) - (rcIx.X + rcIx.Width);
rcNew.Y = rcIx.Y;
rcNew.Height = rcIx.Height;
}

RefreshScreen(rcNew);

µ¥ÔªÔàÇø 3£º

rcNew = new Rectangle();

if (sprite.PreviousBounds.Y < rcIx.Y)
{
rcNew.Y = sprite.PreviousBounds.Y;
rcNew.Height = rcIx.Y - sprite.PreviousBounds.Y;
rcNew.X = sprite.PreviousBounds.X;
rcNew.Width = sprite.PreviousBounds.Width;
}
else
{
rcNew.Y = rcIx.Y + rcIx.Height;
rcNew.Height = (sprite.PreviousBounds.Y +
sprite.PreviousBounds.Height) - (rcIx.Y + rcIx.Height);
rcNew.X = sprite.PreviousBounds.X;
rcNew.Width = sprite.PreviousBounds.Width;
}

RefreshScreen(rcNew);

³åÍ»¼ì²â

ÏÖÔÚ£¬ÈÃÎÒÃÇ¿´Ò»ÏÂÒ»¸ö×ÓͼÐÎÓëÁíÒ»¸ö×ÓͼÐγåÍ»µÄÇéÐΡ£´Ó»æͼ½Ç¶ÈÀ´¿´£¬¿ÉÒÔ¼òµ¥µØºöÂÔÁ½¸ö×ÓͼÐÎÖ®¼äµÄ³åÍ»£¬¶øÖ»ÊÇʹÓÃÇ°ÃæÌÖÂ۵ļ¼ÊõÖð¸ö¸üÐÂÕâЩ×ÓͼÐεÄÔàÇø¡£

µ«ÊÇ£¬Äú¾­³£ÐèÒª¼ì²â×ÓͼÐεijåÍ»ÒÔ±ãʹÓÎÏ·×ö³öÏìÓ¦¡£ÀýÈ磬ÔÚÉä»÷ÓÎÏ·ÖУ¬µ±×Óµ¯»÷ÖÐÄ¿±êʱ£¬Äú¿ÉÄÜÏ£Íûͨ¹ý±¬Õ¨»òÀàËÆÐÎʽֱ¹ÛµØ×ö³ö·´Ó¦¡£

ÔÚ±¾ÎÄÖУ¬ÎÒ½«²»»áÏêϸÌÖÂÛ¸÷ÖÖ¿ÉÓõijåÍ»¼ì²â¼¼Êõ¡£µ«ÊÇ£¬ÎÒ½«ÖصãÌÖÂÛÆäÖеļ¸ÖÖ¼¼Êõ¡£

ÎÒÃÇ¿ÉÒÔ»ØÒäһϣ¬´ó¶àÊý×ÓͼÐεÄÐÎ×´¶¼ÊDz»¹æÔòµÄ£¬µ«ÓÃÓÚ±íʾËüÃǵĹâդͼÐÎÊǾØÐΡ£ºÜÄѽ«×ÓͼÐεĽçÏÞ£¨»ò°üÂçÏߣ©±íʾΪ¿ª·ÅµÄÇøÓò£¬Òò´ËÎÒÃǽ«Í¨¹ý·â±ÕµÄ¾ØÐÎÀ´±íʾËü¡£

µ±Íæ¼Ò¿´µ½ÆÁÄ»ÉϵÄ×ÓͼÐÎʱ£¬Ëû/Ëýʵ¼ÊÉÏ¿´µ½µÄÊÇ×ÓͼÐÎÇøÓò£¬¶ø¾õ²ì²»µ½·Ç×ÓͼÐÎÇøÓò¡£Òò´Ë£¬×ÓͼÐÎÖ®¼äµÄÈκγåÍ»¼ì²â¶¼±ØÐë½ö·¢ÉúÔÚÆä¸÷×ÔµÄ×ÓͼÐÎÇøÓòÖ®¼ä£¬¶ø²»Ó¦°üÀ¨·Ç×ÓͼÐÎÇøÓò¡£

¶ÔÓÚ½ÏСµÄ×ÓͼÐΣ¬ÔÚ¼ÆËã³åÍ»²¢Ö±¹ÛµØ±ÜÃâ³åͻʱ£¬Í¨³£¿ÉÒÔʹÓÃÕû¸ö×ÓͼÐνçÏÞ¡£ÒòΪ¶ÔÏó½ÏС²¢ÇÒÒƶ¯Ñ¸ËÙ£¬ÈâÑÛ½«²»»á×¢Òâµ½´í¾õ¡£¼òµ¥µØ¼ÆËãÁ½¸ö×ÓͼÐνçÏ޵ľØÐν»¼¯¾Í×ã¹»ÁË¡£

Rectangle.Intersect(sprite1.Bounds, sprite2.Bounds);

Èç¹û×ÓͼÐεÄÐÎ×´ÊÇÔ²ÐΣ¬Ôò¿ÉÒÔ¼òµ¥µØ¼ÆËãËüÃǵÄÔ²ÐÄÖ®¼äµÄ¾àÀë²¢¼õÈ¥Æä°ë¾¶£»Èç¹û½á¹ûСÓÚÁ㣬Ôò±íÃ÷´æÔÚ³åÍ»¡£

¶ÔÓÚÖð¸öÏñËصÄÅöײ¼ì²â£¬¿ÉÒÔʹÓà Rectangle.Contains ·½·¨£º

if (sprite.Bounds.Contains(x,y)
DoHit();

Ê×ÏÈÓ¦¸ÃÓ¦ÓÿìËٱ߽çÏཻ¼¼ÊõÀ´¼ì²â×ÓͼÐÎÖ®¼äµÄ±ß½ç³åÍ»¡£Èç¹û·¢ÉúÁ˳åÍ»£¬ÔòÎÒÃÇ¿ÉÒÔʹÓÃÒ»ÖÖ¸üΪ¾«È·µÄ·½·¨£¨Èç³åͻλͼÆÁ±Î¼¼Êõ£©À´È·¶¨Ï໥ÖصþµÄÏñËØ¡£

Èç¹ûÎÒÃDz»¹ØÐÄÏñËؼ¶Á£¶È£¬Ôò¿ÉÒÔʹÓÃÎÒÃÇÒѾ­ÔÚÔàÇø¼ÆËãÒ»½ÚÖп´µ½µÄ¼¼ÊõÀ´¼ÆËã³åÍ»ÇøÓò¡£ÎÒÃǽ«´¦ÀíÁ½¸ö·¢Éú³åÍ»µÄ×ÓͼÐεĽçÏÞ£¬¶ø²»ÊÇ´¦ÀíÒ»¸ö×ÓͼÐεľɽçÏÞºÍнçÏÞ¡£

³åÍ»¼ì²â¼¼ÊõÊÇרÓõģ¬Ó¦¸Ã¸ù¾Ý¾ßÌåÇé¿ö¼ÓÒÔÈ·¶¨¡£Ó¦¸Ã¸ù¾Ý¶àÖÖÒòËØÀ´Ñ¡ÔñÌض¨µÄ¼¼Êõ£¬Èç×ÓͼÐεĴóСºÍÐÎ×´¡¢ÓÎÏ·µÄÌØÐԵȡ£ÔÚÒ»¸öÓÎÏ·ÖÐʹÓÃÉÏÊö¼¼ÊõÖеĶà¸ö¼¼ÊõÊǺܳ£¼ûµÄ¡£

×ÓͼÐÎËÙ¶È

Ö¡ËÙÂʾ­³£±»Îó½âΪÒƶ¯×ÓͼÐεÄËٶȡ£ÎÒÃDz»Ó¦¸ÃÖ»ÒÀÀµÓÚÖ¡ËÙÂÊ£¬»¹Ó¦¸Ã¿ØÖÆ×ÓͼÐÎÔÚÿ֡ÖÐÒƶ¯µÄ¾àÀ룬ÒÔ»ñµÃÆÚÍûµÄ¾»Ëٶȡ£

ÈÃÎÒÃÇ¿¼ÂÇÏÂÃæµÄʾÀý£¬ÔÚ¸ÃʾÀýÖУ¬ÎÒÃÇÏ£Íû×ÓͼÐÎÿÃëÖÓ×ÝÏòÒƶ¯ 100 ¸öÏñËص¥Î»¡£ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ½«Ö¡ËÙÂʹ̶¨Îª 10 fps£¬²¢ÇÒ½«×ÓͼÐÎÿ֡×ÝÏòÒƶ¯ 10 ¸öÏñËØ£¬ÒÔ±ã´ïµ½ÉÏÊö¾»Ëٶȣ¬»òÕßÎÒÃÇ»¹¿ÉÒÔ½«Ö¡ËÙÂÊÔö¼Óµ½ 20 fps£¬²¢ÇÒʹ×ÓͼÐεÄÿ֡×ÝÏòÒƶ¯¾àÀëϽµÖÁ 5 ¸öÏñËØ¡£

²ÉÓÃÈÎÒ»ÖÖ·½·¨¶¼¿ÉÒÔ´ïµ½ÏàͬµÄ¾»Ëٶȣ¬Çø±ðÔÚÓÚ£ºÔÚÇ°Ò»ÖÖÇéÐÎÏ£¬×ÓͼÐεÄÒƶ¯¿´ÆðÀ´¿ÉÄÜÓÐÒ»µãÌøÔ¾ÐÔ£¬ÒòΪÓëºóÒ»ÖÖÇéÐÎÏà±È£¬ËüÒƶ¯Ïàͬ¾àÀëËùÓõÄË¢ÐÂÖÜÆÚÒª¶ÌһЩ¡£µ«ÊÇ£¬ÔÚºóÒ»ÖÖÇéÐÎÖУ¬ÎÒÃÇÒÀÀµÓÚÓÎÏ·ÒÔ 20 fps µÄÖ¡ËÙÂÊäÖȾ¡£Òò´Ë£¬ÔÚ¾ö¶¨Ê¹ÓÃÄÄÒ»ÖÖ·½·¨Ö®Ç°£¬ÎÒÃÇÐèÒª¾ø¶ÔÈ·¶¨Ó²¼þ¡¢ÏµÍ³ºÍ .NET ¿ò¼ÜѹËõ°æµÄ¹¦ÄÜ¡£

ÓÎÏ·Ç°½ø¼¼Êõ

µ±ÆÁÄ»Ëæ×Åʱ¼äµÄÍÆÒƶø·¢Éú±ä»¯²¢ÇÒÖ±¹ÛµØÏìÓ¦Óû§½»»¥Ê±£¬¾Í˵ÓÎÏ·ÕýÔÚÇ°½ø¡£

ÓÎÏ·Ñ­»·

ÔÚÓÎÏ·ÄÚ²¿£¬ÎÒÃÇ¿ªÊ¼¡¢Î¬»¤ºÍÆÆ»µÑ­»·£¬ÕâʹÎÒÃÇÔÚ±ØҪʱÓлú»áäÖȾÆÁÄ»¡£Í¨³££¬µ±ÓÎÏ·Æô¶¯Ê±£¬ÓÎÏ·Ñ­»·¿ªÊ¼£¬È»ºó¸ù¾ÝÐèÒªÐÝÃߺÍÑ­»··µ»ØÀ´½øÐÐά»¤£¬Ö±µ½ÓÎÏ·½áÊøΪֹ ¡ª ´ËʱÓÎÏ·Ñ­»·½«±»µ¯³ö¡£

ÕâÖÖ¼¼Êõ¿ÉÒÔÌṩ¶¯×÷ÓÎÏ·ËùÐèµÄ×î´óµÄÁé»îÐԺͿìËÙµÄÖÜתËٶȡ£ÏÖÔÚ£¬ÎÒÃǽ«ÎªÎÒÃǵÄ×ãÇòÓÎϷʵÏÖÒ»¸öÓÎÏ·Ñ­»·¡£ÈÃÎÒÃǼÙÉè¸ÃÓÎÏ·¾ßÓжà¸ö¼¶±ð¡£

private void DoGameLoop()
{
// Create and hold onto on-screen graphics object
// for the life time of the loop

m_gxOn = this.CreateGraphics();

do
{
// Init game parameters such as level

DoLevel();

// Update game parameters such as level

// Ready the game for the next level
}
while (alive);

// End game loop

// Dispose the on-screen graphics as we don't need it anymore

m_gxOn.Dispose();

// Ready the game for next time around
}

private void DoLevel()
{
int tickLast = Environment.TickCount;
int fps = 8;

while ((alive) && (levelNotCompleted))
{
// Flush out any unprocessed events from the queue

Application.DoEvents();

// Process game parameters and render game

// Regulate the rate of rendering (fps)
// by sleeping appropriately
Thread.Sleep(Math.Abs(tickLast + (1000/fps) a€¡°
Environment.TickCount));
tickLast = Environment.TickCount;
}
}

×¢Ò⣬ÔÚÑ­»··µ»Ø֮ǰÎÒÃÇÿ´Î¶¼ÒªÔÚÑ­»·ÄÚ²¿µ÷Óà Application.DoEvents()£¬ÎÒÃÇÕâÑù×öµÄÄ¿µÄÊDZ£³ÖÓëϵͳ֮¼äµÄ»î¶¯Í¨Ñ¶£¬ÒÔ¼°±ãÓÚ¶Ôʼþ¶ÓÁÐÖйÒÆðµÄÏûÏ¢½øÐд¦Àí¡£ÕâÊÇÓбØÒªµÄ£¬ÒòΪµ±ÎÒÃǵÄÓ¦ÓóÌÐò´¦ÓÚÑ­»·ÖÐʱ£¬ÎÒÃÇ»ù±¾ÉÏʧȥÁË´¦ÀíÈκÎÀ´×ÔϵͳµÄ´«ÈëÏûÏ¢µÄÄÜÁ¦£»³ý·ÇÎÒÃÇÃ÷È·µ÷Óà Application.DoEvents()£¬·ñÔòÎÒÃǵÄÓ¦ÓóÌÐò½«²»»áÏìӦϵͳʼþ£¬²¢Òò´Ë¾ßÓв»ºÏÐèÒªµÄ¸±×÷Óá£

ÔÚÓÎÏ·Ñ­»·ÖÐÐèÒª¿¼ÂǵÄÁíÒ»¸öÖØÒªÒòËØÊÇäÖȾËÙÂÊ¡£´ó¶àÊýÓÎÏ·¶¯»­ÖÁÉÙÐèҪÿÃëÖÓ 8-10 Ö¡ (fps) µÄËÙÂÊ¡£ÎªÁËʹÄúÓÐÒ»¸ö´óÖµĸÅÄÒÔµäÐ͵Ŀ¨Í¨Ó°Æ¬ÎªÀý£¬ËüµÄäÖȾËÙÂÊÊÇÿÃëÖÓ 14-30 Ö¡¡£

Ò»ÖÖ¼òµ¥µÄÖ¡ËÙÂÊ¿ØÖƼ¼ÊõÊǾö¶¨ËùÐèµÄ fps ÒÔ¼°Ñ­»·ÄÚ²¿µÄÐÝÃß (1000/fps) ºÁÃë¡£µ«ÊÇ£¬ÎÒÃÇ»¹ÐèÒª½«´¦Àíµ±Ç°×ßʱËùÐèµÄʱ¼ä¿¼ÂÇÔÚÄÚ¡£·ñÔò£¬ÎÒÃǵÄäÖȾËÙÂʽ«±ÈÆÚÍûµÄËÙÂÊÂý¡£´¦Àíʱ¼ä¿ÉÄÜÏ൱¿É¹Û£¬¶ÔÓÚËٶȽÏÂýµÄÓ²¼þÓÈÆäÈç´Ë£¬ÒòΪÕâÉæ¼°µ½¿ªÏú½Ï¸ßµÄ²Ù×÷£¬Èç´¦ÀíÓû§ÊäÈë¡¢äÖȾÓÎÏ·µÈµÈ¡£

Òò´Ë£¬ÔÚ¼ÌÐøÑ­»·Ö®Ç°£¬ÎÒÃÇÐèÒªÐÝÃß 1000/fps ¼õÈ¥´¦Àíµ±Ç°×ßʱËùÐèµÄʱ¼ä£¨ºÁÃ룩¡£

Thread.Sleep(Math.Abs(tickLast + (1000 / fps) - Environment.TickCount));
tickLast = Environment.TickCount;

¼ÆʱÆ÷»Øµ÷

ÁíÒ»Ïî¼¼ÊõÊÇÉèÖÃÒ»¸ö¶¨Æڻص÷µÄϵͳ¼ÆʱÆ÷¡£¶ÔÓÚÓÎÏ·Ñ­»·¶øÑÔ£¬Í¨³£Çé¿öÏ£¬ÔÚÓÎÏ·Æô¶¯Ê±ÊµÀý»¯¼ÆʱÆ÷£¬ÔÚÓÎÏ·½áÊø£¨´Ëʱ¼ÆʱÆ÷±»´¦Öã©Ö®Ç°¶Ô¼ÆʱÆ÷¼Æʱʼþ£¨¶¨ÆÚ·¢Éú£©½øÐд¦Àí¡£

ÕâÒª±ÈÓÎÏ·Ñ­»·¼òµ¥£¬ÒòΪÎÒÃÇÈÃϵͳΪÎÒÃÇ´¦Àí¼ÆʱÆ÷Ñ­»·¡£ÎÒÃÇÖ»ÐèÒª´¦Àí¼ÆʱÆ÷»Øµ÷£¬²¢ÇÒͨ¹ýÒ»´Î»æÖÆÒ»¸öÖ¡À´Ê¹ÓÎÏ·Ç°½ø¡£¶øÇÒ£¬ÎÒÃÇÎÞÐëµ£ÐÄÏÔʽÏûºÄʼþ¶ÓÁеÄÎÊÌâ¡£

µ«ÊÇ£¬ÎÒÃDZØÐë·Ç³£Ð¡ÐĵØÑ¡Ôñ¼ÆʱÆ÷µÄ×ßʱ¼ä¸ô£¬ÒòΪËü¾ö¶¨ÁËÓÎÏ·µÄÖ¡ËÙÂÊ¡£

ÔÚÓÎÏ·Ñ­»·¼¼ÊõÖУ¬Á½´Î×ßʱ֮¼äµÄʱ¼ä¼ä¸ôÍêÈ«ÔÚÎÒÃǵĿØÖÆ֮ϣ¬²¢ÇÒÕýÈçÇ°ÃæËù¿´µ½µÄ£¬¿ÉÒÔÇáËɵضÔÆä½øÐпØÖÆÒԱ㽫´¦Àíʱ¼ä¿¼ÂÇÔÚÄÚ¡£ÁíÒ»·½Ã棬¼ÆʱÆ÷»Øµ÷ÒâζןÃʱ¼ä¼ä¸ô²»Äܱ仯¡£Òò´Ë£¬ÎÒÃÇÐèҪѡÔñ×ã¹»´óµÄ×ßʱ¼ä¸ôÒÔ±ãÍê³Éÿ¸ö»Øµ÷µÄ´¦Àí£¬»òÕßͨ¹ýÏÔʽ¸ú×Ù´¦ÀíÒ»´Î×ßʱËùÐèµÄʱ¼äÀ´¿ØÖÆ×ßʱµÄ´¦Àí£¬²¢ÇÒÔÚ±ØҪʱÌø¹ý×ßʱÒÔ±£³Ö½Ú×à¡£

¼ÆʱÆ÷»Øµ÷µÄÒ»¸öÖ÷ҪȱÏÝÊÇÎÒÃÇÐèÒªÒÀÀµÓÚ²Ù×÷ϵͳ¼ÆʱÆ÷µÄ·Ö±æÂÊ¡£¿ÉÄÜ´æÔÚµÄ×îµÍ×ßʱ¼ä¸ôÓɸüÆʱÆ÷¿ÉÄܾßÓеÄ×î¸ß·Ö±æÂʾö¶¨¡£ÔÚ¼ÆʱÆ÷·Ö±æÂÊ½ÏµÍµÄ Pocket PC ÉÏ£¬Õâ¿ÉÄܳÉΪһ¸öÏÞÖÆÒòËØ£¬´Ó¶øÒâζ×ÅÕâÖÖ·½·¨ÖпÉÄܾßÓÐµÄ fps Ò²»á±È½ÏµÍ¡£ÁíÍ⣬²Ù×÷ϵͳ¼ÆʱÆ÷ʼþµÄÓÅÏȼ¶·Ç³£µÍ£¬ÕâÒâζ×ÅÓÎÏ·µÄÏìÓ¦ËÙ¶ÈÒ²±È½ÏµÍ¡£

ËäÈ»ÓÐÕâЩ¾ÖÏÞ£¬µ«¶ÔÓÚÇ°½øËٶȽϵ͵ÄÓÎÏ·£¨´Ëʱ֡ËÙÂʲ»Ì«ÖØÒª£©¶øÑÔ£¬ÕâÒ»¼¼Êõ·Ç³£Êʺϡ£ÀýÈ磬ÆÁÄ»±£»¤³ÌÐò

private void StartTimer ()
{
int fps = 8;

// Create and hold onto on-screen graphics object
// for the life of the game/timer
m_gxOn = this.CreateGraphics();

// Setup timer callback to happen every (1000/fps) milliseconds
m_tmr = new System.Windows.Forms.Timer();
m_tmr.Interval = 1000/fps;

// Specify the timer callback method
m_tmr.Tick += new EventHandler(this.OnTimerTick);

// Start the timer
m_tmr.Enabled = true;

// Init game params such as level
}

protected void OnTick(object sender, EventArgs e)
{
if (alive)
{
// Regulate tick to include processing time,
// skip tick(s) if necessary

if (processTick)
{
// Process game parameters and render game
if (levelCompleted)
{
// Update game params such as level
}
}
}
else
EndTimer ();
}

private void EndTimer ()
{
// End game

// Dispose timer
m_tmr.Dispose();

// Dispose the on-screen graphics as we don't need it anymore

m_gxOn.Dispose();
m_gxOn= null; // Make sure the garbage collector gets it

// Ready the game for next time around
}

×¢£ºÓÎÏ·½áÊøʱ±ØÐë´¦ÖüÆʱÆ÷¡£

Çë×¢Ò⣬ûÓбØҪʹÓà Application.DoEvents()£¬ÒòΪÎÒÃǼÈδѭ»·Ò²Î´×èÈûÈκÎϵͳʼþ£¬¶ø¼ÆʱÆ÷»Øµ÷ʵ¼ÊÉÏÖ»ÊÇÒ»¸öϵͳʼþ¡£Í¬Ê±£¬Çë×¢Òâ´ó¶àÊýÓÎÏ·Âß¼­±»Ñ¹Èëµ½ OnTimerTick() ʼþ´¦Àí³ÌÐòÖС£

ÎÞЧ-¸üÐÂ

ÁíÒ»ÖÖʹÓÎÏ·Ç°½øµÄ·½Ê½ÊÇÔÚÌرð µÄ»ù´¡ÉÏäÖȾËü¡£Ã¿µ±ÓÎÏ·Âß¼­¼ì²âµ½ÆÁÄ»ÐèҪˢÐÂʱ£¬ÎÒÃÇ¿ÉÒÔÇëÇóϵͳʹÆÁÄ»µÄÏàÓ¦²¿·ÖÎÞЧ²¢¶ÔÆä½øÐÐˢС£

ÕâÒ»¼¼ÊõÊÇ×î¼òµ¥µÄ£¬²¢ÇÒ×îÊʺϻùÓÚÓû§½»»¥Ç°½øµÄÓÎÏ·¡£ÕâÊÇÖ¸ÄÇЩ²»ÊdzÖÐø²»¶ÏµØ×ßʱºÍäÖȾ£¨ÈçÓÎÏ·Ñ­»·¡¢¼ÆʱÆ÷»Øµ÷ÖÐÄÇÑù£©µÄÓÎÏ·£¬¶øÊǽöµ±Óû§ÓëÆä½»»¥Ê±²ÅÇ°½øµÄÓÎÏ·£¬ÀýÈçÖÇÁ¦²âÑéÓÎÏ·¡£

ÎÒÃÇ¿ÉÒÔͨ¹ýµ÷Óà this.Invalidate() ʹÓÎÏ·´°Ìå µÄÕû¸ö¹¤×÷ÇøÎÞЧ£¬»òÕßͨ¹ýµ÷Óà this.Invalidate(dirtyRect) ½öʹÆäÒ»²¿·ÖÎÞЧ¡£

Ö»µ÷Óà this.Invalidate() ×Ô¼º²»Äܱ£Ö¤»æͼ²Ù×÷»á¼°Ê±·¢Éú¡£ÎÒÃDZØÐëͨ¹ýµ÷Óà this.Update() À´È·±£ÆÁÄ»ÔÚ¼ÌÐøÇ°½ø֮ǰ±»Ë¢Ð¡£ÔÚijЩÇé¿öÏ£¬Òì²½µ÷Óà Invalidate() ºÍ Update() ¿ÉÄÜÓÐÖúÓÚ»ñµÃ¸ü¸ßµÄÐÔÄÜ¡£µ«ÊÇÈç¹û²»Ê¹ÓÃÊʵ±µÄ֡ͬ²½¼¼Êõ£¬¿ÉÄܵ¼ÖÂÆÁÄ»ÉϳöÏÖ²»×ÔÈ»µÄ»­Ã棬²¢ÇÒ/»òÕßÖ¡±»¶ªÆú¡£

Èç¹ûÎÒÃÇÄܹ»³Ðµ£µÃÆðÿ´Î¶¼Ë¢ÐÂÕû¸öÆÁÄ»µÄ¿ªÏú£¬Ôò¿ÉÒÔ¼òµ¥µØµ÷Óà this.Refresh()£¬Ëü¿ÉÒÔÈ·±£ Invalidate() ºÍ Update() ÒÀ´Î·¢Éú¡£

µ±ÎÒÃÇ×÷ΪËùÓÐÕß»æÖÆ´°Ìåʱ£¬ÎÒÃÇ¿ÉÒÔÔÚÆÁÄ»µÄij¸ö²¿·ÖÐèҪˢÐÂʱµ÷Óà this.Refresh()£¬²¢ÇÒÔÚÄÚ²¿¸ú×ÙÆÁÄ»µÄÔàÇø£¬ÒÔ¼°ÔÚ OnPaint() ºÍ OnPaintBackground() ÄÚ²¿ÓÐÑ¡ÔñµØË¢ÐÂÆÁÄ»¡£

ÓÅ»¯Æô¶¯Ê±¼ä

ÔÚÓÎÏ·ÖУ¬¿ª·¢ÈËԱͨ³£»áÔ¤Ïȳõʼ»¯ËùÓÐÓÎÏ·²ÎÊý£¬´Ó¶øÔÚÓÎÏ·½øÐйý³ÌÖбÜÃâ²»±ØÒªµÄÔËÐÐʱÑÓ³Ù¡£ÕâÒ»·½·¨µÄȱÏÝÔÚÓÚÑÓ³Ù±»×ªÒƵ½ÓÎÏ·Æô¶¯¹ý³ÌÖУ¬Èç¹ûÓÎÏ·»¨·ÑÌ«³¤µÄʱ¼ä¼ÓÔØ£¬È»ºóÓû§²ÅÄÜÓëÆä½»»¥£¬ÔòÓÈÆä»áÁîÈ˸е½²»¿ì¡£

ÕâÖÖÇé¿öÏ£¬¿ÉÈ¡µÄ×ö·¨ÊǾ¡¿ÉÄÜ¿ìµØÏÔʾһ¸öº¬ÓÐÓëÓÎÏ·Ïà¹ØÐÅÏ¢µÄÆô¶¯»­Ã棬ÒÔ±ãÎüÒýÓû§¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔÔÚºǫִ́ÐÐÆô¶¯»î¶¯£¬Èç¼ÓÔØ×ÊÔ´¡¢³õʼ»¯ÓÎÏ·²ÎÊýµÈµÈ¡£

ÎÒÃÇ¿ÉÒÔʹÓõ¥¶ÀµÄÈ«ÆÁ ´°Ìå×÷ΪÆô¶¯»­Ã棬Ҳ¿ÉÒÔʹÓýöº¬Óлù±¾ÓÎÏ·ÐÅÏ¢µÄÖ÷ÓÎÏ· ´°Ìå±¾Éí¡£

public Game()
{
// Set visibility first
this.Visible = true;

// Create on-screen graphics
Graphics gxOn = this.CreateGraphics();

// Display Splash screen
DoSplashScreen(gxOn);

// Destroy on-screen graphics
gxOn.Dispose();

// Proceed with your Game Screen
}

void DoSplashScreen(Graphics gxPhys)
{
// Load minimal resources such as title bitmap

Assembly asm = Assembly.GetExecutingAssembly();
Bitmap bmpTitle =
new Bitmap(asm.GetManifestResourceStream("title"));

// Draw the title screen a€_ this is your splash screen
gxPhys.DrawImage(bmpTitle, 0, 0);

// Now proceed with loading rest of the resources
// and initializing the game

// Regulate the splash time if necessary
}

ÖØÒªµÄÊDz»ÒªÔÚÆô¶¯»­ÃæÖÐÌṩÈκι¦ÄÜ£¬¶øÖ»Ó¦¸Ã½«ÆäÓÃ×÷¼ò½é/ÐÅÏ¢Ò³¡£²¢²»×ÜÊÇÐèҪͨ¹ýÆô¶¯»­ÃæÀ´Æô¶¯¡£

ÓÎÏ·°´Å¥

µ¼º½¼ü

ÔÚ Pocket PC ÖУ¬µ¼º½¼ü£¨¼´Ïò×ó¼ü¡¢ÏòÓÒ¼ü¡¢ÏòÉϼüºÍÏòϼü£©ÔÚÓÎÏ·Öз¢»Ó×ÅÖÁ¹ØÖØÒªµÄ×÷Óá£ÎÒÃÇ¿ÉÒÔͨ¹ýÖØдÓÎÏ·´°ÌåµÄ¸÷¸öʼþ·½·¨£¬·ÃÎÊÕâЩ¼üµÄ KeyDown¡¢KeyPress ºÍ KeyUp ʼþ¡£

ͨ³££¬ÎÒÃÇÐèÒª´¦ÀíÕâЩµ¼º½¼üµÄ KeyDown ʼþ£¬²¢ÌṩÓÎÏ·¼¶¹¦ÄÜ¡£

protected override void OnKeyDown(KeyEventArgs keyg)
{
switch(keyg.KeyData)
{
case Keys.Left:
// Provide game functionality for Left key
break;

case Keys.Right:
// Provide game functionality for Right key
break;

case Keys.Up:
// Provide game functionality for Up key
break;

case Keys.Down:
// Provide game functionality for Down key
break;

default:
// We don't care
break;
}

// Always call the base implementation
// so that the registered delegates for this event are raised.
base.OnKeyDown(keyg);
}

Pocket PC ±ÊÕë

Pocket PC µÄ±ÊÕëÀàËÆÓŲ́ʽµçÄÔµÄÊó±ê¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÖØдÓÎÏ·´°ÌåµÄ¸÷¸öʼþ·½·¨À´·ÃÎÊ MouseDown¡¢MouseMove ºÍ MouseUp ʼþ¡£

protected override void OnMouseDown(MouseEventArgs mouseg)
{
Point ptHit = new Point(mouseg.X, mouseg.Y));

}

ÔÚ Pocket PC ÉÏ£¬µ½ 1.0 °æΪֹ£¬.NET ¿ò¼ÜѹËõ°æ²»Ö§³ÖÊó±êÓÒ¼üºÍÓ²¼þ°´Å¥¡£

ÆäËû¼¼Êõ

ÈçÓпÉÄÜ£¬Ó¦»æÖÆͼÐΣ¬¶ø²»ÊÇʹÓÃλͼ¡£Õ⽫¼õСÄÚ´æ´óС£¬²¢ÇÒ»¹¿ÉÒÔÌá¸ßÐÔÄÜ¡£ÀýÈ磬ÔÚÌ«¿ÕÉä»÷ÓÎÏ·ÖУ¬×îºÃ²»ÒªÊ¹Óùö¶¯µÄλͼ×÷Ϊ±³¾°£¬¿ÉÒÔͨ¹ýÓúÚÉ«µÄ¾ØÐÎÌî³ä±³¾°È»ºó»æÖÆÐÇÐÇÀ´»ñµÃÏàͬµÄЧ¹û¡£

³¢ÊÔ½«ÀàËƵÄλͼÂß¼­µØ×éºÏΪһ¸ö´óÐÍλͼ£¬ÒÔºó¸ù¾ÝÐèҪʹÓÃÏà¹Ø×ø±êÌáÈ¡Êʵ±µÄµ¥ÔªÎ»Í¼¡£ÓµÓÐÒ»¸ö´óλͼ¶ø²»ÊǶà¸öСλͼ¿ÉÒÔ½µµÍ×ÊÔ´´óС¡£

¾¡¿ÉÄܳ¢ÊÔʹÓÃͼÐθñʽ¶ø²»ÊÇ BMP£¬ÒÔ±ãÀûÓøüºÃµÄͼÐÎѹËõ¼¼Êõ£¨ÀýÈç JPEG£©¡£

±ÜÃâÔÚËùÓÐÕß»æÖƵÄÓÎÏ·´°Ìå ÉÏʹÓÿؼþ¡£ÄúÓ¦¸Ã×÷ΪËùÓÐÕß»æÖÆËùÓÐÄÚÈÝ¡£ÀýÈ磬Èç¹ûÄúÐèÒªÒ»¸ö Label£¬Ó¦¸ÃʹÓà Graphics.DrawString() ¶ø²»ÊÇ´´½¨×Ô¶¨ÒåµÄ×ÓͼÐΡ£

ÒÔ¾²Ì¬·½Ê½¾¡¿ÉÄܶàµØ³õʼ»¯ÓÎÏ·Âß¼­£¬´Ó¶øÔÚÔËÐÐʱ±ÜÃâÖ´ÐпªÏú½Ï´óµÄ¼ÆËã¡£ÀýÈ磬ÔÚÖÇÁ¦²âÑéÓÎÏ·ÖУ¬Ó¦¸Ã¾¡¿ÉÄܵØÔ¤ÏȾ²Ì¬´æ´¢»ñʤ×éºÏ£¬¶ø²»ÊÇÔÚÓÎÏ·ÔËÐйý³ÌÖÐʹÓÿªÏú½Ï´óµÄ¶¯Ì¬Ëã·¨ÒÔ¼°ÀàËƵŦÄÜ¡£

С½á

ÔÚΪÖîÈç Pocket PC ÕâÑùµÄÉ豸±àдÓÎϷʱ£¬ÐèÒª¼ÇסÏÔʾÆÁÄ»³ß´çÒª±È×ÀÃæ¼ÆËã»úСµÃ¶à£¬²¢ÇÒÓ²¼þµÄ¹¦ÄÜҲûÓÐ×ÀÃæ¼ÆËã»úÄÇÑùÇ¿´ó¡£

Òò´Ë£¬¶ÔÓÚÕâЩСÐÍÉ豸£¬Ó¦¸Ã±È×ÀÃæ¼ÆËã»ú¸ü¼ÓÑϸñµØÓÅ»¯ÓÎÏ·¡£Í¬Ê±ÔÚÉè¼ÆÓÎϷʱ£¬»¹Ó¦¸ÃÈÏÕ濼ÂÇÄ¿±êÓ²¼þ¡¢²Ù×÷ϵͳºÍ .NET ¿ò¼ÜѹËõ°æµÄ¹¦ÄÜ¡£

ÓÎÏ·µÄÐÔÄܸߵÍÖ÷Ҫȡ¾öÓÚËüµÄ»æͼÀý³Ì¡£¸ßЧµÄ»æͼ¼¼Êõ¾ö¶¨ÁËÓÎÏ·µÄÏìÓ¦Ëٶȣ¬ÓÈÆäÊÇÔÚÖîÈç Pocket PC ÕâÑùµÄСÐÍÉ豸ÖС£Òò´Ë£¬Ó¦¸ÃʹÓÃÇ°ÃæÌÖÂÛµÄËùÓлæͼÓÅ»¯¼¼Êõ£¨ÈçÔàÇø¼ÆË㣩£¬ÒÔ±ã½Úʡÿ֡µÄ»æͼʱ¼ä¡£

Ö¡ËÙÂÊÊÇÁíÒ»¸öÐèÒª¼ÇסµÄÖØÒªÒòËØ£¬Çë»ùÓÚÄ¿±êÉ豸µÄ¹¦ÄÜÃ÷ÖǵؼÓÒÔÑ¡Ôñ¡£


°æȨËùÓУºUMLÈí¼þ¹¤³Ì×éÖ¯