C# Scroll window(Chomre, FireFox) perticular pixel using Window handle

Ankur Tripathi 1 Reputation point
2023-08-16T10:52:50.93+00:00

Like previously I tried to get ScrollInfo of the window but it's not working on Chrome or other windows where they are using a custom scroll bar....

So it's possible to scroll windows by code particular height like I want to scroll Chomre window 100 Pixel?

i tried to scroll windows by mouse delta also but it's different for each window.. so looking for a fixed like want to scroll 100 pixels for any type of window(Chrome, Visual Studio etc)

Code I tried

InputHelpers.SendVerticalMouseWheel(-100);

internal class InputHelpers
    {
        internal static bool SendKeyPress(VirtualKeyCode keyCode)
        {
            var manager = new InputManager();
            manager.SendKeyPress(keyCode);
            return manager.SendInputs();
        }

        internal static bool SendVerticalMouseWheel(int delta)
        {
            InputManager inputManager = new InputManager();
            inputManager.AddVerticalMouseWheel(delta);
            return inputManager.SendInputs();
        }

        internal static bool SendHorizontalMouseWheel(int delta)
        {
            InputManager inputManager = new InputManager();
            inputManager.AddHorizontalMouseWheel(delta);
            return inputManager.SendInputs();
        }
    }
public class InputManager
    {
        public List<INPUT> InputList { get; private set; }

        public InputManager()
        {
            InputList = new List<INPUT>();
        }

        internal void SendKeyPress(VirtualKeyCode key)
        {
            INPUT input = new INPUT();
            input.Type = InputType.InputKeyboard;
            input.Data.Keyboard = new KEYBDINPUT();
            input.Data.Keyboard.wVk = key;
            input.Data.Keyboard.dwFlags = KeyboardEventFlags.KEYEVENTF_KEYUP;
            InputList.Add(input);

            input = new INPUT();
            input.Type = InputType.InputKeyboard;
            input.Data.Keyboard = new KEYBDINPUT();
            input.Data.Keyboard.wVk = key;
            InputList.Add(input);
        }

        internal void AddVerticalMouseWheel(int delta)
        {
            INPUT input = new INPUT();
            input.Type = InputType.InputMouse;
            input.Data.Mouse = new MOUSEINPUT();
            input.Data.Mouse.dwFlags = MouseEventFlags.MOUSEEVENTF_WHEEL;
            input.Data.Mouse.mouseData = (uint)delta;
            InputList.Add(input);
        }

        internal void AddHorizontalMouseWheel(int delta)
        {
            INPUT input = new INPUT();
            input.Type = InputType.InputMouse;
            input.Data.Mouse = new MOUSEINPUT();
            input.Data.Mouse.dwFlags = MouseEventFlags.MOUSEEVENTF_HWHEEL;
            input.Data.Mouse.mouseData = (uint)delta;
            InputList.Add(input);
        }

        public bool SendInputs()
        {
            INPUT[] inputList = InputList.ToArray();
            uint len = (uint)inputList.Length;
            uint successfulInputs = NativeMethods.SendInput(len, inputList, Marshal.SizeOf(typeof(INPUT)));
            return successfulInputs == len;
        }
    }

[DllImport("user32.dll")]         public static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,627 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,941 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,726 Reputation points Microsoft Vendor
    2023-08-18T00:54:01.4233333+00:00

    The code uses IUIAutomation::ElementFromPoint for quick test. Let the mouse hover over the scroll bar to retrieve the UI Automation element.

    #include <Windows.h>
    #include <UIAutomation.h>
    #include <wchar.h>
    
    int Element(IUIAutomation* automation)
    {
        // Get the element under the cursor
    // Use GetPhysicalCursorPos to interact properly with
    // High DPI
        POINT pt;
        GetPhysicalCursorPos(&pt);
    
        IUIAutomationElement* pAtMouse;
        HRESULT hr = automation->ElementFromPoint(pt, &pAtMouse);
        if (FAILED(hr))
            return hr;
    
        // Get the element's name and print it
        BSTR name;
        hr = pAtMouse->get_CurrentName(&name);
        if (SUCCEEDED(hr))
        {
            wprintf(L"Element's Name: %s \n", name);
            SysFreeString(name);
    
            //IUIAutomationValuePattern* pattern;
            //pAtMouse->GetCurrentPatternAs(UIA_ValuePatternId, IID_IUIAutomationValuePattern,(void**)&pattern);
            IUIAutomationLegacyIAccessiblePattern* pattern;
    
            //pAtMouse->GetCurrentPatternAs(UIA_LegacyIAccessiblePatternId, IID_IUIAutomationLegacyIAccessiblePattern,(void**)&pattern);
            pAtMouse->GetCurrentPatternAs(UIA_LegacyIAccessiblePatternId, IID_PPV_ARGS(&pattern));
            //TODO
            BSTR url = nullptr;
            pattern->get_CurrentValue(&url);
            DWORD state = 0;
            pattern->get_CurrentState(&state);
            BSTR elename = nullptr;
            pattern->get_CurrentName(&elename);
            DWORD role = 0;
            pattern->get_CurrentRole(&role);
            //wprintf(L"Element's ValuePattern: %s \n", url);
            SysFreeString(url);
            SysFreeString(elename);
        }
        IUIAutomationScrollPattern* pattern1;
        if (pAtMouse->GetCurrentPatternAs(UIA_ScrollPatternId, IID_PPV_ARGS(&pattern1)) == S_OK)
        {
            wprintf(L"Success! \n");
            if (pattern1)
            {
                BOOL b{};
                pattern1->get_CurrentVerticallyScrollable(&b);
                double size;
                pattern1->get_CurrentVerticalViewSize(&size);
                double p;
                pattern1->get_CurrentVerticalScrollPercent(&p);
                pattern1->SetScrollPercent(0,p+1.0);
    
                wprintf(L"get_CurrentVerticalViewSize:%f get_CurrentVerticalScrollPercent:%f\n", size, p);
            }
        }
    
        // Get the element's Control Type (in the current languange)
        // and print it
        BSTR controlType;
        hr = pAtMouse->get_CurrentLocalizedControlType(&controlType);
        if (SUCCEEDED(hr))
        {
            wprintf(L"Element's Control Type: %s \n", controlType);
            SysFreeString(controlType);
        }
    
        // Clean up our COM pointers
        pAtMouse->Release();
        return hr;
    }
    
    int main(int argc, TCHAR* argv[])
    {
        // Initialize COM and create the main Automation object
        IUIAutomation* g_pAutomation;
        CoInitialize(NULL);
        HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL,
            CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation),
            (void**)&g_pAutomation);
        if (FAILED(hr))
            return (hr);
    
        bool quit = false;
        while (!quit)
        {
            SHORT leftControlMod = GetAsyncKeyState(VK_LCONTROL);
            if (leftControlMod != 0)
            {
                Element(g_pAutomation);
                Sleep(100);
            }
            quit = GetAsyncKeyState(VK_ESCAPE);
        }
    
        g_pAutomation->Release();
        CoUninitialize();
        return 0;
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.