Sign in to follow this  
Followers 0
MrCreatoR

How to SetScrollPos for ListView?

6 posts in this topic

Hi,

I am trying to make my app "remember" the last scroll position in ListView control.

I thought that SetScrollPos from User32.dll can help me with that, but any action (in LV control) after the scroll will return the scroll bar to the top, and the items are not visible for a while.

Here is my example, i found a way using GUICtrlSendMsg($ListView, $WM_VSCROLL, $SB_LINEDOWN, 0), but it's slowing down the startup, i need to use loop for it to scroll every line (see commented lines)...

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>

$iLast_ScrollPos = IniRead("Test.ini", "Scroll", "VPos", 0)

$GUI = GUICreate("Test Script", 300, 200)

$ListView = GUICtrlCreateListView("Col          ", 20, 20, 260, 160)
$hListView = GUICtrlGetHandle($ListView)

For $i = 1 To 80
    GUICtrlCreateListViewItem("Item " & $i, $ListView)
Next

DllCall("user32.dll", "int", "SetScrollPos", "hwnd", $hListView, "int", $SB_VERT, "int", $iLast_ScrollPos, "int", True)

;~ For $i = 1 To $iLast_ScrollPos
;~  GUICtrlSendMsg($ListView, $WM_VSCROLL, $SB_LINEDOWN, 0)
;~ Next

GUISetState(@SW_SHOW, $GUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            $aGetScrollPos = DllCall("user32.dll", "int", "GetScrollPos", "hwnd", $hListView, "int", $SB_VERT)
            
            IniWrite("Test.ini", "Scroll", "VPos", $aGetScrollPos[0])
            
            Exit
    EndSwitch
WEnd

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites



MrCreatoR

Hi friend! I think this be easy:

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

$iLastPos = IniRead("Test.ini", "Scroll", "VPos", 0)

$GUI = GUICreate("Test Script", 300, 200)

$ListView = GUICtrlCreateListView("Col          ", 20, 20, 260, 160)
$hListView = GUICtrlGetHandle($ListView)

For $i = 1 To 80
    GUICtrlCreateListViewItem("Item " & $i, $ListView)
Next

GUISetState(@SW_SHOW, $GUI)

_GUICtrlListView_EnsureVisible($hListView, $iLastPos + 8)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            IniWrite("Test.ini", "Scroll", "VPos", _GUICtrlListView_GetTopIndex($hListView))
            Exit
    EndSwitch
WEnd

:)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks rasim! >_<

But it's good only for vertical scrollbar, but what we can do with the horizontal one?

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <ScrollBarConstants.au3>
;

$iLast_ScrollHPos = IniRead("Test.ini", "Scroll", "HPos", 6)
$iLast_ScrollVPos = IniRead("Test.ini", "Scroll", "VPos", 19)

$GUI = GUICreate("Remember Scroll Position Demo", 300, 200)

$ListView = GUICtrlCreateListView("Col", 20, 20, 260, 160)
$hListView = GUICtrlGetHandle($ListView)

GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, 400)

For $i = 1 To 80
    GUICtrlCreateListViewItem("Item " & $i, $ListView)
Next

;~ DllCall("user32.dll", "int", "SetScrollPos", "hwnd", $hListView, "int", $SB_HORZ, "int", $iLast_ScrollHPos, "int", True)
;~ DllCall("user32.dll", "int", "SetScrollPos", "hwnd", $hListView, "int", $SB_VERT, "int", $iLast_ScrollVPos, "int", True)

For $i = 1 To $iLast_ScrollHPos
    GUICtrlSendMsg($ListView, $WM_HSCROLL, $SB_LINERIGHT, 0)
    ;DllCall("user32.dll", "int", "SendMessage", "hwnd", $hListView, "int", $WM_HSCROLL, "int", $SB_LINERIGHT, "int", 0)
Next

For $i = 1 To $iLast_ScrollVPos
    GUICtrlSendMsg($ListView, $WM_VSCROLL, $SB_LINEDOWN, 0)
    ;DllCall("user32.dll", "int", "SendMessage", "hwnd", $hListView, "int", $WM_VSCROLL, "int", $SB_LINEDOWN, "int", 0)
Next

GUISetState(@SW_SHOW, $GUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            $aGetScrollHPos = DllCall("user32.dll", "int", "GetScrollPos", "hwnd", $hListView, "int", $SB_HORZ)
            $aGetScrollVPos = DllCall("user32.dll", "int", "GetScrollPos", "hwnd", $hListView, "int", $SB_VERT)
            
            IniWrite("Test.ini", "Scroll", "HPos", Int($aGetScrollHPos[0] / 5))
            IniWrite("Test.ini", "Scroll", "VPos", $aGetScrollVPos[0])
            
            Exit
    EndSwitch
WEnd

Why the «SetScrollPos» doesn't work? :)

Edited by MrCreatoR

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

MrCreatoR

Why the «SetScrollPos» doesn't work?

Hmm...

MSDN

Note. The SetScrollPos function is provided for backward compatibility. New applications should use the SetScrollInfo function.

Share this post


Link to post
Share on other sites

Hmm...

:( I read that thanks... so, it should work, doesn't? Otherwise it is not backward compatible :P

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

MrCreatoR

You know, i searched in the Google and seems your solution is correct. Anyway i saw examples like your :P

For $i = 1 To $iLast_ScrollVPos
    GUICtrlSendMsg($ListView, $WM_VSCROLL, $SB_LINEDOWN, 0)
Next

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0