Tuesday, May 29, 2012

Horizontal Scroll And Vertical Scroll

Option Explicit 
  
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
  
Public Const SB_LINEUP As Long = 0 
Public Const SB_LINEDOWN As Long = 1 
  
Public Const WM_VSCROLL As Long = &H115 
Public Const WM_HSCROLL As Long = &H114 
Public Const WM_MOUSEWHEEL As Long = &H20A 
Public Const GWL_WNDPROC = (-4) 
  
Public PrevProc As Long 
Public blnFocusScroll As Boolean 
  
Function NewWindowProc(ByVal hWnd As Long, _ 
ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
On Error Resume Next 
    Select Case Msg 
        Case Is = WM_MOUSEWHEEL 
            If blnFocusScroll = True Then 
                If (wParam > 0) Then 
                    'Form1 adalah nama form yang akan akan digunakan 
                    'Scroll adalah nama scrollbar yang akan digunakan 
                    SendMessage Form1.Scroll.hWnd, WM_VSCROLL, SB_LINEUP, 0& 
                    Form1.Scroll.Value = Form1.Scroll.Value - _ 
                    Form1.Scroll.LargeChange 
                Else 
                    SendMessage Form1.Scroll.hWnd, WM_VSCROLL, SB_LINEDOWN, 0& 
                    Form1.Scroll.Value = Form1.Scroll.Value + _ 
                    Form1.Scroll.LargeChange 
                End If 
                Form1.Scroll_Change 
            End If 
        End Select 
        ' 
        NewWindowProc = CallWindowProc(PrevProc, hWnd, Msg, wParam, lParam) 
End Function 
  
Public Sub HookForm(F As Form) 
    PrevProc = SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf NewWindowProc) 
End Sub 
  
Public Sub UnHookForm(F As Form) 
    SetWindowLong F.hWnd, GWL_WNDPROC, PrevProc 
End Sub 

Dalam form tambahkan kode di bawah ini:

Option Explicit 
  
Dim AwalTop As Long 
  
Sub Scrolling(Value As Long) 
    Dim i As Long 
  
    picItem(0).Top = picItem(0).Top + (AwalTop - Value) 
  
    For i = 1 To picItem.Count - 1 
        picItem(i).Top = picItem(i - 1).Top + Me.picItem(0).Height + 20 
        DoEvents 
    Next 
  
    AwalTop = Value 
End Sub 
  
Private Sub Form_Load() 
    HookForm Me 
    blnFocusScroll = True 
    Me.Scroll.Max = 2500 
    Me.Scroll.SmallChange = 10 
    Me.Scroll.LargeChange = 100 
End Sub 
  
Private Sub Form_Unload(Cancel As Integer) 
    UnHookForm Me 
End Sub 
  
Sub Scroll_Change() 
    Scrolling Me.Scroll.Value 
End Sub 
  
Sub Scroll_Scroll() 
    Scrolling Me.Scroll.Value 
End Sub 

Perhatian:
Kode di atas menggunakan subclassing, kesalahan mengkode dapat menyebabkan CRASH!