1

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);
    }
Zbig K.
  • 141
  • 1
  • 7

1 Answers1

2

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.

Community
  • 1
  • 1
Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • 1
    +1, this is correct. MainWindowHandle returns Notepad's frame window, it doesn't have a scrollbar. Easily visible with Spy++ – Hans Passant Jun 27 '10 at 15:13