Sie sind auf Seite 1von 11

td_win32asm_012.

asm
;==============================================================================
;
Test Department's WINDOWS 32 BIT x86 ASSEMBLY example's
012
;==============================================================================
;==============================================================================
; ==> Part 012 : Trackbar control class (Slider)
;-----------------------------------------------------------------------------; Bonjour,
; What should I say, controls are us, laughing ...
;
; Hint: Not all controls send a NM_CLICK message (for example this trackbar) !
;
;
; cya
;
; TD
td@crahkob.com
;
;==============================================================================
; Assembler directives
;-----------------------------------------------------------------------------.386
; the processor our program want run on
.Model Flat ,StdCall
; always the same for Win9x (32 Bit)
option casemap:none
; case sensitive !!!
;==============================================================================
; Include all files where API functins resist you want use, set correct path
;-----------------------------------------------------------------------------include D:\Masm32\include\windows.inc
includelib kernel32.lib
includelib user32.lib
includelib comctl32.lib
includelib gdi32.lib
;==============================================================================
; Declaration of used API functions,take a look into WIN32.HLP and *.inc files
;-----------------------------------------------------------------------------GetModuleHandleA
PROTO :DWORD
MessageBoxA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
InitCommonControls
PROTO
LoadIconA
PROTO :DWORD,:DWORD
LoadCursorA
PROTO :DWORD,:DWORD
RegisterClassExA
PROTO :DWORD
CreateSolidBrush
PROTO :DWORD
CreateWindowExA
PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,
:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ShowWindow
PROTO :DWORD,:DWORD
UpdateWindow
PROTO :DWORD
GetMessageA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
TranslateMessage
PROTO :DWORD
DispatchMessageA
PROTO :DWORD
PostQuitMessage
PROTO :DWORD
DefWindowProcA
PROTO :DWORD,:DWORD,:DWORD,:DWORD
Page 1

ExitProcess
DestroyWindow
DeleteObject
InvalidateRect
SendMessageA
LoadBitmapA
GetClientRect
SetFocus

PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO
PROTO

td_win32asm_012.asm
:DWORD
:DWORD
:DWORD
:DWORD,:DWORD,:DWORD
:DWORD,:DWORD,:DWORD,:DWORD
:DWORD,:DWORD
:DWORD,:DWORD
:DWORD

;==============================================================================
; .const = the constants area starts here,constants are defined and fixed
;-----------------------------------------------------------------------------.const
; - Parameter MAIN WINDOW CallBack Procedure ( API=RegisterClassExA ) WP1_CallBack
equ [ebp+4]
;return address
WP1_hWnd
equ [ebp+8]
;handle of window who receives message
WP1_uMsg
equ [ebp+12]
;the message number
WP1_wParam
equ [ebp+16]
;extra info about the message
WP1_lParam
equ [ebp+20]
;extra info about the message
;==============================================================================
; .Data = the data area starts here, datas are defined but not fixed
;-----------------------------------------------------------------------------.Data
ClassName
db "TDClass_TabControl",0
;name windows class
WindowName
db "Trackbar Control example",0 ;titel bar
TrackbarClassName
db "msctls_trackbar32",0
;predefined class
StaticClassName
db "STATIC",0
;predefined class
align 4
Xpos_org
Ypos_org
Xpos_max
Ypos_max

dd
dd
dd
dd

nFrames

dd 9

180h
180h
320
200

align 4
; - WndClassEx Structure cbSize
dd 0h
style
dd 0h
lpfnWndProc
dd 0h
cbclsExtra
dd 0h
cbWndExtra
dd 0h
hInstance
dd 0h
hIcon
dd 0h
hcursor
dd 0h
hbrBackground
dd 0h
lpszMenuName
dd 0h
lpszClassName
dd 0h
hIconSm
dd 0h

;X orgin point window


;Y orgin point window
;max. window width
;max. window height
;max. number of frames to display

;size in bytes of this structure


;window style
;address of user proc function
;extra bytes to allocate set to 0
;extra bytes class directive, rc file
;program handle(API=GetModuleHandleA)
;handle of icon (API=LoadIconA)
;handle of cursor (API=LoadCursor)
;background color, 0=transparent
;name of menu class in resource file
;name of windows this window class
;iconhandle 0=search in resource file

