¤p¤ì°¸¦b³o¤@³¹ùØ¡A§Q¥Î Windows ´£¨Ñªº GDI ªA°È°Æµ{¦¡¡A²Ê²L¦a¤¶²Ð°Êµeªº§Þ¥©¡A¨Ã¥B¥Ü½d¤@Óµ{¦¡¡ABALL.ASM¡A³oÓµ{¦¡µøµ¡ùئ³¤@Áû²y¤£°±¦a°µµ¥³t«×¹B°Ê¡Aª½¨ì¸IIJ¨ìµøµ¡Ãä®Ø®É¡A·|¥H¼u©Ê¸I¼²ªº¤è¦¡¤Ï¼u¡C¦¹¥~¡A³oÓµøµ¡¨S¦³¿ï³æ¡B©ñ¤j²°¡BÁY¤p²°¡A°£«D«·s²ÕĶ¡A¨Ï¥ÎªÌ¤]µLªk§ïÅܵøµ¡¤j¤p¡A¨ä°õ¦æµe±¦p¤U¡G
¤p¤ì°¸§â ball.asm ¥H¤Î©Ò»Ý¦ì¤¸¹Ï¡B¸ê·½´yzÀɳ£À£ÁY¦¨¤@ÓÀɮסAball.rar¡AÁ¿¥¦¸Ñ¶}¦Ü¦P¤@¤l¥Ø¿ý¡AµM«á¶}±Ò¡y©R¥O´£¥Ü¦r¤¸¡z¡A¤Á´«¨ì¸ÑÀ£ÁYªº¤l¥Ø¿ý¡A¿é¤J¤U±«ü¥O´N¥i¥H±o¨ì ball.exe ¥i°õ¦æÀÉ¡C
E:\HomePage\SOURCE>cd ball [Enter] E:\HomePage\SOURCE\BALL>rc ball.rc [Enter] E:\HomePage\SOURCE\BALL>ml ball.asm /link ball.res [Enter] Microsoft (R) Macro Assembler Version 6.14.8444 Copyright (C) Microsoft Corp 1981-1997. All rights reserved. Assembling: ball.asm Microsoft (R) Incremental Linker Version 5.12.8078 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. /SUBSYSTEM:WINDOWS "ball.obj" "/OUT:ball.exe" "ball.res" E:\HomePage\SOURCE\BALL>
¦b«e¤@³¹ùØ¡A¤p¤ì°¸¤]®i¥Ü¤F°Êµe¡A®É°w¡B¤À°w¡B¬í°wªº¨«°Ê¡A·íµM¤]¬O°Êµe¡C¤£¹L¦b¨ºÓ¨Ò¤lùØ¡A©Ò²¾°Êªºª«Åé«Ü¤p¡A©Ò¥H¤p¤ì°¸¬OÅý CLOCK µøµ¡¨C¦¸¦¬¨ì WM_PAINT °T®§®É¡A§âI´º¹Ï¿ï¤J¨ì³oÓ°O¾ÐÅé³]³Æ¤º®e¡A¦A§Q¥Î BitBlt API §â³oÓ°O¾ÐÅé³]³Æ¤º®eªºI´º¹Ï«þ¨©¨ì¤u§@°Ï¤W±¡A³Ì«á¦Aµe¤W®É°w¡B¤À°w¡B¬í°w¡C¦]¬°¦b¤u§@°Ïªº³]³Æ¤º®e¤W°µ¤F¦n´XӰʧ@¡G§â°O¾ÐÅé³]³Æ¤º®eªºI´º¹Ï«þ¨©¨ì¤u§@°Ï¡B¦b¤u§@°Ïµe¥X®É°w¡B¤À°w¡B¬í°wµ¥¥|Ӱʧ@¡A©Ò¥H¦bºC³tªº¹q¸£¤W¡A©Î³\·|¬Ý¨ì°{Ã{ªº²{¶H¡A¦ý¦]²¾°Êª«Åé¤p©Î¤u§@°Ï¤p¡A³oºØ²{¶H¨Ã¤£©úÅã¡C°²¦pn³]p¹q°Êª±¨ã¡A´N·|»Ýn¤jªº¤u§@°Ï¡A¤]»Ýn²¾°Ê¤jªºª«Åé¡A¨º»ò¥H³oºØ¤è¦¡©Ò°µ¥X¨Óªºµ{¦¡¡A°{Ã{ªº°ÝÃD´N·|«ÜÄY«¤F¡C
n¸Ñ¨M³oÓ°ÝÃD¡A¥i¥H¥Î CreateCompatibleDC ¥ý·Ç³Æ¦n¤@Ó½w½Äªº³]³Æ¤º®e¡A³oÓ½w½Ä°Ï³]³Æ¤º®e¬O«Ø¥ß¦b°O¾ÐÅéùØ¡A¨Ã¤£ª½±µ¿é¥X©ó¿Ã¹õ¤Wªº¤u§@°Ï¡C§Ú̧â©Ò¦³ªºI´º¡B°Êµe¾Þ§@³£¥ý¦b³oÓ½w½Ä°Ïªº³]³Æ¤º®eùئw±Æ¦n¡AµM«á§â½w½Ä°Ïªº³]³Æ¤º®eùتº¹Ï®×¤@¦¸«þ¨©¨ì¤u§@°Ïªº³]³Æ¤º®e¤W¡A¤§«á´N¤£¦A¹ï¤u§@°Ïªº³]³Æ¤º®e¶i¦æø»s¹Ï¤ù¡C³o¼Ë¦b¨Ï¥ÎªÌªº²´¤¤¬Ý¨Ó¡A¤u§@°Ï¥u¦³¤@¦¸ªºÅܤơA´N¤£·|¦³°{Ã{ªº±¡ªp¤F¡C
BALL ªºI´º¬O¥Î¤@Ó¸û¤pªº¦ì¤¸¹Ï¡A¥HÃþ¦ü²¡¿j¤è¦¡«÷´ê¦Ó¦¨¤@Ó¸û¤jªºI´º¹Ï®×¡A³oºØ¸û¤pªº¦ì¤¸¹Ï¤]¥i¥HºÙ¬°¹Ï¼Ë ( pattern )¡C¨Æ¹ê¤W³\¦hºô¶ªºI´º¹Ï¡A¬Æ¦Ü¦³®ÉÔ Windows ªº®à±¦³®É¤]¬O¥H³oºØ¤è¦¡«÷´êªº¡A³oºØ¤è¦¡ªº¦n³B¬O©Ò»ÝI´º¹Ï¤£»Ýn«Ü¤j¡A´N¥i¥HÅý¥¦¶ñ¥R¾ãÓµe±¡C·íµM°Õ¡I³oºØI´º¹Ï¬O·|¤@ª½«½Æªº¡A¤ñ¸û³æ½Õ¬O¨ä¯ÊÂI¡CBALL µ{¦¡¬O§Q¥Î¤U¹Ï¥ªÃ䪺 pattern.bmp ·í°µ¹Ï¼Ë¡A«÷´ê¦¨I´º¹Ï¡Cpattern.bmp ¬Oªø¼e¦U 300 ¹Ï¯À ( pixel ) ªº¦ì¤¸¹Ï¡A¦Ó BALL ¤u§@°Ï¬Oªø 300 ¹Ï¯À¡B¼e 600 ¹Ï¯À¡A©Ò¥H«½Æ¤@¦¸¡A½Ð°Ñ¦Ò¤U¹Ï¤Î¤W¹Ï¡G
BOOL PatBlt( HDC hdc, // handle to device context int nXLeft, // x-coord. of upper-left corner of rect. to be filled int nYLeft, // y-coord. of upper-left corner of rect. to be filled int nWidth, // width of rectangle to be filled int nHeight,// height of rectangle to be filled DWORD dwRop // raster operation code );
PatBlt ·|§â hdc ¥Ø«eªºµ§¨ê§@¬°¹Ï¼Ë¡A±q®y¼Ð ( nXLeft¡AnYLeft ) ¶}©l¶ñ¥R¡A¶ñ¥R°Ï°ìªº¼e«×»P°ª«×¤À§O¬O nWidth¡BnHeight¡C¶ñ¥R¤è¦¡¥i¥H¥Ñ dwRop «ü©w¡AdwRop ¥u¥i¥H¬O¤U±¤ºØ±¡§Î¡G
dwRop | ·N¡@¡@¸q |
PATCOPY | ¶ñ¥Rµ§¨ê |
PATINVERT | µ§¨ê XOR ¥Ø¦a |
DSTINVERT | NOT ¥Øªº |
BLACKNESS | ¥þ³¡¶ñ¥R¶Â¦â |
WHITENESS | ¥þ³¡¶ñ¥R¥Õ¦â |
¥Ñ¦¹¥iª¾¡A°²¦pn¥Î¦ì¤¸¹Ï¬°¹Ï¼Ë¡A¶ñ¥RI´º¡A¥i¥H¥Î LoadBitmap ¥Ñ¸ê·½¸ü¤J¦ì¤¸¹Ï¡AµM«á¥Î CreatePatternBrush §â³oӦ줸¹ÏÅܦ¨µ§¨ê¡A³Ì«áÁÙn¥H SelectObject §â³oÓµ§¨ê¿ï¤J¨ì³]³Æ¤º®e¤~¥i¥H¹F¨ì¥Øªº¡C¦¹³BÁÙ¶·ª`·N¤@ÓÆ[©À¡A¨C·í§Ú̥ΠGDI ªA°Èµ{¦¡Ã¸¹Ï ( ¦pµe½u¡Bµe¯x§Îµ¥ ) ©Î¨ä¥L¾Þ§@ ( ¦p¿ï¾Ü GDI ª«¥ó®É )¡A¨ä¹ï¶H³£¬O³]³Æ¤º®e¦Ó«D¦ì¤¸¹Ï¡C©Ò¥Hn§â GDI ªA°Èµ{¦¡§@¥Î©ó¦ì¤¸¹Ï®É¡A¥²¶·¥ý¥Î CreateCompatibleDC «Ø¥ß¤@Ó¬Û®eªº³]³Æ¤º®e¡A¦A§â¦ì¤¸¹Ï¿ï¤J³oÓ¬Û®eªº³]³Æ¤º®e¡A¤§«áªº GDI ªA°Èµ{¦¡¤~·|§@¥Î¦b¦ì¤¸¹Ï¤W¡C¤@¯ë¦Ó¨¥¡An¾Þ§@¤@Ӧ줸¹Ï¡A´N±o«Ø¥ß¤@Ó³]³Æ¤º®e¡C¦ì¤¸¹Ï¤j¦h¦s©ñ©ó¸ê·½ùØ¡A»Ýn®É¥Ñ¸ê·½¸ü¤J§Y¥i¡A³o®Éªº¦ì¤¸¹Ï¬O¤w¸gªì©l¤Æ¤Fªº¡F¦ý¬O¤]¦³¥¼ªì©l¤Æªº¦ì¤¸¹Ï¡A¨Ò¦pn§â¹Ï¼Ë¶ñ¥R¨ì½w½Äªº³]³Æ¤º®e·í°µI´º¹Ï®É¡A³oÓI´º¹Ï´N¬O¥¼ªì©l¤Æªº¡A¦]¬°I´º¹Ï³£©|¥¼¶ñ¥R§¹¦¨¡A·íµMÁÙ¨Sªì©l¤Æ¡A§Ṳ́]´NµLªk¥Î SelectObject ¿ï¤J¬Y¤@Ӧ줸¹Ï¡C³o®ÉÔ¥i¥H¥Î CreateCompatibleBitmap «Ø¥ß¥¼ªì©l¤Æªº¦ì¤¸¹Ï¡AµM«á¦A¥Î SelectObject ¿ï¤J¨ì½w½Äªº³]³Æ¤º®e¡A³Ì«á¦A¥H PatBlt µe¥XI´º¹Ï¡A¤~ºâ¬O§¹¦¨ªì©l¤Æ¡A³o®É´N¥i¥HÅã¥XI´º¹Ï®×¤F¡C
CreateCompatibleBitmap ªºì«¬¬O
HBITMAP CreateCompatibleBitmap( HDC hdc, // handle to device context int nWidth, // width of bitmap, in pixels int nHeight // height of bitmap, in pixels );
CreateCompatibleBitmap «Ø¥ß»P hdc ¬Û®eªº¦ì¤¸¹Ï¡A©Ò¿×¬Û®eªº¦ì¤¸¹Ï·N«ä¬O«ü³oӦ줸¹ÏÃC¦â¦h¹è»P hdc ¤@¼Ë¡A¨Ã¤£¬O§â¦ì¤¸¹Ï¿ï¤J¨ì hdc ùØ¡CnWidth¡BnHeight ¬O¦ì¤¸¹Ïªº¼e«×»P°ª«×¡A¥H¹Ï¯À¬°³æ¦ì¡C¦pªG¦¨¥\«Ø¥ß¦ì¤¸¹Ï¡A«h¶Ç¦^¦ì¤¸¹Ï¥N½X¡F§_«h¶Ç¦^ NULL¡C
CreatePatternBrush ¬O§â¯S©wªº¦ì¤¸¹Ï»s¦¨µ§¨ê¡A¨ä»yªk¬O
HBRUSH CreatePatternBrush( HBITMAP hbmp // handle to bitmap );
hbmp ¬O·Qn»s§@¦¨µ§¨êªº¦ì¤¸¹Ï¥N½X¡C¦pªG¦¨¥\¡ACreatePatternBrush ·|¶Ç¦^µ§¨ê¥N½X¡F§_«h¶Ç¦^ NULL¡C
Á¿¤F³o»ò¦h¡A²{¦b§ÚÌ¥i¥H¦Û¸ê·½ùبú¥X¦ì¤¸¹Ï§@¬°¹Ï¼Ë¡AµM«á§â¥¦¶ñ¥R¦b½w½Ä³]³Æ¤º®eùØ¡A³Ì«á¦A¶Ç¨ì¤u§@°Ïªº³]³Æ¤º®eùØ¡A¨ä¹Lµ{¤j¬ù¬O¹³©³¤U³o¼Ë¡G
INVOKE LoadBitmap,hInstance,OFFSET szBkGround ;¸ü¤J pattern.bmp ¦ì¤¸¹Ï mov hBmpBk,eax ;°O¿ý¦ì¤¸¹Ï¥N½X©ó hBmpBk ÅܼÆùØ INVOKE CreatePatternBrush,hBmpBk ;§â pattern.bmp »s§@¦¨µ§¨ê mov hBrushBk,eax ;§â³oÓµ§¨ê¥N½X°O¿ý¦b hBrushBk ÅܼÆùØ ... ... ... ... ;¨ä¥Lµ{¦¡¤ù¬q INVOKE GetDC,hWnd ;¨ú±oµøµ¡¤u§@°Ïªº³]³Æ¤º®e mov hdc,eax ;°O¿ý©ó hdc ÅܼÆùØ INVOKE CreateCompatibleDC,hdc ;«Ø¥ß½w½Ä°Ïªº°O¾ÐÅé³]³Æ¤º®e mov hdcbuffer,eax ;½w½Ä°Ïªº°O¾ÐÅé³]³Æ¤º®e¥N½X¦s©ó hdcbuffer ÅܼÆùØ INVOKE CreateCompatibleBitmap,hdc,xClient,yClient ;¥H hdc ¬°¥»¡A«Ø¥ß¥¼ªì©l¤Æªº¦ì¤¸¹Ï INVOKE SelectObject,hdcbuffer,eax ;§â¦ì¤¸¹Ï¿ï¤J½w½Ä°Ïªº°O¾ÐÅé³]³Æ¤º®e INVOKE SelectObject,hdcbuffer,hBrushBk ;¿ï¤Jµ§¨ê INVOKE PatBlt,hdcbuffer,0,0,xClient,yClient,PATCOPY;¶ñ¥Rµ§¨ê
¨ì¦¹¡AI´º¹Ï´N¤w¸g¥þ³¡§¹¦¨¤F¡C
·í²y²¾°Ê®É¡Aµ{¦¡¥²¶·¥ý§âªº²yÅé¹Ï®×À¿±¼Åܦ¨I´º¡A¦A§â²yÅé¹Ï®×µe¦b·sªº¦ì¸m¤W¡A¨C¹j¤@¦¸·¥µuªº®É¶¡³£«½Æ³oÓ¹Lµ{°µ¡A¨º»ò¦b¨Ï¥ÎªÌ¬Ý¨Ó¡A´N¦n¹³²yÅé·|²¾°Ê¤@¼Ë¡C¤p¤ì°¸¦b³B²z WM_CREATE °T®§®É¡A«Ø¥ß¤F¤@Óp®É¾¹¡G
INVOKE SetTimer,hWnd,TimerID,time,NULL
³oÓp®É¾¹¨C¹j time ²@¬íµo¥X¤@Ó WM_TIMER °T®§¡A¤p¤ì°¸¦b¦¹°T®§¤¤pºâ²yÅé¹Ï®×·sªº¥ª¤W¨¤®y¼Ð¡A¨Ã°O¿ý©ó ( x¡Ay )¡C
¦b§âªº²yÅé¹Ï®×À¿±¼ªº¹Lµ{¤¤¡A§ÚÌ¥i¥H§â¾ãÓ¤u§@°Ï³£«·sø»s¡A¦ý¬O³o¼Ë°µ¨Æ¤ñ¸û¯Ó¶O¸ê·½¡C¥t¤@ºØ°µªk¬O¡A¥u¦bªº²yÅé¹Ï®×ªº¨º¤@³¡¥÷¦ì¸m¡A¥HI´º¹Ï¬Û¹ïÀ³ªº¦ì¸mµe¤W¥h¡C¦b ball µ{¦¡ùØ¡A¤p¤ì°¸±Ä¥Î«áºØ°µªk¡C©Ò¥H¤p¤ì°¸·Ç³Æ¤F¨âÓ³]³Æ¤º®e¡Ghdcbk »P hdcbuffer¡A³o¨âÓ³]³Æ¤º®e³£¬O°O¾ÐÅé³]³Æ¤º®e¡A¨âªÌ¤]³£¦s¦³I´º¹Ï¡A¦ý¬O«eªÌ¶î¤WI´º¹Ï«á¡A´N¤£·|¦AÅÜ°Ê¡F¦Ó«áªÌ«h·|µe¤W¹B°Êªº²yÅé¡A¨C¤@¦¸²¾°Ê²yÅén¦bªº²yÅé¦ì¸mÀ¿±¼²yÅé¹Ï®×®É¡Aµ{¦¡´N±q hdcbk ¬Û¹ïÀ³Âªº²yÅé¦ì¸m¨ú¥XI´º¹ÏÂл\¤W¡Cµ{¦¡
INVOKE BitBlt,hdcbuffer,x0,y0,BallSize,BallSize,hdcbk,x0,y0,SRCCOPY
´N¬O¦b°õ¦æ³o¼Î¤u§@¡Ax0¡By0 ¬O²yÅé¹Ï®×ªºÂ¦ì¸mªº¥ª¤W¨¤®y¼Ð¡C±µ¤U¨Ó´N¬O§â·sªº²yÅé¦ì¸m¡A( x¡Ay )¡Aµe¤W²yÅé¹Ï®×¡C²yÅé¹Ï®×ªº¼Ë¤l¦p¹Ï¤@¤¤¶¡ªº ball.bmp¡A¥¦¬Oªø¼e¦U 80 ¹Ï¯Àªº¦ì¤¸¹Ï¡C°²¦p¥u¥Î³æ¯Âªº¥H SRCCOPY ¬°¾Þ§@°Ñ¼Æ©I¥s BitBlt §â²yÅé«þ¨©¨ì hdcbuffer¡A¨º»ò²yÅé©P³ò·|¦³¶Â®Ø¡F°²¦p·Q§â¶Â®Ø¥h±¼¡A±o©MI´º¹Ï¶i¦æ OR ¹Bºâ ( ¦]¬°¶Â¦âªºÃC¦â¬° 0¡A»PI´º¹Ï§@ OR ¹Bºâ«á¡A¤´·|±o¨ìI´º¹Ï )¡A¬d MSDN¡A±oª¾À³¥Î SRCPAINT ¬°¾Þ§@°Ñ¼Æ©I¥s BitBlt¡C¦ý¦p¦¹¤@¨Ó¡A²yÅ骺³¡¥÷¤]·|»PI´º¹Ï§@ OR ¹Bºâ¡A¦ý¬O°²¦p¯à¥ýÅýI´º¹Ïªº²yÅ鳡¥÷Åܦ¨¶Â¦â¤S¤£¼vÅT¨ì²yÅé©P³òªº³¡¥÷¡A´N¥i¥H¸Ñ¨M³oÓ°ÝÃD¤F¡C¤@¯ëªº°µªk¬O¦A«Ø¥ß¤@±i¹Ï¤ù¡A³o±i¹Ï¤ù¤@¯ëºÙ¬°¾B¸n¹Ï¡A¤]´N¬O¹Ï¤@³Ì¥kÃ䪺 mask.bmp¡A¹Ï®×ªº¥Dn³¡¥÷¡A¤]´N¬O²yÅé¡A¬°¶Â¦â¡A¦ÓI´º¬°¥Õ¦â¡A¥ýÅý¾B¸n¹Ï»PI´º¹Ï§@ AND ¹Bºâ¡A¦A¨Ï¦ì¤¸¹Ï»PI´º¹Ï§@ OR ¹Bºâ§Y¥i¡G
INVOKE BitBlt,hdcbuffer,x,y,BallSize,BallSize,hdcmask,0,0,SRCAND INVOKE BitBlt,hdcbuffer,x,y,BallSize,BallSize,hdcball,0,0,SRCPAINT
¥H¤Wªº¨BÆJ¥i¥H¥Î¤U¹Ï¨Ó»¡©ú¡G
¤W¹Ï¥ªªºI´º¹Ï¤Wªº¥Õ®Ø¥Nªí¦¹³B±Nµe¤W²yÅ骺¹Ï®×¡Aº¥ý¬O±o·Ç³Æ¦n¤@±i¾B¸n¹Ï¡A¦¹¾B¸n¹Ï¬O§ânµe¥Xªº¹Ï®×³¡¥÷¥H¶Â¦âªí¥Ü¡A¦Ó¨ä¥L¤´¬°I´ºªº³¡¥÷¥H¥Õ¦âªí¥Ü¡A±µµÛ§âI´º¹Ï¥Õ®Ø³¡¥÷»P¾B¸n¹Ï§@ AND ¹Bºâ¡A³Ì«á¦A§âè誺µ²ªG»P²yÅé§@ OR ¹Bºâ¡A´N¥i¥H¥u§â²yÅéµe¤W¥h¡A¦Ó¦b¥Õ®Ø¤º¥B¦b²yÅé¥H¥~ªº³¡¥÷¤´¬°I´º¹Ï¡C
¦b WM_TIMER ³Ì«á¡A©I¥s InvalidateRect ¨Ï¤u§@°ÏÅܬ°µL®Ä¡C¦Ó¦b WM_PAINT °T®§¤¤¡A¶È¦³¤@±ø«ü¥O¡A´N¬O§â½w½Ä°Ïªº³]³Æ¤º®e«þ¨©¨ì¤u§@°Ï¡G
INVOKE BitBlt,ps.hdc,0,0,xClient,yClient,hdcbuffer,0,0,SRCCOPY
BALL µ{¦¡µLªk½Õ¾ãµøµ¡¤j¤p¡A¥Dn¬O¦b©I¥s CreateWindowEx ®É¡Aµøµ¡·®æ¶È¥Î WS_CAPTION or WS_SYSMENU¡A¤£¨Ï¥Î±`¨£ªº WS_OVERLAPPEDWINDOW¡G
INVOKE CreateWindowEx,NULL,OFFSET szClassName,OFFSET szAppName,\ WS_CAPTION or WS_SYSMENU,0,0,xClient,yClient,0,NULL,hInstance,\ NULL