Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 888717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:26:43+00:00 2026-05-15T13:26:43+00:00

I have a problem with getting scrollbar positions. Is it possible to get the

  • 0

I have a problem with getting scrollbar positions. Is it possible to get the scrollbar position of another process for example Notepad. I wrote small app where i tested and always get 0 0 as a position of scrollbars.

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int GetScrollPos(IntPtr hWnd, int nBar);

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

    [DllImport("user32.dll")]
    static extern IntPtr SetActiveWindow(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);


    private void Form1_Load(object sender, EventArgs e)
    {
        this.SuspendLayout();

        Process notepad = new Process();
        ProcessStartInfo psi = new ProcessStartInfo(@"c:\list1.txt");
        psi.WindowStyle = ProcessWindowStyle.Normal;
        notepad.StartInfo = psi;

        notepad.Start();

        this.ResumeLayout();

        notepad.WaitForInputIdle(3000);

        IntPtr old = SetParent(notepad.MainWindowHandle, this.Handle);

        SetWindowLong(notepad.MainWindowHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
        MoveWindow(notepad.MainWindowHandle, 100, 100, 400, 400, true);

        SetActiveWindow(notepad.MainWindowHandle);
        SwitchToThisWindow(notepad.MainWindowHandle, true);            
    }

I have button which send PGDN event to notepad and it works great but after pgdn event position of scrollbar also is 0 0

    private void PGDN_Click(object sender, EventArgs e)
    {
        Process[] procs = Process.GetProcessesByName("Notepad");
        IntPtr hwnd = procs[0].MainWindowHandle;

        SetActiveWindow(hwnd);
        SwitchToThisWindow(hwnd, true);
        Thread.Sleep(2000);
        SendKeys.SendWait("{PGDN}");
        Thread.Sleep(2000);
        label1.Text = "OK";
        label1.Text = "";

        label1.Text =  HScrollPos().ToString() + "  " + VScrollPos().ToString(); }

Here are the HScrollPos and VScrollPos functions :

    public int VScrollPos()
    {
        Process[] procs = Process.GetProcessesByName("Notepad");
        IntPtr hwnd = procs[0].MainWindowHandle;
        if (procs.Length != 0)
        {
            return GetScrollPos(hwnd , SB_VERT);

        }
        else
        {
            MessageBox.Show("Notepad Nor Running");
            return 0;
        }      
    }

    public int HScrollPos()
    {
        Process[] procs = Process.GetProcessesByName("Notepad");
        IntPtr hwnd = procs[0].MainWindowHandle;
        if (procs.Length != 0)
        {

            return GetScrollPos(hwnd , SB_HORZ);
        }
        else
        {
            MessageBox.Show("Notepad Nor Running");
            return 0;
        }            
    }

Maybe there is another way to get Scrollbar Position of another process/window in windows? Please Help. Thx for granted.

And here is the Working Code based on Answer. Thx

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    private void button4_Click(object sender, EventArgs e)
    {
        string lpszParentClass = "Notepad";
        string lpszParentWindow = "Untitled - Notepad";
        string lpszClass = "Edit";

        IntPtr ParenthWnd = new IntPtr(0);
        IntPtr hWnd = new IntPtr(0);
        ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
        if (ParenthWnd.Equals(IntPtr.Zero))
            MessageBox.Show("Notepad Not Running");
        else
        {
            hWnd = FindWindowEx(ParenthWnd, hWnd, lpszClass, "");
            if (hWnd.Equals(IntPtr.Zero))
                  MessageBox.Show("Notepad doesn't have an edit component ?");
            else
            {
                MessageBox.Show("Notepad Window: " + ParenthWnd.ToString());
                MessageBox.Show("Edit Control: " + hWnd.ToString());
            }
        }
        SetActiveWindow(ParenthWnd);
        label5.Text = GetScrollPos(hWnd, SB_VERT) + "   " + GetScrollPos(hWnd, SB_HORZ);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:26:44+00:00Added an answer on May 15, 2026 at 1:26 pm

    I suspect the problem is that you are using the main window handle, you should be using the handle of the Edit control, which is a child of the main window.

    Using the main window hwnd you can enumrate the child windows to find the hWnd of the edit control and then use that hWnd in your call to get the scroll bar position.

    SendKeys is working because it is sending the key stroke to the window that has input focus which in this case is the Edit control.

    Here is an answer to a question I provided sometime back that will help with the interop for EnumChildWindows if you need, there is a lot more there but it might help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.