Netwide Assembler (NASM) >> Assembly (x86) >> Win32 API

Set ScrollBar


參考資訊:
1. nasm
2. masm32

當視窗的可視區域(如:300x300像素)小於顯示圖片大小(如:640x480像素)時,這時可以使用Windows視窗元件ScrollBar,用來做顯示位置調整的動作,ScrollBar元件有垂直和水平兩種方向並且提供視窗事件回報機制(WM_VSCROLL、WM_HSCROLL),因此,這裡的ScrollBar元件並不能夠自動幫忙做顯示位置調整的動作,取而代之的是,在收到WM_VSCROLL、WM_HSCROLL事件時,使用者必須自己決定哪些東西要顯示在視窗的可視區域上

main.asm

    [bits 32]
    global _start

    extern _sprintf
    extern _SetTimer@16
    extern _KillTimer@8
    extern _ExitProcess@4
    extern _GetMessageA@16
    extern _MessageBoxA@16
    extern _SetScrollPos@16
    extern _DestroyWindow@4
    extern _SetScrollRange@20
    extern _SetWindowTextA@8
    extern _SetWindowLongA@12
    extern _CallWindowProcA@20
    extern _GetCommandLineA@0
    extern _CreateWindowExA@48
    extern _PostQuitMessage@4
    extern _GetModuleHandleA@4
    extern _DispatchMessageA@4

    %define ARG1            8h
    %define ARG2            0ch
    %define ARG3            10h
    %define ARG4            14h
    %define sprintf         _sprintf
    %define SetTimer        _SetTimer@16
    %define KillTimer       _KillTimer@8
    %define GetMessage      _GetMessageA@16
    %define MessageBox      _MessageBoxA@16
    %define ExitProcess     _ExitProcess@4
    %define SetScrollPos    _SetScrollPos@16
    %define DestroyWindow   _DestroyWindow@4
    %define SetScrollRange  _SetScrollRange@20
    %define SetWindowText   _SetWindowTextA@8
    %define SetWindowLong   _SetWindowLongA@12
    %define CallWindowProc  _CallWindowProcA@20
    %define GetCommandLine  _GetCommandLineA@0
    %define CreateWindowEx  _CreateWindowExA@48
    %define PostQuitMessage _PostQuitMessage@4
    %define GetModuleHandle _GetModuleHandleA@4
    %define DispatchMessage _DispatchMessageA@4
    
    struc POINT
        .x: resd 1
        .y: resd 1
        .SIZE:
    endstruc
     
    struc MSG
        .hwnd:    resd 1
        .message: resd 1
        .wParam:  resd 1
        .lParam:  resd 1
        .time:    resd 1
        .pt:      resb POINT.SIZE
        .SIZE:
    endstruc

    SB_LINEUP           equ 0
    SB_LINELEFT         equ 0
    SB_LINEDOWN         equ 1
    SB_LINERIGHT        equ 1
    SB_PAGEUP           equ 2
    SB_PAGELEFT         equ 2
    SB_PAGEDOWN         equ 3
    SB_PAGERIGHT        equ 3
    SB_VERT             equ 1
    GWL_WNDPROC         equ -4
    SW_SHOWNORMAL       equ 1h
    WC_DIALOG           equ 8002h
    WM_VSCROLL          equ 115h
    WM_TIMER            equ 113h
    WM_MOUSEMOVE        equ 200h
    WM_SYSKEYDOWN       equ 104h
    WM_KEYDOWN          equ 100h
    WM_DESTROY          equ 2h
    WM_CLOSE            equ 10h
    WS_VSCROLL          equ 200000h
    WS_OVERLAPPED       equ 0h
    WS_CAPTION          equ 0c00000h
    WS_SYSMENU          equ 80000h
    WS_THICKFRAME       equ 40000h
    WS_MINIMIZEBOX      equ 20000h
    WS_MAXIMIZEBOX      equ 10000h
    WS_VISIBLE          equ 10000000h
    WS_EX_LEFT          equ 00000000h
    WS_OVERLAPPEDWINDOW equ WS_OVERLAPPED | \
                            WS_CAPTION | \
                            WS_SYSMENU | \
                            WS_THICKFRAME | \
                            WS_MINIMIZEBOX | \
                            WS_MAXIMIZEBOX
     
    section .drectve info
_entry    db "/entry:start "
_gdi32    db "/defaultlib:c:\\masm32\\lib\\gdi32.lib "
_user32   db "/defaultlib:c:\\masm32\\lib\\user32.lib "
_msvcrt   db "/defaultlib:c:\\masm32\\lib\\msvcrt.lib "
_kernel32 db "/defaultlib:c:\\masm32\\lib\\kernel32.lib "
      
    segment .data
