FASM win32 snippets

Create Timer Thread

	invoke  SetTimer, [hwnd], 1, 500, _proc

	proc    _proc	hWnd, IdEvent, uElapse, dwTime
		push	esi edi ebx
		invoke	InvalidateRect, [hwnd], NULL, FALSE ; trigger WM_PAINT
		pop	ebx edi esi
	ret

Dialog Template

	.data
		message	rb	0xff
		caption	rb	0xff
	.code

	start:  invoke	GetModuleHandle, 0
		invoke	DialogBoxParam, eax, 123, HWND_DESKTOP, DialogProc, 0
		or	eax, eax
		jz	exit
		invoke	MessageBox, HWND_DESKTOP, message, caption, MB_OK
	exit:	invoke	ExitProcess, 0

	proc	DialogProc hwnddlg, msg, wparam, lparam
		push	ebx esi edi
		cmp	[msg], WM_INITDIALOG
		je	wminit
		cmp	[msg], WM_COMMAND
		je	wmcmd
		cmp	[msg], WM_CLOSE
		je	wmclose
		xor	eax, eax
		jmp	finish
	wminit:	mov	eax, [hwnddlg]
		mov	[hwnd], eax
		jmp	done
	crfail:	invoke	ExitProcess, 1
	wmcmd:	jmp	done
	wmclose:invoke	EndDialog, [hwnd], 0
	done:	mov	eax, 1
	finish:	pop	edi esi ebx
		ret
	endp

	section '.rsrc' resource data readable
	 directory RT_DIALOG, dialogs
	 resource dialogs, 123, LANG_ENGLISH+SUBLANG_DEFAULT, demo
	 dialog demo, 'dialog template', 300, 300, 320, 175, WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME

	enddialog

Double Buffered GDI Drawing

	.data
		ps	PAINTSTRUCT
		memDC	dd	?
		backBMP	dd	?
		oldBMP	dd	?
		rc	RECT

		hdc	dd	?
	.code
	wmpaint:
		invoke	BeginPaint, [hwnd], ps
		mov	[hdc], eax
		invoke	CreateCompatibleDC, [hdc]
		mov	[memDC], eax
		invoke	CreateCompatibleBitmap, [hdc], 0xff, 0xff
		mov	[backBMP], eax
		invoke	SelectObject, [memDC], [backBMP]
		mov	[oldBMP], eax
		
		mov	[rc.left], 50
		mov	[rc.top], 50
		mov	[rc.right], 80
		mov	[rc.bottom], 80

		invoke	GetStockObject, WHITE_BRUSH
		invoke	FillREct, [memDC], rc, eax
		invoke	BitBlt, [hdc], 0, 0, 0xff, 0xff, [memDC], 0, 0, SRCCOPY
		invoke	SelectObject, [memDC], [oldBMP]
		invoke	DeleteObject, [backBMP]
		invoke	DeleteDC, [memDC]
		invoke	EndPaint, [hwnd], ps
		jmp	done

Listbox select

	wmcmd:	cmp	[wparam], LBN_SELCHANGE shl 16 + 1 ; 1 is id
		je	sel

	sel:	invoke	SendMessageA, [lparam], LB_GETCURSEL, 0, 0