; - Msg Structure Page 2

hWnd
message
wParam
lParam
time
xpt
ypt

dd
dd
dd
dd
dd
dd
dd

0h
0h
0h
0h
0h
0h
0h

td_win32asm_012.asm
;handle of window who receives message
;the message number
;extra info about the message
;extra info about the message
;time the message was posted
;cursor x-position, POINT struc
;cursor x-position, POINT struc

;==============================================================================
; .Data? = the data? area starts here, not defined and not fixed
;-----------------------------------------------------------------------------.data?
align 4
hWnd_trackbar
dd ?
;handle tab control window
hWnd_static
dd ?
;handle static window
hImg_static
dd ?
;handle static image
; - RECT Structure rc_main_left
rc_main_top
rc_main_right
rc_main_bottom

dd
dd
dd
dd

?
?
?
?

;RECT,
;RECT,
;RECT,
;RECT,

x-coordinate
y-coordinate
x-coordinate
y-coordinate

upper-left corner
upper-left corner
lower-right corner
lower-right corner

;==============================================================================
; .CODE = our code area starts here
Main=label of our program code
;-----------------------------------------------------------------------------.Code
Main:
;==============================================================================
; Always get your program ID first (API=GetModuleHandleA)
;-----------------------------------------------------------------------------push
0h
;lpModuleHandle, 0=get program handle
call
GetModuleHandleA
;- API Function mov
hInstance,eax
;return value in eax=handle of program
;==============================================================================
; Trackbar control class seems to be part of common controls, force to init...
;-----------------------------------------------------------------------------call
InitCommonControls
;- API Function ;==============================================================================
; The API function "RegisterClassExA" registers a window class
; This API needs a "WNDCLASSEX" structure so we fill it with correct values
;-----------------------------------------------------------------------------mov
cbSize,30h
;size in bytes of WNDCLASSEX structure
mov
style,3h
;style, CS_HREDRAW, CS_VREDRAW
mov
lpfnWndProc,OFFSET WP1
;address of user lpfnWndProc function
mov
cbclsExtra,0h
;extra bytes to allocate set to 0
mov
cbWndExtra,0h
;class directive in rc file
mov
hbrBackground,5
;handle brush,backg. color,COLOR_WINDOW
mov
lpszMenuName,0h
;menu name in resource file
mov
lpszClassName,OFFSET ClassName ;name of windows class
Page 3

td_win32asm_012.asm
;-----------------------------------------------------------------------------; API "LoadIconA" loads an icon defined in the resource file and store the
; handle in the "WNDCLASSEX" structure
;-----------------------------------------------------------------------------push
1000
;icon-string or icon resource id
push
hInstance
;our program handle
call
LoadIconA
;- API Function mov
hIcon,eax
;handle of newly loaded icon
mov
hIconSm,eax
;iconhandle 0=search in rc file
;-----------------------------------------------------------------------------; API "LoadCursorA" loads a default system cursor, in this case we must set
; hInstance to 0 and lpCursorName to a default system cursor value, here 32512
; Then we store the cursor handle in the "WNDCLASSEX" structure
;-----------------------------------------------------------------------------push
32512
;lpCursorName, default value in dezimal
push
0h
;hInstance, 0=default system cursor
call
LoadCursorA
;- API Function mov
hcursor,eax
;store handle of the cursor
;-----------------------------------------------------------------------------; Now, after filled the "WNDCLASSEX" structure we call API "RegisterClassEx"
;-----------------------------------------------------------------------------push
OFFSET cbSize
;pointer to WNDCLASSEX structure
call
RegisterClassExA
;- API Function ;==============================================================================
; API "CreateWindowExA" creates an overlapped, pop-up, or child window with an
; extended style. The return value in EAX is the handle of the new window.
; This API sends a WM_CREATE message to the window procedure (WP1).
;-----------------------------------------------------------------------------push
0h
;lpParam, extra pointer data 0=no data
push
hInstance
;hInstance, handle of our program
push
0h
;hMenu, handle window menu 0=class menu
push
0h
;hWndParent, handle parent window 0=no
push
Ypos_max
;intnHeight, window height pixel
push
Xpos_max
;intnWidth, window width pixel
push
Ypos_org
;inty, vertical position window
push
Xpos_org
;intx, horizontal position window
push
02CA0000h
;dwStyle, look into WIN32.HLP and window.inc
push
OFFSET WindowName
;lpWindowName, pointer to window name
push
OFFSET ClassName
;lpClassName, pointer to class name
push
0h
;dwExStyle, extra window style 0=no
call
CreateWindowExA
;- API Function mov
hWnd,eax
;hwnd, return value=handle of window
;==============================================================================
; API "ShowWindow" function sets the specified window's show state.
;-----------------------------------------------------------------------------push
1h
;nCmdShow, show state 1=SW_SHOWNORMAL
push
hWnd
;hwnd, handle of window
call
ShowWindow
;- API Function ;==============================================================================
; API "UpdateWindow" updates the area of the specified window by sending a
Page 4