hWin        dd 0
szCaption   db "main",0
hInstance   dd 0
defWndProc  dd 0
CommandLine dd 0
SLUP        db "LINE++",0
SLDN        db "LINE--",0
SPUP        db "PAGE++",0
SPDN        db "PAGE--",0

msg istruc MSG
    at .hwnd,    dd 0
    at .message, dd 0
    at .wParam,  dd 0
    at .lParam,  dd 0
    at .time,    dd 0
    at .pt,      dd 0, 0
iend
     
    segment .text
WndProc:
    push ebp
    mov ebp, esp
    
    cmp dword [ebp + ARG2], WM_VSCROLL
    je .handle_vscroll
    cmp dword [ebp + ARG2], WM_CLOSE
    je .handle_close
    cmp dword [ebp + ARG2], WM_DESTROY
    je .handle_destroy
    jmp .handle_default

.handle_vscroll:
    cmp word [ebp + ARG3], SB_LINEUP
    je .handle_sb_line_up
    cmp word [ebp + ARG3], SB_LINEDOWN
    je .handle_sb_line_down
    cmp word [ebp + ARG3], SB_PAGEUP
    je .handle_sb_page_up
    cmp word [ebp + ARG3], SB_PAGEDOWN
    je .handle_sb_page_down
    jmp .finish

.handle_sb_line_up:
    push SLUP
    push dword [ebp + ARG1]
    call SetWindowText
    jmp .finish

.handle_sb_line_down:
    push SLDN
    push dword [ebp + ARG1]
    call SetWindowText
    jmp .finish

.handle_sb_page_up:
    push SPUP
    push dword [ebp + ARG1]
    call SetWindowText
    jmp .finish

.handle_sb_page_down:
    push SPDN
    push dword [ebp + ARG1]
    call SetWindowText
    jmp .finish

.handle_close:
    push dword [ebp + ARG1]
    call DestroyWindow
    xor eax, eax
    jmp .finish
    
.handle_destroy:
    push 0
    call PostQuitMessage
    xor eax, eax
    jmp .finish
    
.handle_default:
    push dword [ebp + ARG4]
    push dword [ebp + ARG3]
    push dword [ebp + ARG2]
    push dword [ebp + ARG1]
    push dword [defWndProc]
    call CallWindowProc
    
.finish:
    leave
    ret 16
    
WinMain:
    push ebp
    mov ebp, esp
     
    push 0
    push 0
    push 0
    push 0
    push 300
    push 300
    push 0
    push 0
    push WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_VSCROLL
    push szCaption
    push WC_DIALOG
    push WS_EX_LEFT
    call CreateWindowEx
    mov [hWin], eax
    
    push WndProc
    push GWL_WNDPROC
    push dword [hWin]
    call SetWindowLong
    mov [defWndProc], eax
 
    push 0
    push 100
    push 0
    push SB_VERT
    push dword [hWin]
    call SetScrollRange

    push 1
    push 50
    push SB_VERT
    push dword [hWin]
    call SetScrollPos
.loop:
    push 0
    push 0
    push 0
    push msg
    call GetMessage
    cmp eax, 0
    je .exit
     
    push msg
    call DispatchMessage
    jmp .loop
     
.exit:
    mov eax, [msg + MSG.wParam]
    leave
    ret 16
     
_start:
    push 0
    call GetModuleHandle
    mov [hInstance], eax
     
    call GetCommandLine
    mov [CommandLine], eax
     
    push SW_SHOWNORMAL
    push dword [CommandLine]
    push 0
    push dword [hInstance]
    call WinMain
     
    push eax
    call ExitProcess

Line 135~168:處理ScrollBar訊息並且顯示在視窗標題
Line 206:WS_VSCROLL是垂直的ScrollBar,WS_HSCROLL則是水平的ScrollBar
Line 219~224:設定ScrollBar最大的範圍
Line 226~230:設定ScrollBar目前的位置

Makefile

export WINEPREFIX=/home/user/.wine_amd64

TARGET=main
MYWINE=box86 wine
NASM32=/home/user/.wine_amd64/drive_c/nasm
MASM32=/home/user/.wine_amd64/drive_c/masm32

all:
	${MYWINE} ${NASM32}/bin/nasm.exe -fwin32 ${TARGET}.asm
	${MYWINE} ${MASM32}/bin/link.exe /SUBSYSTEM:WINDOWS /MERGE:.rdata=.text ${TARGET}.obj

run:
	${MYWINE} ${TARGET}.exe

clean:
	rm -rf ${TARGET}.exe ${TARGET}.obj

編譯、執行

$ make
$ make run


返回上一頁