;  #########################################################################
; 	Автор Чеботарёв Александр Михайлович ( Chib777 )
;	ОДЕССА (Украина - UKRAINE) 2003 // 09 // 30
;	chib_odessa@ua.fm
;	chib_odessa@mail.ru
;  #########################################################################
;Программа протестированна с MASM32V8!!!

      .486                      ; create 32 bit code
      .model flat, stdcall      ; 32 bit memory model
      option casemap :none      ; case sensitive
      include ErBd.inc        ; local includes for this file

.const
IDB_BITMAP1 	equ	101
IDB_BITMAP2 	equ	102
IDB_BITMAP3 	equ	103 
IDB_BITMAP4 	equ	104 
IDB_BITMAP5 	equ	105
IDB_BITMAP6 	equ	106
IDB_BITMAP7 	equ	107
IDB_BITMAP8 	equ	108
IDB_BITMAP9 	equ	109
IDB_BITMAP10 	equ	110
IDC_EDIT1	   equ		      3000
.code

; #########################################################################

start:

        invoke GetModuleHandle, NULL
        mov hInstance, eax

        invoke GetCommandLine
        mov CommandLine, eax

        invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
        invoke ExitProcess,eax

; #########################################################################

WinMain proc hInst     :DWORD,
             hPrevInst :DWORD,
             CmdLine   :DWORD,
             CmdShow   :DWORD

        ;====================
        ; Put LOCALs on stack
        ;====================

        LOCAL wc   :WNDCLASSEX
        LOCAL msg  :MSG

        LOCAL Wwd  :DWORD
        LOCAL Wht  :DWORD
        LOCAL Wtx  :DWORD
        LOCAL Wty  :DWORD

        ;==================================================
        ; Fill WNDCLASSEX structure with required variables
        ;==================================================

        mov wc.cbSize,         sizeof WNDCLASSEX
        mov wc.style,          CS_HREDRAW or CS_VREDRAW \
                               or CS_BYTEALIGNWINDOW
        mov wc.lpfnWndProc,    offset WndProc
        mov wc.cbClsExtra,     NULL
        mov wc.cbWndExtra,     NULL
        m2m wc.hInstance,      hInst	;<< NOTE: macro not mnemonic
        mov wc.hbrBackground,  NULL	;для обработки сообщения WM_ERASEBKGND, необходимо установливать в NULL
        mov wc.lpszMenuName,   NULL
        mov wc.lpszClassName,  offset szClassName
        mov wc.hIcon, FUNC(LoadIcon,hInst,500)	; icon ID
        mov wc.hCursor, FUNC(LoadCursor,NULL,IDC_ARROW)
        mov wc.hIconSm,        0

        invoke RegisterClassEx, ADDR wc

        ;================================
        ; Centre window at following size
        ;================================

        mov Wwd, 700
        mov Wht, 500

        invoke GetSystemMetrics,SM_CXSCREEN
        invoke TopXY,Wwd,eax
        mov Wtx, eax

        invoke GetSystemMetrics,SM_CYSCREEN
        invoke TopXY,Wht,eax
        mov Wty, eax
  ; -------------------------------------------------
        mov     hMenu,FUNC (LoadMenuA, hInstance, 600)
        invoke CreateWindowEx,WS_EX_LEFT,
                              ADDR szClassName,
                              ADDR szDisplayName,
                              WS_OVERLAPPEDWINDOW,
                              Wtx,Wty,Wwd,Wht,
                              NULL,hMenu,
                              hInst,NULL
        mov   hWnd,eax

        invoke ShowWindow,hWnd,SW_SHOWNORMAL
        invoke UpdateWindow,hWnd

      ;===================================
      ; Loop until PostQuitMessage is sent
      ;===================================

    StartLoop:
      invoke GetMessage,ADDR msg,NULL,0,0
      cmp eax, 0
      je ExitLoop
      invoke TranslateMessage, ADDR msg
      invoke DispatchMessage,  ADDR msg
      jmp StartLoop
    ExitLoop:

      return msg.wParam

WinMain endp

; #########################################################################

WndProc proc hWin   :DWORD,
             uMsg   :DWORD,
             wParam :DWORD,
             lParam :DWORD

    LOCAL hBmp   :DWORD
    LOCAL Rct    :RECT

    .if uMsg == WM_COMMAND
    ;======== menu commands ========


        .if wParam == 1010
            invoke SendMessage,hWin,WM_SYSCOMMAND,SC_CLOSE,NULL

       .elseif wParam == 1900

            invoke DialogBoxParamA, hInstance, ADDR DlgAbout, hWin, AboutProc, NULL
                
       .elseif wParam == 1200
	    m2m hBrush, hBrush1
              jmp m_end   
       .elseif wParam == 1202
	    m2m hBrush, hBrush2              
              jmp m_end                
       .elseif wParam == 1204
	    m2m hBrush, hBrush3                       
              jmp m_end                              
       .elseif wParam == 1206
	    m2m hBrush, hBrush4       
              jmp m_end        
       .elseif wParam == 1208
 	    m2m hBrush, hBrush5      
              jmp m_end                                          
       .elseif wParam == 1210
	    m2m hBrush, hBrush6       
              jmp m_end        
       .elseif wParam == 1212
	    m2m hBrush, hBrush7       
              jmp m_end        
       .elseif wParam == 1214
	    m2m hBrush, hBrush8       
              jmp m_end        
       .elseif wParam == 1216
	    m2m hBrush, hBrush9
              jmp m_end	    
       .elseif wParam == 1218
	    m2m hBrush, hBrush10  	            