td_win32asm_012.asm
; WM_PAINT message to the window if the window's update region is not empty.
;-----------------------------------------------------------------------------push
hWnd
;hwnd, handle of window
call
UpdateWindow
;- API Function ;==============================================================================
; API "GetMessageA" retrieves a message & places it in the specified structure.
;-----------------------------------------------------------------------------LoopGetMessage:
push
0h
;wMsgFilterMax, highest message value
push
0h
;wMsgFilterMin, lowest message value
push
0h
;hWnd, handle of window who gets msg.
push
OFFSET hWnd
;lpMsg, pointer to MSG structure
call
GetMessageA
;- API Function cmp
eax,0h
;check if return value=0 (exit)
je
ExitPrg
;if return value is 0 goto LABEL
;==============================================================================
; API "TranslateMessage" translates key code into ASCII character messages
;-----------------------------------------------------------------------------push
OFFSET hWnd
;lpMSG, pointer to msg structure
call
TranslateMessage
;- API Function - keyboard code
;==============================================================================
; API "DispatchMessageA" function dispatches a message to a window procedure.
;-----------------------------------------------------------------------------push
OFFSET hWnd
;lpMSG, pointer to msg structure
call
DispatchMessageA
;- API Function jmp
LoopGetMessage
;check for message again, goto LABEL
ExitPrg:
;==============================================================================
; Next we terminate our program (API=ExitProcess)
;-----------------------------------------------------------------------------push
hInstance
;push our programm handle to exit
call
ExitProcess
;- API Function ;##############################################################################
; The Window Procedure lpfnWndProc (API=RegisterClassExA) for this window.
;-----------------------------------------------------------------------------WP1:
push
ebp
;create stack frame
mov
ebp,esp
;
pushad
;push all register to the stack
mov
eax,WP1_uMsg
;move the message number to eax
;==============================================================================
; WM_CREATE (value=01h) message received ?
;-----------------------------------------------------------------------------WP1_uMsg_01h:
cmp
eax,1h
;check if WM_CREATE message recieved
jne
WP1_uMsg_02h
;if not goto label
;-----------------------------------------------------------------------------Page 5

