WIN32汇编语言教程:第07章 图形操作 · 7.3 创建和使用位图(3)

               invoke DeleteDC,hDcBack
               invoke DeleteDC,hDcClock
               invoke DeleteObject,hBmpBack
               invoke DeleteObject,hBmpClock
               ret
 
_DeleteBackGround  endp
;####################################################################
_Init      proc
               local  @hBmpBack,@hBmpCircle
 
;********************************************************************
; 初始化菜单
;********************************************************************
               invoke CreatePopupMenu
               mov    hMenu,eax
               invoke AppendMenu,hMenu,0,IDM_BACK1,offset szMenuBack1
               invoke AppendMenu,hMenu,0,IDM_BACK2,offset szMenuBack2
               invoke AppendMenu,hMenu,MF_SEPARATOR,0,NULL
               invoke AppendMenu,hMenu,0,IDM_CIRCLE1,offset szMenuCircle1
               invoke AppendMenu,hMenu,0,IDM_CIRCLE2,offset szMenuCircle2
               invoke AppendMenu,hMenu,MF_SEPARATOR,0,NULL
               invoke AppendMenu,hMenu,0,IDM_EXIT,offset szMenuExit
               invoke CheckMenuRadioItem,hMenu,IDM_BACK1,IDM_BACK2,\
                       IDM_BACK1,NULL
               invoke CheckMenuRadioItem,hMenu,IDM_CIRCLE1,IDM_CIRCLE2,\
                       IDM_CIRCLE1,NULL
;********************************************************************
; 设置圆形窗口并设置“总在最前面”
;********************************************************************
               invoke CreateEllipticRgn,0,0,CLOCK_SIZE+1,CLOCK_SIZE+1
               push       eax
               invoke SetWindowRgn,hWinMain,eax,TRUE
               pop    eax
               invoke DeleteObject,eax
               invoke SetWindowPos,hWinMain,HWND_TOPMOST,0,0,0,0,\
                       SWP_NOMOVE or SWP_NOSIZE
;********************************************************************
; 建立背景
;********************************************************************
               mov    dwNowBack,IDB_BACK1
               mov    dwNowCircle,IDB_CIRCLE1
                  invoke _CreateBackGround
                  invoke _CreateClockPic
                  invoke SetTimer,hWinMain,ID_TIMER,1000,NULL
                 ret
 
_Init         endp
;####################################################################
_Quit         proc
 
               invoke KillTimer,hWinMain,ID_TIMER
               invoke DestroyWindow,hWinMain
               invoke PostQuitMessage,NULL
               invoke _DeleteBackGround
               invoke DestroyMenu,hMenu
               ret
 
_Quit        endp
;####################################################################
_ProcWinMain      proc       uses ebx edi esi hWnd,uMsg,wParam,lParam
                  local  @stPS:PAINTSTRUCT
               local  @hDC
               local  @stPos:POINT
   
               mov    eax,uMsg
;********************************************************************
               .if    eax == WM_TIMER
                       invoke _CreateClockPic
                       invoke InvalidateRect,hWnd,NULL,FALSE
;********************************************************************
               .elseif eax == WM_PAINT
                       invoke BeginPaint,hWnd,addr @stPS
                       mov @hDC,eax
                       mov eax,@stPS.rcPaint.right
                       sub eax,@stPS.rcPaint.left
                       mov ecx,@stPS.rcPaint.bottom
                       sub ecx,@stPS.rcPaint.top
                       invoke BitBlt,@hDC,\
                               @stPS.rcPaint.left,@stPS.rcPaint.top,\
                               eax,ecx,hDcClock,@stPS.rcPaint.left,\
                               @stPS.rcPaint.top,SRCCOPY
                       invoke EndPaint,hWnd,addr @stPS
;********************************************************************
               .elseif eax == WM_CREATE
                       mov    eax,hWnd
                       mov    hWinMain,eax
                       invoke _Init
;********************************************************************
               .elseif eax == WM_COMMAND
                       mov    eax,wParam
;********************************************************************
;由于印刷宽度的问题,影响源代码的缩进格式,请读者注意
;********************************************************************
.if        ax ==  IDM_BACK1
             mov    dwNowBack,IDB_BACK1
              invoke CheckMenuRadioItem,hMenu,IDM_BACK1,IDM_BACK2,IDM_BACK1,NULL
.elseif    ax ==  IDM_BACK2
              mov    dwNowBack,IDB_BACK2
              invoke CheckMenuRadioItem,hMenu,IDM_BACK1,IDM_BACK2,IDM_BACK2,NULL
