天天看点

win32汇编--创建窗口程序(RadAsm)

.386
.model flat,stdcall
option casemap:none

include	windows.inc
include	kernel32.inc
include	user32.inc
include	gdi32.inc


includelib	gdi32.lib
includelib	kernel32.lib
includelib	user32.lib


.data?
	hInstance		dd	?
	hWinMain		dd	?

.data
	szCaption		db	'Different Style Windows',0
	szClass		db	'WindowStyle',0

.const
	IDC_CUR		equ	1001
	IDC_MAIN	equ	1002
.code
;****************************************************************************************************
;窗口过程
;****************************************************************************************************
_ProcWinMain	proc	hWnd,uMsg,wParam,lParam
		LOCAL	@stPs:PAINTSTRUCT
		LOCAL	@stRect:RECT
		LOCAL	@hdc:HDC
		mov	eax,uMsg
		.if	eax==WM_PAINT
			invoke	BeginPaint,hWnd,addr @stPs
			mov	@hdc,eax
			
			invoke	GetClientRect,hWnd,addr @stRect
			invoke	DrawText,@hdc,addr szCaption,-1,addr @stRect,DT_VCENTER or DT_CENTER
			invoke	EndPaint,hWnd,addr @stPs
		.elseif    	eax ==WM_CLOSE
			invoke	DestroyWindow,hWnd
			invoke	PostQuitMessage,NULL
		.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	RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
		;	给程序添加新的鼠标形状
		invoke	LoadCursor,hInstance,IDC_CUR
		mov	@stWndClass.hCursor,eax
		push	hInstance
		pop	@stWndClass.hInstance
		mov	@stWndClass.cbSize,sizeof WNDCLASSEX
		mov	@stWndClass.style,CS_HREDRAW or CS_HREDRAW
		mov	@stWndClass.lpfnWndProc,offset _ProcWinMain
		mov	@stWndClass.hbrBackground,COLOR_WINDOW+1
		mov	@stWndClass.lpszClassName,offset szClass
		;	给程序添加图标
		invoke	LoadIcon,hInstance,IDC_MAIN
		mov	@stWndClass.hIcon,eax
		invoke	RegisterClassEx,addr @stWndClass
;*********************************************************************************************************
;建立并显示窗口
;*********************************************************************************************************		
		invoke	CreateWindowEx,WS_EX_CLIENTEDGE,addr szClass,addr szCaption,WS_OVERLAPPEDWINDOW,100,100,600,400,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

           

   使用RADAsm 编写的WIN32 窗口程序,主要用到函数与编辑过程与VC 差不多。WIN32  窗口程序主要分三个部分:

   1.窗口过程,用于处理窗口接受到的各种消息。在WIN32汇编中窗口过程的函数格式比较固定 ,主要有三个参数:hWnd:窗口句柄 uMsg:消息类型,wparam,lparam:与消息有关的·

_ProcWinMain	proc	hWnd,uMsg,wParam,lParam

_ProcWinWain    endp
           

2.注册窗口类,填充 WNDCLASSEX 结构,使用CreateWindowEx创建窗口。

3. 死循环创建消息循环。GetMessage, translateMessage,DisPatchMessage  

继续阅读