td_win32asm_012.asm
; API "GetClientRect" retrieves the coordinates of a window's client area.
;-----------------------------------------------------------------------------push
OFFSET rc_main_left
;lpRect, address structure client coordinates
push
WP1_hWnd
;hWnd, handle of window
call
GetClientRect
;- API Function ;-----------------------------------------------------------------------------; API "CreateWindowExA" creates a window with a predefined class name (STATIC).
;-----------------------------------------------------------------------------push
0h
;lpParam, extra pointer data 0=no data
push
hInstance
;hInstance, handle of our program
push
200h
;hMenu, the child-window ID
push
WP1_hWnd
;hWndParent, handle parent window 0=no
push
88h
;intnHeight, window height pixel
push
rc_main_right
;intnWidth, window width pixel
push
rc_main_top
;inty, vertical position window
push
rc_main_left
;intx, horizontal position window
push
5400020Eh
;dwStyle, SS_CENTERIMAGE=200h, SS_BITMAP = 0Eh
push
0h
;lpWindowName, pointer to window name
push
OFFSET StaticClassName ;lpClassName, pointer to class name
push
0h
;dwExStyle, look WIN32.HLP + windows.inc
call
CreateWindowExA
;- API Function mov
hWnd_static,eax
;hwnd, store handle of window
;-----------------------------------------------------------------------------; API "LoadBitmapA" loads a bitmap from the resource file
;-----------------------------------------------------------------------------push
2000
;lpBitmapName, lowest bitmap resource ID
push
hInstance
;hInstance, handle of modul instance
call
LoadBitmapA
;- API Function mov
hImg_static,eax
;hObject, handle of graphic object
;-----------------------------------------------------------------------------; API "SendMessage" sends the specified message to a window.
;-----------------------------------------------------------------------------push
hImg_static
;push handle of the bitmap
push
0h
;wParam, fImageType, IMAGE_BITMAP=0h
push
172h
;uMsg, STM_SETIMAGE=172h
push
hWnd_static
;hwnd, handle of destination window
call
SendMessageA
;- API Function ;-----------------------------------------------------------------------------; API "CreateWindowExA" creates a window with a predefined class name (Trackbar)
;-----------------------------------------------------------------------------push
0h
;lpParam, extra pointer data 0=no data
push
hInstance
;hInstance, handle of our program
push
300h
;hMenu, the child-window ID
push
WP1_hWnd
;hWndParent, handle parent window 0=no
push
14h
;intnHeight, window height pixel
push
118h
;intnWidth, window width pixel
push
90h
;inty, vertical position window
push
10h
;intx, horizontal position window
push
54000101h
;dwStyle,TBS_TOOLTIPS=100h,TBS_ENABLESELRANGE=20h,
;TBS_HORZ=0h,TBS_AUTOTICKS=1h,TBS_BOTH=8h,TBS_TOP=4h
push
0h
;lpWindowName, pointer to window name
push
OFFSET TrackbarClassName;lpClassName, pointer to class name
Page 6

td_win32asm_012.asm
push
0h
;dwExStyle,
call
CreateWindowExA
;- API Function mov
hWnd_trackbar,eax
;hwnd, store handle of window
;-----------------------------------------------------------------------------; API "SendMessage" sends the specified message to a window.
;-----------------------------------------------------------------------------mov
eax,nFrames
;max. number of frames to display
shl
eax,16
;mov into high word
push
eax
;lParam, min./max. position for the slider
push
1h
;wParam,fRedraw,TRUE=redraw trackbar after set
push
1030
;uMsg, TBM_SETRANGE, WM_USER(0400h)+6
push
hWnd_trackbar
;hwnd, handle of destination window
call
SendMessageA
;- API Function ;-----------------------------------------------------------------------------; API "SendMessage" sends the specified message to a window.
;-----------------------------------------------------------------------------push
1h
;lParam, lPageSize, new page size
push
0h
;wParam,
push
1045
;uMsg, TBM_SETPAGESIZE, WM_USER(0400h)+21
push
hWnd_trackbar
;hwnd, handle of destination window
call
SendMessageA
;- API Function ;-----------------------------------------------------------------------------; API "SendMessage" sends the specified message to a window.
;-----------------------------------------------------------------------------push
0h
;lParam,
push
0h
;wParam,fLocation,to display tooltip,TBTS_TOP=0h
push
1055
;uMsg, TBM_SETTIPSIDE (WM_USER)+31
push
hWnd_trackbar
;hwnd, handle of destination window
call
SendMessageA
;- API Function ;-----------------------------------------------------------------------------; API "SetFocus" sets the keyboard focus to the specified window.
;-----------------------------------------------------------------------------push
hWnd_trackbar
;hwnd, handle of window to receive focus
call
SetFocus
;- API Function jmp
WP1_return
;==============================================================================
; WM_DESTROY (value=02h) message received ?
;-----------------------------------------------------------------------------WP1_uMsg_02h:
cmp
eax,2h
;check if value=2h (WM_DESTROY)
jne
WP1_uMsg_0Fh
;if not 2h go to LABEL
xyz:
call
My_CleanSystem
;- SubRoutine ;-----------------------------------------------------------------------------; API "PostQuitMessage" indicates to Windows a request to terminate
;-----------------------------------------------------------------------------push
0h
;nExitCode, exit code=wParam
call
PostQuitMessage
;- API Function popad
;pop all register back from stack
xor
eax,eax
;set eax to 0 to exit our program
mov
esp,ebp
;delete stack frame
pop
ebp
;
Page 7

