The scale of the scrollbar is typically based on the size of the offscreen content. As your code has no way to detect the offscreen size, you can not calculate the number of pixels the scroll moves. Take chrome for instance, the length of the html document will control movement of the scrollbar.
C# SCROLLINFO not working on Chrome window
I am attempting to retrieve scroll information from the window handle, but I keep getting a value of 0. I am pursuing this approach because my goal is to capture a complete scrolling screenshot of the window. The intention is to enable automatic scrolling and image capture. While I have successfully implemented automatic window scrolling, I've encountered variations in the scrolling behaviour for different windows.
For instance, sending a mouse delta of 100 results in Chrome scrolling by around 70 pixels, whereas Visual Studio scrolls by approximately 110 pixels. Each window appears to exhibit distinct scrolling characteristics. Therefore, my current objective is to calculate the precise amount of pixel movement during scrolling using the scrollbar. However, I have encountered difficulties in achieving this functionality.
Basically, I want to scroll screenshots.. so I have 2 options 1) After scrolling automatically by code calculate how much pixel scroll moved and I will capture that part and attach it to the image 2) If I am able to scroll pre-define height so, in that case, I know I scrolled 100 pixels so capture a 100-pixel image only trying option 1 but no luck yet..
Getting "False" from GetScrollInfo and GetLastError() returns 1447 error code
Code:-
// get scrollbar info
SCROLLINFO scrollinfo = new SCROLLINFO();
scrollinfo.cbSize = (uint)Marshal.SizeOf(scrollinfo);
scrollinfo.fMask = SIF_ALL; // Get all the scroll bar information.
bool horBar = GetScrollInfo1(windowHandle, SB_HORZ, out scrollinfo);
Console.WriteLine(horBar);
bool vertBar = GetScrollInfo1(windowHandle, SB_VERT, out scrollinfo);
Console.WriteLine(vertBar);
Console.WriteLine(scrollinfo.nPage + ", " + scrollinfo.nPos);
[DllImport("user32.dll")]
public static extern bool GetScrollInfo(IntPtr hWnd, int fnBar, ref SCROLLINFO lpsi);
Windows development Windows API - Win32
Developer technologies .NET Other
Developer technologies C#
2 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
2023-08-13T15:31:58.7933333+00:00 -
Castorix31 90,681 Reputation points
2023-08-15T09:36:03.9833333+00:00 my goal is to capture a complete scrolling screenshot of the window.
You can use the same method as in ShareX : if GetScrollInfo fails, it compares the last 2 images to detect end of scrolling
(I tested with Chrome on Windows 10 22H2)