;-scroll2.asm---------------------------------------------------------------- ; x86 - Dos & Win32 - Assembly Language Programming ; ; ; ; Written by: John A Lyons ; ; Email : sysop@megablast.8k.com ; ; Page : http://www.asmsource.8k.com/ ; ; Compiler : Masm32 v6.13 Microsoft Macro Assembler ; ; Date : 22-1-2001 ; ; Purpose : Shows how to use the vertical scrollbar and horizontal ; ; scrollbar. This is an updated version to scroll.asm below and ; ; handles resizing of the window properly ; ; Updates 26-1-2001. Now contains flicker free version thanks to; ; advice from hutch@pbq.com.au to change wc.style to ; ; mov wc.style, CS_BYTEALIGNWINDOW ;flicker free ; ; CS_HREDRAW or CS_VREDRAW ;for flicker ; ; To see difference, compare scroll2.exe and scroll2f.exe. ; ; ; ;---------------------------------------------------------------------------- ; Compile with nmake .386 .MODEL FLAT , STDCALL include windows.inc include user32.inc include kernel32.inc include gdi32.inc includelib user32.lib includelib kernel32.lib includelib gdi32.lib EXTRN wsprintfA:PROC .data ClassName db "SimpleWinClass",0 AppName db "Vertical and Horizontal Scrollbars",0 hInstance HINSTANCE ? CommandLine LPSTR ? hwnd HWND ? scrollpos DWORD ? hscrollpos DWORD ? hsize dword ? vsize dword ? counter DWORD ? buff db 200 dup(0) num db "%lu ",0 num1 db "(%lu, ",0 num2 db "%lu) ",0 hdc DWORD ? .code WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:SDWORD LOCAL wc:WNDCLASSEX LOCAL msg:MSG LOCAL scr:SCROLLINFO ;typedef struct tagSCROLLINFO { // si ; ; UINT cbSize; ; UINT fMask; ; int nMin; ; int nMax; ; UINT nPage; ; int nPos; ; int nTrackPos; ;} SCROLLINFO; mov wc.cbSize,SIZEOF WNDCLASSEX mov wc.style, CS_BYTEALIGNWINDOW ;flicker free ;CS_HREDRAW or CS_VREDRAW ;lots of flicker mov wc.lpfnWndProc, OFFSET WndProc mov wc.cbClsExtra,NULL mov wc.cbWndExtra,NULL push hInstance pop wc.hInstance mov wc.hbrBackground,COLOR_WINDOW+1 mov wc.lpszMenuName,NULL mov wc.lpszClassName,OFFSET ClassName invoke LoadIcon,NULL,IDI_APPLICATION mov wc.hIcon,eax mov wc.hIconSm,0 invoke LoadCursor,NULL,IDC_ARROW mov wc.hCursor,eax invoke RegisterClassEx, addr wc INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\ WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, 300,168, ;size NULL,NULL,\ hInst,NULL mov hwnd,eax ; invoke ShowScrollBar,hwnd,SB_HORZ ,TRUE mov scr.cbSize,SIZEOF scr ; mov scr.fMask,SIF_POS ;SIF_RANGE mov scr.fMask,SIF_POS + SIF_RANGE + SIF_PAGE mov scr.nMin,0 mov scr.nMax,100 mov scr.nPos,5 mov scrollpos,5 mov scr.nPage,10 invoke SetScrollInfo,hwnd,SB_VERT,addr scr,TRUE mov scr.nPos,50 mov hscrollpos,50 invoke SetScrollInfo,hwnd,SB_HORZ,addr scr,TRUE INVOKE ShowWindow, hwnd,SW_SHOWNORMAL INVOKE UpdateWindow, hwnd .WHILE TRUE INVOKE GetMessage, ADDR msg,NULL,0,0 .BREAK .IF (!eax) INVOKE TranslateMessage, ADDR msg INVOKE DispatchMessage, ADDR msg .ENDW mov eax,msg.wParam ret WinMain endp WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM LOCAL ps:PAINTSTRUCT LOCAL scr:SCROLLINFO mov eax,uMsg .IF eax==WM_DESTROY invoke PostQuitMessage,NULL .ELSEIF eax==WM_PAINT invoke BeginPaint,hWnd, ADDR ps mov hdc,eax ; invoke TextOut,hdc,0,0,ADDR TestString,(SIZEOF TestString) -1 call shownum invoke EndPaint,hWnd, ADDR ps ;------------------------------------------------------------------- ; Deal with window resizing .ELSEIF eax==WM_SIZE mov eax,lParam and eax,0ffffh shr eax,7 add eax,1 mov hsize,eax mov scr.nPage,eax mov scr.cbSize,SIZEOF scr mov scr.fMask,SIF_PAGE invoke SetScrollInfo,hwnd,SB_HORZ,addr scr,TRUE mov eax,lParam shr eax,16 shr eax,4 inc eax mov vsize,eax mov scr.nPage,eax mov scr.cbSize,SIZEOF scr mov scr.fMask,SIF_PAGE invoke SetScrollInfo,hwnd,SB_VERT,addr scr,TRUE ;------------------------------------------------------------------- ; Deal with horizontal scrollbar .ELSEIF eax==WM_HSCROLL invoke GetDC,hWnd mov hdc,eax mov eax,wParam and eax,0ffffh mov ebx,wParam shr ebx,16 mov ecx,hscrollpos mov edx,100 sub edx,hsize .IF eax==SB_THUMBTRACK mov hscrollpos,ebx call updatehscroll .ELSEIF eax==SB_THUMBPOSITION mov ebx,scrollpos cmp ebx,0 je hnosr1 dec ebx mov hscrollpos,ebx call updatescroll .ELSEIF eax==SB_LINEUP cmp ecx,0 je hnosr1 dec ecx mov hscrollpos,ecx call updatehscroll .ELSEIF eax==SB_LINEDOWN cmp ecx,hsize jae hnosr1 inc ecx mov hscrollpos,ecx call updatehscroll .ELSEIF eax==SB_PAGEUP cmp ecx,0 je hnosr1 cmp ecx,hsize ja hsub1 mov ecx,0 jmp hsub1a hsub1: sub ecx,hsize hsub1a: mov hscrollpos,ecx call updatehscroll .ELSEIF eax==SB_PAGEDOWN cmp ecx,edx jae hnosr1 sub edx,hsize cmp ecx,edx jb hsub2 mov ecx,edx add ecx,hsize jmp hsub2a hsub2: add ecx,hsize hsub2a: mov hscrollpos,ecx call updatehscroll .ENDIF hnosr1: call shownum invoke EndPaint,hWnd, ADDR ps ;------------------------------------------------------------------- ; Deal with vertical scrollbar .ELSEIF eax==WM_VSCROLL invoke GetDC,hWnd mov hdc,eax mov eax,wParam and eax,0ffffh mov ebx,wParam shr ebx,16 mov ecx,scrollpos mov edx,100 sub edx,vsize .IF eax==SB_THUMBTRACK mov scrollpos,ebx call updatescroll .ELSEIF eax==SB_THUMBPOSITION .ELSEIF eax==SB_LINEUP cmp ecx,0 je nosr1 dec ecx mov scrollpos,ecx call updatescroll .ELSEIF eax==SB_LINEDOWN cmp ecx,edx jae nosr1 inc ecx mov scrollpos,ecx call updatescroll .ELSEIF eax==SB_PAGEUP cmp ecx,0 je nosr1 cmp ecx,10 ja sub1 mov ecx,0 jmp sub1a sub1: sub ecx,vsize sub1a: mov scrollpos,ecx call updatescroll .ELSEIF eax==SB_PAGEDOWN cmp ecx,edx jae nosr1 sub edx,vsize cmp ecx,edx jb sub2 add edx,vsize mov ecx,edx jmp sub2a sub2: add ecx,vsize sub2a: mov scrollpos,ecx call updatescroll .ENDIF nosr1: call shownum invoke EndPaint,hWnd, ADDR ps ;------------------------------------------------------------------------- .ELSE invoke DefWindowProc,hWnd,uMsg,wParam,lParam ret .ENDIF xor eax,eax ret WndProc endp count proc push esi mov eax,0 noc2: cmp [esi],byte ptr 0 je noc1 inc esi inc eax jmp noc2 noc1: mov [counter],eax pop esi ret count endp shownum proc push ebp push edi mov edx,hscrollpos mov ecx,hsize mov edi,0 mov ebp,0 list1: push ecx push ebp push edi mov eax,scrollpos mov ecx,vsize listo: push edx push eax push ecx push edi push edx push eax push offset num1 push offset buff call wsprintfA add esp,0ch lea esi,buff call count invoke TextOut,hdc,ebp,edi,esi,counter push offset num2 push offset buff call wsprintfA add esp,0ch lea esi,buff call count add ebp,30 invoke TextOut,hdc,ebp,edi,esi,counter sub ebp,30 pop edi pop ecx pop eax pop edx inc eax add edi,16 loop listo pop edi pop ebp pop ecx add ebp,7fh inc edx dec ecx cmp ecx,0 jne list1 pop edi pop ebp ret shownum endp updatescroll proc LOCAL scr:SCROLLINFO mov ebx,scrollpos mov scr.cbSize,SIZEOF scr mov scr.fMask,SIF_POS mov scr.nPos,ebx invoke SetScrollInfo,hwnd,SB_VERT,addr scr,TRUE ret updatescroll endp updatehscroll proc LOCAL scr:SCROLLINFO mov ebx,hscrollpos mov scr.cbSize,SIZEOF scr mov scr.fMask,SIF_POS mov scr.nPos,ebx invoke SetScrollInfo,hwnd,SB_HORZ,addr scr,TRUE ret updatehscroll endp start: invoke GetModuleHandle, NULL mov hInstance,eax invoke GetCommandLine invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT invoke ExitProcess,eax end start