td_win32asm_012.asm
ret

10h

;return and clear stack

;==============================================================================
; WM_PAINT (value=0Fh) message, used to repaint the window area
;-----------------------------------------------------------------------------WP1_uMsg_0Fh:
cmp
eax,0Fh
;check if WM_PAINT message recieved
jne
WP1_uMsg_4Eh
;if not goto label
jmp

WP1_return

;==============================================================================
; WM_NOTIFY (value=4Eh) message, gives us a pointer to a NMHDR structure.
;-----------------------------------------------------------------------------WP1_uMsg_4Eh:
cmp
eax,4Eh
;check if WM_NOTIFY message recieved
jne
WP1_uMsg_7Eh
;if not goto label
mov
ebx,WP1_lParam
;pointer to struc, given by windows !
;-----------------------------------------------------------------------------; The NMHDR structure contains information about a notification message.
; The POINTER to this structure is specified as lParam member of WM_NOTIFY.
; This POINTER is given to us with the WM_NOTIFY message by Windows.
; - NMHDR structure ; hwndFrom
= [WP1_lParam+00]
;handle to control sending message
; idFrom
= [WP1_lParam+04]
;identifier of control sending message
; code
= [WP1_lParam+08]
;notification code
; - NMCUSTOMDRAW structure ; hdr
= placeholder NMHDR ;required for all WM_NOTIFY messages
; dwDrawStage
= [WP1_lParam+12]
;current drawing stage
; hdc
= [WP1_lParam+16]
;handle to the control's device context
; rc
= [WP1_lParam+20]
;RECT structure
;
= [WP1_lParam+24]
;
;
= [WP1_lParam+28]
;
;
= [WP1_lParam+32]
;
; dwItemSpec
= [WP1_lParam+36]
;item number
; uItemState
= [WP1_lParam+40]
;current item state
; lItemlParam
= [WP1_lParam+44]
;application-defined item data
;-----------------------------------------------------------------------------mov
eax,[ebx+0]
;get the handle of the control sending msg.
cmp
eax,hWnd_trackbar
;is control = trackbar control ?
jne
WP1_return
mov
eax,[ebx+8]
;move code ( NMHDR structure ) into eax
cmp
eax,0FFFFFFF4h
;is code NM_CUSTOMDRAW, NM_FIRST-12
jne
WP1_return
;if not skip
jmp

WP1_return

;==============================================================================
; WM_DISPLAYCHANGE (value=7Eh) message, used to repaint the window area
;-----------------------------------------------------------------------------WP1_uMsg_7Eh:
cmp
eax,7Eh
;check if WM_DISPLAYCHANGE message recieved
jne
WP1_uMsg_111h
;if not goto label
Page 8