m_end:
	    invoke GetClientRect, hWin, ADDR Rct		;определим размер клиентской области
	    invoke InvalidateRect, hWin, ADDR Rct, TRUE		;WM_PAINT -> WM_ERASEBKGND
;см. описание макроса
           CheckMIGroup hMenu, MF_UNCHECKED, <1200, 1202, 1204, 1206, 1208, 1210, 1212, 1214, 1216, 1218>
	 CheckMIGroup hMenu, MF_CHECKED, <wParam> 
                
        .endif
    ;====== end menu commands ======

    .elseif uMsg == WM_CREATE

          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP1)	;загрузим ресурс растровое изображение - BMP
	mov hBrush1, FUNC (CreatePatternBrush, hBmp)		;создадим кисть на основе растрового изображения
	invoke DeleteObject, hBmp				;удалим ресурс растровое изображение - BMP

          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP2)	
	mov hBrush2, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp		
	
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP3)
	mov hBrush3, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp	
	
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP4)
	mov hBrush4, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp		
	
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP5)
	mov hBrush5, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp		
	
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP6)
	mov hBrush6, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp		
	
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP7)
	mov hBrush7, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp
				
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP8)	
	mov hBrush8, FUNC (CreatePatternBrush, hBmp)	
	invoke DeleteObject, hBmp					

          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP9)
	mov hBrush9, FUNC (CreatePatternBrush, hBmp)
	invoke DeleteObject, hBmp
				
          mov hBmp, FUNC (LoadBitmap, hInstance, IDB_BITMAP10)	
	mov hBrush10, FUNC (CreatePatternBrush, hBmp)	
	invoke DeleteObject, hBmp	

          
	CheckMIGroup hMenu, MF_CHECKED, <1210>		;инициализация фона по умолчанию
	m2m hBrush, hBrush6
          
    .elseif uMsg == WM_ERASEBKGND
    
	invoke GetClientRect, hWin, ADDR Rct
	invoke SelectObject, wParam, hBrush				;выберем в контекст кисть
          invoke PatBlt, wParam, 0, 0, Rct.right, Rct.bottom, PATCOPY		;заполним прямоугольную область
	return TRUE

    .elseif uMsg == WM_CLOSE

    .elseif uMsg == WM_DESTROY
        invoke DeleteObject, hBrush1    
        invoke DeleteObject, hBrush2    
        invoke DeleteObject, hBrush3    
        invoke DeleteObject, hBrush4    
        invoke DeleteObject, hBrush5    
        invoke DeleteObject, hBrush6    
        invoke DeleteObject, hBrush7    
        invoke DeleteObject, hBrush8    
        invoke DeleteObject, hBrush9
        invoke DeleteObject, hBrush10        
        invoke PostQuitMessage,NULL
        return 0

    .endif

    invoke DefWindowProc,hWin,uMsg,wParam,lParam

    ret

WndProc endp

; ########################################################################

TopXY proc wDim:DWORD, sDim:DWORD

    shr sDim, 1      ; divide screen dimension by 2
    shr wDim, 1      ; divide window dimension by 2
    mov eax, wDim    ; copy window dimension into eax
    sub sDim, eax    ; sub half win dimension from half screen dimension

    return sDim

TopXY endp

; ########################################################################

AboutProc proc hWin   :DWORD,
             uMsg   :DWORD,
             wParam :DWORD,
             lParam :DWORD

    LOCAL hDC :DWORD
    LOCAL Rct :RECT
            
    .if uMsg == WM_INITDIALOG
	invoke GetDlgItem,hWin,IDC_EDIT1
	mov hEdit1, eax    
	invoke SendMessage, hEdit1, WM_SETTEXT, 0, ADDR About 

   .elseif uMsg == WM_COMMAND
    
	    .if wParam == IDOK
	    
	    jmp 	dlg_end
	    .endif
	    
   .elseif uMsg == WM_ERASEBKGND

	invoke GetClientRect, hWin, ADDR Rct
	invoke SelectObject, wParam, hBrush5
          invoke PatBlt, wParam, 0, 0, Rct.right, Rct.bottom, PATCOPY	
	mov eax, TRUE
	ret          
    .elseif uMsg == WM_CLOSE
	dlg_end:
	invoke EndDialog, hWin, 0    
    
             
    .endif
    
    xor eax, eax
    
    ret
    
AboutProc endp

; ########################################################################

end start
