如何滚动任何活动窗口? [英] How to scroll any active window ?

查看:95
本文介绍了如何滚动任何活动窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否可以将滚动消息发送到焦点窗口(任何键入,例如IE,Excel,Winform ...),以便我可以以编程方式滚动活动窗口.如果可能的话,该怎么办?
我是C#的新手.

预先感谢.

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.

推荐答案

最后,我找到了答案:) ...下面的代码获取聚焦窗口的句柄并向其发送滚动消息.该代码不适用于某些Windows,但现在还可以....


< pre lang ="cs"> public const uint WM_SCROLL = 276; //水平滚动
公共常量WM_VSCROLL = 277; //垂直滚动
public const int SB_LINEUP = 0; //向上滚动一行
public const int SB_LINELEFT = 0;//向左滚动一个单元格
public const int SB_LINEDOWN = 1; //向下滚动一行
public const int SB_LINERIGHT = 1;//向右滚动一个单元格
public const int SB_PAGEUP = 2; //向上滚动一页
public const int SB_PAGELEFT = 2;//向左滚动一页
public const int SB_PAGEDOWN = 3; //向下滚动一页
public const int SB_PAGERIGTH = 3; //向右滚动一页
public const int SB_PAGETOP = 6; //滚动到左上方
public const int SB_LEFT = 6; //滚动到左侧
public const int SB_PAGEBOTTOM = 7; //滚动到右上方
public const int SB_RIGHT = 7; //滚动到右侧
public const int SB_ENDSCROLL = 8; //结束滚动


[StructLayout(LayoutKind.Sequential)]
公共结构RECT
{
剩下的公共单位;
公共uint顶部;
公共权利;
公共uint底部;
}
[StructLayout(LayoutKind.Sequential)]
公共结构GUITHREADINFO
{
公共uint cbSize;
公共uint标志;
公共IntPtr hwndActive;
public IntPtr hwndFocus;
公共IntPtr hwndCapture;
公共IntPtr hwndMenuOwner;
公共IntPtr hwndMoveSize;
公共IntPtr hwndCapred;
公共RECT rcCaret;
}

[DllImport(& quot; user32.dll& quot;))]
公共静态外部int SendMessage(
IntPtr hWnd,//到达目标窗口的句柄
uint消息,//消息
IntPtr wParam,//第一个消息参数
IntPtr lParam//第二个消息参数
);


公共Form1()
{
InitializeComponent();
}
[DllImport(&"user32.dll&",EntryPoint =&"GetGUIThreadInfo& quot;))]
公共静态外部布尔GetGUIThreadInfo(uint tId,out GUITHREADINFO threadInfo);


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



调用"scrollActiveWindow"以滚动当前活动的窗口...
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...


IE使用javascript
Excel使用宏
IE Using javascript
Excel Using Macros


这篇关于如何滚动任何活动窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