程式語言 - Netwide Assembler (NASM) - Win32 API (NASMX) - Set Scrollbar



參考資訊:
https://masm32.com/board/index.php
https://www.nasm.us/xdoc/2.13rc9/html/nasmdoc0.html

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

main.asm

    %include "c:\\nasmx\\inc\\nasmx.inc"
    %include "c:\\nasmx\\inc\\win32\\msvcrt.inc"
    %include "c:\\nasmx\\inc\\win32\\windows.inc"
    %include "c:\\nasmx\\inc\\win32\\user32.inc"
    %include "c:\\nasmx\\inc\\win32\\kernel32.inc"
   
    entry start
   
    section .data
hWin         dd 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
szCaption    declare(NASMX_TCHAR) NASMX_TEXT("main"),0
    
NASMX_ISTRUC msg, MSG
    NASMX_AT hwnd,    NULL
    NASMX_AT message, NULL
    NASMX_AT wParam,  NULL
    NASMX_AT lParam,  NULL
    NASMX_AT time,    NULL
    NASMX_ISTRUC pt, POINT
        NASMX_AT x, 0
        NASMX_AT y, 0
    NASMX_IENDSTRUC
NASMX_IENDSTRUC
    
    section .text
proc WndProc, dword hWnd, uint_t uMsg, dword wParam, dword lParam
    locals none
 
    switch dword [argv(.uMsg)]
    case dword WM_VSCROLL
        mov eax, [argv(.wParam)]
        and eax, 0ffffh
        switch eax
        case SB_LINEUP
            invoke SetWindowText, [argv(.hWnd)], SLUP
            break
        case SB_LINEDOWN
            invoke SetWindowText, [argv(.hWnd)], SLDN
            break
        case SB_PAGEUP
            invoke SetWindowText, [argv(.hWnd)], SPUP
            break
        case SB_PAGEDOWN
            invoke SetWindowText, [argv(.hWnd)], SPDN
            break
        endswitch
        break
    case dword WM_CLOSE
        invoke DestroyWindow, [argv(.hWnd)]
        break
    case dword WM_DESTROY
        invoke PostQuitMessage, 0
        break
    default
        invoke CallWindowProc, [defWndProc], [argv(.hWnd)], [argv(.uMsg)], [argv(.wParam)], [argv(.lParam)]
    endswitch
endproc
    
proc WinMain, dword hInst, dword hPrevInst, dword CmdLine, dword CmdShow
    locals none
     
    invoke CreateWindowEx, WS_EX_LEFT, WC_DIALOG, szCaption, \
        WS_OVERLAPPEDWINDOW | WS_VISIBLE | 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

    do
        invoke DispatchMessage, msg
        invoke GetMessage, msg, NULL, NULL, NULL
    while eax, !=, 0
     
    mov eax, dword [msg + MSG.wParam]
endproc
     
proc start, dword argc, dword argv
    locals none
     
    invoke GetModuleHandle, NULL
    mov [hInstance], eax
     
    invoke GetCommandLine, NULL
    mov [CommandLine], eax
     
    invoke WinMain, [hInstance], NULL, [CommandLine], SW_SHOWNORMAL
    invoke ExitProcess, NULL
endproc

Line 37~53:處理Scrollbar訊息並且顯示在視窗標題
Line 70:WS_VSCROLL是垂直的Scrollbar,WS_HSCROLL則是水平的Scrollbar
Line 76:設定Scrollbar最大的範圍
Line 77:設定Scrollbar目前的位置

完成