.elseif    ax ==  IDM_CIRCLE1
             mov    dwNowCircle,IDB_CIRCLE1
             invoke CheckMenuRadioItem,hMenu,IDM_CIRCLE1,IDM_CIRCLE2,\
                      IDM_CIRCLE1,NULL
.elseif    ax ==  IDM_CIRCLE2
             mov    dwNowCircle,IDB_CIRCLE2
             invoke CheckMenuRadioItem,hMenu,IDM_CIRCLE1,IDM_CIRCLE2,\
                     IDM_CIRCLE2,NULL
.elseif    ax ==  IDM_EXIT
       call       _Quit
       xor    eax,eax
       ret
.endif
;********************************************************************
;恢复源代码缩进格式
;********************************************************************
                 invoke _DeleteBackGround
                 invoke _CreateBackGround
                 invoke _CreateClockPic
                 invoke InvalidateRect,hWnd,NULL,FALSE
          .elseif eax == WM_CLOSE
                 call      _Quit
;********************************************************************
; 按下右键时弹出一个POPUP菜单
;********************************************************************
           .elseif eax == WM_RBUTTONDOWN
                   invoke GetCursorPos,addr @stPos
                   invoke TrackPopupMenu,hMenu,TPM_LEFTALIGN,\
                           @stPos.x,@stPos.y,NULL,hWnd,NULL
;********************************************************************
; 由于没有标题栏,下面代码用于按下左键时移动窗口
; UpdateWindow:即时刷新,否则要等到放开鼠标时窗口才会重画
;********************************************************************
           .elseif eax == WM_LBUTTONDOWN
                  invoke SetCursor,hCursorMove
                  invoke UpdateWindow,hWnd
                 invoke ReleaseCapture
                     invoke  SendMessage,hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0
                 invoke SetCursor,hCursorMain
;********************************************************************
           .else
               invoke DefWindowProc,hWnd,uMsg,wParam,lParam
               ret
           .endif
;********************************************************************
              xor        eax,eax
              ret
 
_ProcWinMain      endp
;####################################################################
_WinMain      proc
             local  @stWndClass:WNDCLASSEX
             local  @stMsg:MSG
 
             invoke GetModuleHandle,NULL
             mov    hInstance,eax
             invoke LoadCursor,hInstance,IDC_MOVE
             mov    hCursorMove,eax
             invoke LoadCursor,hInstance,IDC_MAIN
             mov    hCursorMain,eax
;********************************************************************
; 注册窗口类
;********************************************************************
             invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
             invoke LoadIcon,hInstance,ICO_MAIN
             mov    @stWndClass.hIcon,eax
             mov    @stWndClass.hIconSm,eax
             push       hCursorMain
             pop    @stWndClass.hCursor
             push       hInstance
             pop    @stWndClass.hInstance
             mov    @stWndClass.cbSize,sizeof WNDCLASSEX
             mov    @stWndClass.style,CS_HREDRAW or CS_VREDRAW
             mov    @stWndClass.lpfnWndProc,offset _ProcWinMain
             mov    @stWndClass.hbrBackground,COLOR_WINDOW + 1
             mov    @stWndClass.lpszClassName,offset szClassName
             invoke RegisterClassEx,addr @stWndClass
;********************************************************************
; 建立并显示窗口
;********************************************************************
             invoke CreateWindowEx,NULL,\
                     offset szClassName,offset szClassName,\
                     WS_POPUP or WS_SYSMENU,\
                     100,100,CLOCK_SIZE,CLOCK_SIZE,\
                     NULL,NULL,hInstance,NULL
             Mov    hWinMain,eax
             invoke ShowWindow,hWinMain,SW_SHOWNORMAL
             invoke UpdateWindow,hWinMain
;********************************************************************
; 消息循环
;********************************************************************
             .while TRUE
                     invoke GetMessage,addr @stMsg,NULL,0,0
                     .break .if eax == 0
                     invoke TranslateMessage,addr @stMsg
                     invoke DispatchMessage,addr @stMsg
             .endw
             ret
 
_WinMain      endp
;####################################################################
start:
             call       _WinMain
             invoke ExitProcess,NULL
;####################################################################
              end    start

上页:第07章 图形操作 · 7.3 创建和使用位图(2) 下页:第07章 图形操作 · 7.3 创建和使用位图(4)

第07章 图形操作

版权所有 © 中山市飞娥软件工作室 证书:粤ICP备09170368号