FASM >> Assembly (x86) >> Win32 API
Set ScrollBar
參考資訊:
1. fasm
當視窗的可視區域(如:300x300像素)小於顯示圖片大小(如:640x480像素)時,這時可以使用Windows視窗元件ScrollBar,用來做顯示位置調整的動作,ScrollBar元件有垂直和水平兩種方向並且提供視窗事件回報機制(WM_VSCROLL、WM_HSCROLL),因此,這裡的ScrollBar元件並不能夠自動幫忙做顯示位置調整的動作,取而代之的是,在收到WM_VSCROLL、WM_HSCROLL事件時,使用者必須自己決定哪些東西要顯示在視窗的可視區域上
main.asm
format PE GUI 4.0 entry start include "c:\fasm\include\win32a.inc" section ".idata" import data readable writeable library user, "user32.dll", kernel, "kernel32.dll" import user, \ wsprintf, "wsprintfA", \ SetTimer, "SetTimer", \ KillTimer, "KillTimer", \ MessageBox, "MessageBoxA", \ GetMessage, "GetMessageA", \ SetScrollPos, "SetScrollPos", \ DestroyWindow, "DestroyWindow", \ SetWindowText, "SetWindowTextA", \ SetWindowLong, "SetWindowLongA", \ SetScrollRange, "SetScrollRange", \ CreateWindowEx, "CreateWindowExA", \ CallWindowProc, "CallWindowProcA", \ DispatchMessage, "DispatchMessageA", \ PostQuitMessage, "PostQuitMessage" import kernel, \ ExitProcess, "ExitProcess", \ GetCommandLine, "GetCommandLineA", \ GetModuleHandle, "GetModuleHandleA" section ".data" data readable writeable hWin dd 0 szCaption db "main",0 hInstance dd 0 CommandLine dd 0 defWndProc dd 0 SLUP db "LINE++",0 SLDN db "LINE--",0 SPUP db "PAGE++",0 SPDN db "PAGE--",0 section ".text" code readable executable proc WndProc hWnd, uMsg, wParam, lParam mov eax, [uMsg] cmp eax, WM_VSCROLL je .handle_vscroll cmp eax, WM_CLOSE je .handle_close cmp eax, WM_DESTROY je .handle_destroy invoke CallWindowProc, [defWndProc], [hWnd], [uMsg], [wParam], [lParam] jmp .finish .handle_vscroll: cmp word [wParam], SB_LINEUP je .handle_sb_line_up cmp word [wParam], SB_LINEDOWN je .handle_sb_line_down cmp word [wParam], SB_PAGEUP je .handle_sb_page_up cmp word [wParam], SB_PAGEDOWN je .handle_sb_page_down jmp .finish .handle_sb_line_up: invoke SetWindowText, [hWnd], SLUP jmp .finish .handle_sb_line_down: invoke SetWindowText, [hWnd], SLDN jmp .finish .handle_sb_page_up: invoke SetWindowText, [hWnd], SPUP jmp .finish .handle_sb_page_down: invoke SetWindowText, [hWnd], SPDN jmp .finish .handle_close: invoke DestroyWindow, [hWnd] xor eax, eax jmp .finish .handle_destroy: invoke PostQuitMessage, 0 xor eax, eax jmp .finish .finish: ret endp proc WinMain hInst, hPrevInst, CmdLine, CmdShow local msg:MSG invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szCaption, \ WS_OVERLAPPEDWINDOW or WS_VISIBLE or WS_VSCROLL, 0, 0, 300, 300, NULL, NULL, NULL, NULL mov [hWin], eax invoke SetWindowLong, [hWin], GWL_WNDPROC, WndProc mov [defWndProc], eax invoke SetScrollRange, [hWin], SB_VERT, 0, 100, FALSE invoke SetScrollPos, [hWin], SB_VERT, 50, TRUE @@: lea eax, [msg] invoke GetMessage, eax, NULL, 0, 0 cmp eax, 0 je @f lea eax, [msg] invoke DispatchMessage, eax jmp @b @@: mov eax, [msg.wParam] ret endp start: invoke GetModuleHandle, NULL mov [hInstance], eax invoke GetCommandLine mov [CommandLine], eax stdcall WinMain, [hInstance], NULL, [CommandLine], SW_SHOWNORMAL invoke ExitProcess, eax
Line 53~78:處理ScrollBar訊息並且顯示在視窗標題
Line 98:WS_VSCROLL是垂直的ScrollBar,WS_HSCROLL則是水平的ScrollBar
Line 104:設定ScrollBar最大的範圍
Line 105:設定ScrollBar目前的位置
Makefile
export WINEPREFIX=/home/user/.wine_amd64 TARGET=main MYWINE=box86 wine FASM32=/home/user/.wine_amd64/drive_c/fasm all: ${MYWINE} ${FASM32}/fasm.exe ${TARGET}.asm run: ${MYWINE} ${TARGET}.exe clean: rm -rf ${TARGET}.exe
編譯、執行
$ make $ make run