td_win32asm_012.asm
;-----------------------------------------------------------------------------; API "InvalidateRect" adds a rectangle to specified window's update region.
; This function performs a WM_PAINT message.
;-----------------------------------------------------------------------------push
0h
;bErase, erase-background flag
push
0h
;lpRect, rect structure, 0h=client area
push
WP1_hWnd
;hWnd, handle window update region
call
InvalidateRect
;- API Function jmp
WP1_return
;==============================================================================
; WM_COMMAND (value=111h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_111h:
cmp
eax,111h
;check if WM_COMMAND message recieved
jne
WP1_uMsg_112h
;if not goto label
jmp

WP1_return

;==============================================================================
; WM_SYSCOMMAND (value=112h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_112h:
cmp
eax,112h
;check if WM_COMMAND message recieved
jne
WP1_uMsg_114h
;if not goto label
mov
eax,WP1_wParam
;extra info about the message
cmp
eax,0F060h
;SC_CLOSE=0F060h received ?
jne
WP1_return
;
call
My_CleanSystem
;- SubRoutine jmp
WP1_return
;==============================================================================
; WM_HSCROLL (value=114h) message recieved ?
;-----------------------------------------------------------------------------WP1_uMsg_114h:
cmp
eax,114h
;check if WM_HSCROLL message recieved
jne
WP1_return
;if not goto label
mov
eax,WP1_lParam
;hwndScrollBar,identifies the scroll bar control
cmp
eax,hWnd_trackbar
;is it our trackbar ?
jne
WP1_return
;if not skip
;-----------------------------------------------------------------------------; API "DeleteObject" deletes a bitmap, the specified handle is no longer valid.
;-----------------------------------------------------------------------------push
hImg_static
;hObject, handle object
call
DeleteObject
;- API Function ;-----------------------------------------------------------------------------; API "SendMessage" sends the specified message to a window.
;-----------------------------------------------------------------------------push
0h
;lParam,
push
0h
;wParam,
push
1024
;uMsg, TBM_GETPOS, WM_USER(0400h)
push
hWnd_trackbar
;hwnd, handle of destination window
call
SendMessageA
;- API Function Page 9

td_win32asm_012.asm
;-----------------------------------------------------------------------------; API "LoadBitmapA" loads a bitmap from the resource file
;-----------------------------------------------------------------------------add
eax,2000
;lowest bitmap resource ID !
push
eax
;lpBitmapName or resource id
push
hInstance
;hInstance, handle of modul instance
call
LoadBitmapA
;- API Function mov
hImg_static,eax
;hObject, handle of graphic object
;-----------------------------------------------------------------------------; API "SendMessage" sends the specified message to a window.
;-----------------------------------------------------------------------------push
hImg_static
;push handle of the bitmap
push
0h
;wParam, fImageType, IMAGE_BITMAP=0h
push
172h
;uMsg, STM_SETIMAGE=172h
push
hWnd_static
;hwnd, handle of destination window
call
SendMessageA
;- API Function jmp
WP1_return
;==============================================================================
; API "DefWindowProcA" calls the window procedure to provide default processing
; for any window messages that an application does not process.
; This function ensures that every message is processed.
; It is called with the same parameters received by the window procedure.
;-----------------------------------------------------------------------------WP1_return:
popad
;pop all register from stack
push
WP1_lParam
;extra info about the message
push
WP1_wParam
;extra info about the message
push
WP1_uMsg
;the message number
push
WP1_hWnd
;handle of window who receives message
call
DefWindowProcA
;- API Function mov
esp,ebp
;delete stack frame
pop
ebp
ret
10h
;return and clear stack
;==============================================================================
; Leaving the window procedure returning TRUE (1) or FALSE (0) in EAX.
;-----------------------------------------------------------------------------WP1_return_0:
popad
;pop all register from stack
mov
eax,0h
;set EAX to FALSE (0)
mov
esp,ebp
;delete stack frame
pop
ebp
ret
10h
;return and clear stack
WP1_return_1:
popad
;pop all register from stack
mov
eax,1h
;set EAX to TRUE (1)
mov
esp,ebp
;delete stack frame
pop
ebp
ret
10h
;return and clear stack
;##############################################################################
;******************************************************************************
Page 10

td_win32asm_012.asm
; My own subroutine(s) for a compacter code resist here ...
;-----------------------------------------------------------------------------My_CleanSystem:
;-----------------------------------------------------------------------------; API "DeleteObject" deletes a logical pen, brush, font, bitmap, region, or
; palette, freeing all system resources associated with the object.
; After the object is deleted, the specified handle is no longer valid.
;-----------------------------------------------------------------------------push
hImg_static
;hObject, handle object
call
DeleteObject
;- API Function ret
;******************************************************************************
;==============================================================================
; end Main = end of our program code
;-----------------------------------------------------------------------------end Main
;end of our program code, entry point
;==============================================================================
; To create the exe file use this commands with your Microsoft Assembler/Linker
;-----------------------------------------------------------------------------; ml.exe /c /coff td_win32asm_012.asm
;asm command
; rc.exe /v rsrc.rc
;rc command
; cvtres.exe /machine:ix86 rsrc.res
; link.exe /subsystem:windows td_win32asm_012.obj rsrc.obj
;link command
;==============================================================================

Page 11

Das könnte Ihnen auch gefallen