GUI/Window

MASM

jjryu 2013. 6. 17. 20:47

TITLE Windows Application                   (WinApp.asm)


.386

.MODEL flat,stdcall

INCLUDE SmallWin.inc ; kernel32.lib

INCLUDE GraphWin.inc ; user32.lib

.STACK 4096


;==================== DATA =======================

.data

PopupTitle BYTE "Popup Window",0

PopupText  BYTE "This window was activated by a "

      BYTE "WM_LBUTTONDOWN message",0


ErrorTitle  BYTE "Error",0

WindowName  BYTE "ASM Windows App",0

className   BYTE "ASMWin",0


; Define the Application's Window class structure.

MainWin WNDCLASS <NULL,WinProc,NULL,NULL,NULL,NULL,NULL, \

COLOR_WINDOW,NULL,className>


msg      MSGStruct <>

winRect   RECT <>

hMainWnd  DWORD ?

hInstance DWORD ?


;=================== CODE =========================

.code

WinMain PROC

; Get a handle to the current process.

INVOKE GetModuleHandle, NULL

mov hInstance, eax

mov MainWin.hInstance, eax


; Load the program's icon and cursor.

INVOKE LoadIcon, NULL, IDI_APPLICATION

mov MainWin.hIcon, eax

INVOKE LoadCursor, NULL, IDC_ARROW

mov MainWin.hCursor, eax


; Register the window class.

INVOKE RegisterClass, ADDR MainWin

.IF eax == 0

 call ErrorHandler

 jmp Exit_Program

.ENDIF


; Create the application's main window.

; Returns a handle to the main window in EAX.

INVOKE CreateWindowEx, 0, ADDR className,

 ADDR WindowName,MAIN_WINDOW_STYLE,

 CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,

 CW_USEDEFAULT,NULL,NULL,hInstance,NULL

mov hMainWnd,eax


; If CreateWindowEx failed, display a message & exit.

.IF eax == 0

 call ErrorHandler

 jmp  Exit_Program

.ENDIF


; Show and draw the window.

INVOKE ShowWindow, hMainWnd, SW_SHOW

INVOKE UpdateWindow, hMainWnd


; Begin the program's message-handling loop.

Message_Loop:

; Get next message from the queue.

INVOKE GetMessage, ADDR msg, NULL,NULL,NULL


; Quit if no more messages.

.IF eax == 0

 jmp Exit_Program

.ENDIF


; Relay the message to the program's WinProc.

INVOKE DispatchMessage, ADDR msg

    jmp Message_Loop


Exit_Program:

 INVOKE ExitProcess,0

WinMain ENDP


;-----------------------------------------------------

WinProc PROC,

hWnd:DWORD, localMsg:DWORD, wParam:DWORD, lParam:DWORD

; The application's message handler, which handles

; application-specific messages. All other messages

; are forwarded to the default Windows message

; handler.

;-----------------------------------------------------

mov eax, localMsg


.IF eax == WM_LBUTTONDOWN ; mouse button?

 INVOKE MessageBox, hWnd, ADDR PopupText,
   ADDR PopupTitle, MB_OK

 jmp WinProcExit

.ELSEIF eax == WM_CREATE ; create window?

  ..

 jmp WinProcExit

.ELSEIF eax == WM_CLOSE ; close window?

  ..

 INVOKE PostQuitMessage,0

 jmp WinProcExit

.ELSE ; other message?

 INVOKE DefWindowProc, hWnd, localMsg, wParam, lParam

 jmp WinProcExit

.ENDIF


WinProcExit:

ret

WinProc ENDP


;---------------------------------------------------

ErrorHandler PROC

; Display the appropriate system error message.

;---------------------------------------------------

.data

pErrorMsg  DWORD ? ; ptr to error message

messageID  DWORD ?

.code

INVOKE GetLastError ; Returns message ID in EAX

mov messageID,eax


; Get the corresponding message string.

INVOKE FormatMessage, FORMAT_MESSAGE_ALLOCATE_BUFFER + \

 FORMAT_MESSAGE_FROM_SYSTEM,NULL,messageID,NULL,

 ADDR pErrorMsg,NULL,NULL


; Display the error message.

INVOKE MessageBox,NULL, pErrorMsg, ADDR ErrorTitle,

 MB_ICONERROR+MB_OK


; Free the error message string.

INVOKE LocalFree, pErrorMsg

ret

ErrorHandler ENDP

END WinMain