Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Is it possible to send scroll message to a focused window(any typpe, e.g IE, Excel, Winform...) so that I can scroll active window programatically. If it is possible, then how to do it ?
I am new to C#.

Thanks in advance.
Posted

Finally I found the answer :) ... The following code gets the handle of the focused window and sends it the scroll message. The code doesnt work for some windows but its ok for now....


<pre lang="cs">public const uint WM_SCROLL = 276; // Horizontal scroll
public const uint WM_VSCROLL = 277; // Vertical scroll
public const int SB_LINEUP = 0; // Scrolls one line up
public const int SB_LINELEFT = 0;// Scrolls one cell left
public const int SB_LINEDOWN = 1; // Scrolls one line down
public const int SB_LINERIGHT = 1;// Scrolls one cell right
public const int SB_PAGEUP = 2; // Scrolls one page up
public const int SB_PAGELEFT = 2;// Scrolls one page left
public const int SB_PAGEDOWN = 3; // Scrolls one page down
public const int SB_PAGERIGTH = 3; // Scrolls one page right
public const int SB_PAGETOP = 6; // Scrolls to the upper left
public const int SB_LEFT = 6; // Scrolls to the left
public const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
public const int SB_RIGHT = 7; // Scrolls to the right
public const int SB_ENDSCROLL = 8; // Ends scroll


[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public uint left;
public uint top;
public uint right;
public uint bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public uint cbSize;
public uint flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCapred;
public RECT rcCaret;
}

[DllImport(&quot;user32.dll&quot;)]
public static extern int SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);


public Form1()
{
InitializeComponent();
}
[DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;GetGUIThreadInfo&quot;)]
public static extern bool GetGUIThreadInfo(uint tId, out GUITHREADINFO threadInfo);


private void scrollActiveWindow()
{
GUITHREADINFO gInfo = new GUITHREADINFO();
gInfo.cbSize = (uint)Marshal.SizeOf(gInfo);
GetGUIThreadInfo((uint)0, out gInfo);
IntPtr focusedWin = gInfo.hwndFocus;
SendMessage(focusedWin, WM_VSCROLL, (IntPtr)SB_LINEDOWN, IntPtr.Zero);
textBox1.Text = textBox1.Text + &quot;\r\n&quot; + focusedWin.ToString();
}</pre>



Call "scrollActiveWindow" to scroll currently active window...
 
Share this answer
 
v2
IE Using javascript
Excel Using Macros
 
Share this answer
 
Comments
Nikhil Shekhar 17-Dec-10 23:26pm    
I want the scroll to be done by another program so that it can be used for any Active window. Or is it possible to scroll Active IE by another program ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900