Ch 07 ¥[¤W¹Ï¥Ü

³o¤@³¹¡A±N¤¶²Ð¦bµ{¦¡¤¤¥[¤J¦Û¤v³]­pªº¡y¹Ï¥Ü¡z( icon )¡A¦Ó¤£¨Ï¥Î¨t²Î¤¤¤º©w­È¡C¥[¤J¹Ï¥Ü«áªºµ{¦¡¥i¥H¦b®à­±¤W¡B¸ê®Æ§¨ùØÅã¥Ü³o­Ó¹Ï¥Ü¡A¦P®Éµ{¦¡°õ¦æ®É¡A¯à¦b¼ÐÃDÄæ³BÅã¥Ü³o­Ó¹Ï¥Ü¡C


­ì²z

´N¦p¦P¤W¤@³¹´¿»´»´¦a´£¨ì¡A¹Ï¥Ü¤]¬OÄÝ©ó UI ¸ê·½ªº¤@ºØ¡C­n¦bµ{¦¡¤¤¥[¤J¹Ï¥Ü¡A¥²¶·¥ý³]­p¦n¤@­Ó·sªº¹Ï¥ÜÀÉ¡A¨ä°ÆÀɦW³q±`¬O *.ico¡A±z¥i¥H¥Î¹Ï¥Ü½s¿è¾¹¦Û¦æø»s¹Ï¥ÜÀÉ¡A¤]¥i¥H¦bºô»Úºô¸ô¤W´M§ä«e½ú­Ì¤w»s§@¦nªº¹Ï¥ÜÀÉ¡C

»s§@¦n©Î§ä¨ì¤¤·Nªº¹Ï¥ÜÀÉ«á¡A¦b¸ê·½´y­zÀɤ¤¥[¤W¤@¬q¦³Ãö¹Ï¥Üªº´y­z¡A¨ä®æ¦¡¦p¤U¡G

nameID  ICON    filename

ICON ¬O©T©wªº¡Aªí¥Ü¦¹¬q¸¨¬O´y¬O¹Ï¥Ü¤§¥Î¡CnameID ¬O¹Ï¥Üªº¦WºÙ©ÎÃѧO½X¡A¥i¥H¬O¤@­Ó¦r¦ê¡A¤]¥i¥H¬O¤@­Ó¤p©ó 65535 ªº¥¿¾ã¼Æ¡A±N¨Ó¦bµ{¦¡¤¤¾a nameID »Pµ{¦¡½X³s±µ¡Cfilename ¬O¹Ï¥ÜÀÉ¡A¤@¯ë°ÆÀɦW¬° *.ico¡A°²¦p¹Ï¥ÜÀɤ£¦b²{¦æ¤l¥Ø¿ý¤º¡Afilename ¤]¥i¥H¥]§t¸ô®|¡C

¦b­ì©lµ{¦¡¤¤«h¥H LoadIcon API ¨Ïµ{¦¡¦Û UI ¸ê·½¤¤¸ü¤J¹Ï¥Ü¡CLoadIcon ªº­ì«¬½Ð°Ñ¦Ò²Ä¤G³¹¦³Ãö LoadIcon API ªº»¡©ú¡C¦ý¦¹³B¤£¬O­n¸ü¤J¨t²Î¹w³]­È¡A©Ò¥H¥²¶·¥ý¦b¸ê®ÆÂ_¤¤³]©w¤@­Ó¥H NULL µ²§Àªº¦r¦ê¡A¦Ó¦¹¦r¦ê´N¬O¹Ï¥Ü¦WºÙ¡A¤]´N¬O¦b¸ê·½´y­zÀɤ¤ªº nameID¡C


­ì©lµ{¦¡

©³¤U³o­Ó¨Ò¤l¡A±N¬°«e¤@³¹ªº DRAW.EXE ¥[¤W¹Ï¥Ü¡A¥[¤W¹Ï¥Ü«áªº¥i°õ¦æÀɺ٬° DRAW2.EXE¡C©Ò¥[¤Jªº¹Ï¥ÜÀɦW¬° elephant.ico¡A¦p¤U¹Ï¡G

elephant.ico
¸ê·½´y­zÀÉ¡ADRAW2.RC¡A¤º®e¦p¤U¡G

#define IDM_Exit        10
#define IDM_Line        21
#define IDM_Ellipse     22
#define IDM_Trace       23
#define IDM_Red         24
#define IDM_Green       25
#define IDM_Blue        26

DRAW    MENU
{

MENUITEM    "Â÷¶}(&E)",IDM_Exit

POPUP   "ø¹Ï(&D)"
        {
        MENUITEM    "½u¬q(&L)",IDM_Line
        MENUITEM    "¾ò¶ê(&E)",IDM_Ellipse
        MENUITEM    "¤âø¹Ï(&T)",IDM_Trace
        MENUITEM    SEPARATOR
        MENUITEM    "¬õ(&R)",IDM_Red
        MENUITEM    "ºñ(&G)",IDM_Green
        MENUITEM    "ÂÅ(&B)",IDM_Blue
        }
}

TinyDraw        ICON    elephant.ico

DRAW2.ASM ¨Ó·½Àɤº®e¦p¤U¡G

        .386
        .model  flat,stdcall
        option  casemap:none
include         windows.inc
include         user32.inc
include         gdi32.inc
include         kernel32.inc
includelib      user32.lib
includelib      gdi32.lib
includelib      kernel32.lib

IDM_Exit        equ     10      ;12 ¿ï¶µ½s¸¹
IDM_Line        equ     21
IDM_Ellipse     equ     22
IDM_Trace       equ     23
IDM_Red         equ     24
IDM_Green       equ     25
IDM_Blue        equ     26
red_color       equ     0ffh    ;19 ÃC¦â
green_color     equ     0ff00h
blue_color      equ     0ff0000h

WndProc         proto   :HWND,:UINT,:WPARAM,:LPARAM

        .DATA
ClassName       db          'SimpleWinClass',0
AppName         db          '¤p¤pø¹Ïµ{¦¡',0
AskExit         db          '¬O§_°h¥X¦¹µ{¦¡¡H',0
AskExitTitle    db          '¸ß°Ý',0
MenuName        db          'DRAW',0
IconName        db          'TinyDraw',0
PenDown         db          FALSE       ;31 FALSE¡Jµeµ§´£°_¡ATRUE¡Jµeµ§Ä²¯È
command         dw          IDM_Line    ;32 ¨Ï¥ÎªÌ¥Ñ¿ï³æ¿ï¾Üªº©R¥O
color           dd          red_color   ;33 ¨Ï¥ÎªÌ¥Ñ¿ï³æ¿ï¾ÜªºÃC¦â
hMenu           HMENU       ?
hInstance       HINSTANCE   ?
hwnd            HWND        ?
wc              WNDCLASSEX  <30h,?,?,0,0,?,?,?,?,0,offset ClassName,?>
msg             MSG         <?>
start_point     POINT       <?>         ;39 °_ÂI®y¼Ð
end_point       POINT       <?>         ;40 ²×ÂI®y¼Ð

        .CODE
start:  invoke  GetModuleHandle,NULL
        mov     hInstance,eax
        mov     wc.style,CS_HREDRAW or CS_VREDRAW
        mov     wc.lpfnWndProc,offset WndProc
        mov     eax,hInstance
        mov     wc.hInstance,eax
        mov     wc.hbrBackground,COLOR_WINDOW+1
        invoke  LoadIcon,hInstance,offset IconName
        mov     wc.hIcon,eax
        mov     wc.hIconSm,eax
        invoke  LoadCursor,NULL,IDC_ARROW
        mov     wc.hCursor,eax
        invoke  LoadMenu,hInstance,offset MenuName      ;55 ¨ú±o¿ï³æ
        mov     hMenu,eax
        invoke  RegisterClassEx,offset wc
        invoke  CreateWindowEx,NULL,offset ClassName,\ 
                offset AppName,WS_OVERLAPPEDWINDOW,0,\
                0,400,400,0,hMenu,hInstance,NULL        ;60 ¥[¤W¿ï³æ
        mov     hwnd,eax
        invoke  ShowWindow,hwnd,SW_SHOWNORMAL
        invoke  UpdateWindow,hwnd

.while  TRUE
        invoke  GetMessage,offset msg,NULL,0,0
.break  .if     !eax
        invoke  DispatchMessage,offset msg
.endw        
        mov     eax,msg.wParam
        invoke  ExitProcess,eax
;---------------------------------------
;­pºâ EAX ¤ºªº®y¼Ð¡A¦s©ó EDX ©Ò«üªº¦ì§}¡AEAX¡J°ª¦ì¤¸¬° Y ®y¼Ð¡A§C¦ì¤¸¬° X ®y¼Ð
get_crd proc
        push    eax
        and     eax,0ffffh
        mov     [edx],eax
        pop     eax
        shr     eax,10h
        mov     [edx+4],eax
        ret
get_crd endp
;---------------------------------------
WndProc proc    hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
        local   rectangle:RECT
        local   PS:PAINTSTRUCT
        local   hdc:HDC
        local   newPen:DWORD
        local   oldPen:DWORD
.if     uMsg==WM_COMMAND
        mov     eax,wParam
        mov     PenDown,FALSE
        .if     ax==IDM_Exit
                jmp     exit
        .elseif (ax==IDM_Line)||(ax==IDM_Ellipse)||(ax==IDM_Trace)
                mov     command,ax
        .elseif ax==IDM_Red
                mov     color,red_color
        .elseif ax==IDM_Green
                mov     color,green_color
        .elseif ax==IDM_Blue
                mov     color,blue_color
        .endif

.elseif uMsg==WM_LBUTTONDOWN                    ;105 «ö¤U·Æ¹«¥ªÁä
        .if     (command==IDM_Line)||(command==IDM_Ellipse)
                .if     PenDown
                        mov     eax,lParam      ;108 µeµ§Ä²¯È®É
                        mov     edx,offset end_point
                        call    get_crd
                        invoke  GetClientRect,hWnd,addr rectangle
                        invoke  InvalidateRect,hWnd,addr rectangle,FALSE
                .else
                        mov     eax,lParam      ;114 µeµ§´£°_®É
                        mov     edx,offset start_point
                        call    get_crd
                .endif

        .elseif command==IDM_Trace
                .if     (!PenDown)
                        mov     eax,lParam
                        mov     edx,offset start_point
                        call    get_crd
                .endif                   
        .endif
        xor     PenDown,1       ;118 ¤Á´«µeµ§´£°_©ÎIJ¯È

.elseif uMsg==WM_MOUSEMOVE
        .if     (command==IDM_Trace)&&(PenDown)
                mov     eax,lParam
                mov     edx,offset end_point
                call    get_crd
                invoke  GetClientRect,hWnd,addr rectangle
                invoke  InvalidateRect,hWnd,addr rectangle,FALSE
        .endif

.elseif uMsg==WM_PAINT
        .if     command==IDM_Line
                invoke  BeginPaint,hWnd,addr PS
                mov     hdc,eax
                invoke  CreatePen,PS_SOLID,1,color
                mov     newPen,eax
                invoke  SelectObject,hdc,newPen
                mov     oldPen,eax
                invoke  MoveToEx,hdc,start_point.x,start_point.y,NULL
                invoke  LineTo,hdc,end_point.x,end_point.y
                invoke  SelectObject,hdc,oldPen
                invoke  DeleteObject,newPen
                invoke  EndPaint,hdc,addr PS
        .elseif command==IDM_Ellipse
                invoke  BeginPaint,hWnd,addr PS
                mov     hdc,eax
                invoke  CreatePen,PS_SOLID,1,color
                mov     newPen,eax
                invoke  SelectObject,hdc,newPen
                mov     oldPen,eax
                mov     eax,start_point.x
                add     eax,end_point.x
                shr     eax,1
                push    eax
                invoke  Arc,hdc,start_point.x,start_point.y,\   ;161 µe¥X¥b¾ò¶ê
                        end_point.x,end_point.y,eax,400,eax,0
                pop     eax
                invoke  Arc,hdc,start_point.x,start_point.y,\   ;164 µe¥X¥t¥~¤@¥b¾ò¶ê
                        end_point.x,end_point.y,eax,0,eax,400
                invoke  SelectObject,hdc,oldPen
                invoke  DeleteObject,newPen
                invoke  EndPaint,hdc,addr PS
        .elseif command==IDM_Trace
                invoke  BeginPaint,hWnd,addr PS
                mov     hdc,eax
                invoke  CreatePen,PS_SOLID,1,color
                mov     newPen,eax
                invoke  SelectObject,hdc,newPen
                mov     oldPen,eax
                invoke  MoveToEx,hdc,start_point.x,start_point.y,NULL
                invoke  LineTo,hdc,end_point.x,end_point.y
                invoke  SelectObject,hdc,oldPen
                invoke  DeleteObject,newPen
                invoke  EndPaint,hdc,addr PS
                mov     eax,end_point.x
                mov     edx,end_point.y
                mov     start_point.x,eax
                mov     start_point.y,edx
        .else
                jmp     def
        .endif

.elseif uMsg==WM_CLOSE
exit:   invoke  MessageBox,NULL,offset AskExit,\
                offset AskExitTitle,MB_YESNO or MB_ICONQUESTION
        .if     eax==IDYES
                invoke  DestroyWindow,hWnd
        .endif

.elseif uMsg==WM_DESTROY
        invoke  PostQuitMessage,NULL

.else
def:    invoke  DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
.endif
        xor     eax,eax
        ret
WndProc endp

end     start

DRAW2.MAK ¤§¤º®e¦p¤U¡G

# ²£¥Í DRAW2.EXE
ALL : DRAW2.EXE

# ²ÕĶ¾¹¤Î³sµ²¾¹
DRAW2.EXE : DRAW2.ASM DRAW2.RES
# ²ÕĶ¾¹·|¦Û°Ê¶Ç DRAW2.OBJ µ¹³sµ²¾¹¡A¬G /link ¤§«á¤£¥Î¦A¥[¤W DRAW2.OBJ
    ml /coff DRAW2.ASM /link /SUBSYSTEM:WINDOWS DRAW2.RES 

# ¸ê·½½sĶ¾¹
DRAW2.RES : DRAW2.RC elephant.ico
    rc DRAW2.RC

²ÕĶ®É¡A¶È»Ý¦b DOS ¼Ò¦¡¤U¿é¤J

d:\homepage\source>nmake -f draw2.mak [Enter]

Microsoft (R) Program Maintenance Utility   Version 1.50
Copyright (c) Microsoft Corp 1988-94. All rights reserved.

        ml /coff DRAW2.ASM /link /SUBSYSTEM:WINDOWS DRAW2.RES
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: DRAW2.ASM
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/SUBSYSTEM:WINDOWS /SUBSYSTEM:WINDOWS
"DRAW2.obj"
"/OUT:DRAW2.exe"
"DRAW2.RES"

d:\homepage\source>

´N¥i¥H±o¨ì DRAW2.EXE ÀÉ¡A¥´¶} DRAW2.EXE ©Ò¦b¸ê®Æ§¨¡AÀ˵ø¬Ý¬Ý¬O§_¹Ï¥Ü¤w¸g´«¦¨ elephant.ico ¤F¡G

À˵ø DRAW.EXE ¹Ï¥Ü¬O§_¥¿½TÅã¥Ü
±z¥i¥H¬Ý¨ì¡A¥k¤W¨¤©M¤¤¥¡¤U­±¦U¦³¨â­Ó¤j¶Hªº¹Ï¥Ü¡A¤U­±ªº¬O elephant.ico ¹Ï¥ÜÀÉ¡A¥k¤W¨¤ªº¬O DRAW2.EXE ÀÉ¡A°õ¦æ¬Ý¬Ý
ªG¯u¦b¼ÐÃDÄ椤¹Ï¥Üªº¦ì¸m¡A¤]¤w¸g´«¦¨©M elephant.ico ¤@¼Ëªº¼Ë¤l¤F¡C¨ì¦¹¤p¤ì°¸¤w¸g¦¨¥\ªº¬°°õ¦æÀÉ¥[¤W¹Ï¥Ü¤F¡C

µ{¦¡´X¥G©M¤W¤@³¹¬Û¦P¡A¦]¦¹¨ä¥L³¡¥÷´N¤£¦A¸ÑÄÀ¡A¦Ü©ó¥[¤J¹Ï¥Üªº³¡¥÷¡A±z¥u­nª`·N­ì©lµ{¦¡¤¤¬õ¦âªº³¡¥÷´N¥i¥H¤F¡C


¨ì²Ä¤»³¹¡A ¦^¨ì­º­¶¡A ¨ì²Ä¤K³¹