输入到非活动窗口
问题描述:
我有以下的Visual Basic 2010代码:输入到非活动窗口
Function GetScreenPixel(ByVal Location As Point) As Color
Dim C As Color
Using Bmp As New Bitmap(1, 1)
Using G As Graphics = Graphics.FromImage(Bmp)
G.CopyFromScreen(Location, Point.Empty, Bmp.Size)
C = Bmp.GetPixel(0, 0)
End Using
End Using
Return C
End Function
要获取屏幕上的一个像素的颜色。
我然后使用类似:
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If GetScreenPixel(New Point(650, 728)).ToString = "Color [A=255, R=255, G=255, B=255]" Then
Cursor.Position = New Point(955, 548)
mouse_event(&H2, 0, 0, 0, 0)
mouse_event(&H4, 0, 0, 0, 0)
End If
End Sub
要获取屏幕上的某个按钮是否显示和有效(如果真那么像素650,728将是白色的),如果是的话,它会点击它。
这工作正常。我的问题是,如果我能以某种方式扩展代码,以便获取像素颜色的函数和单击鼠标的代码都可以应用于非活动窗口。这样我就可以让应用程序继续运行,并在必要时单击该按钮,并且还可以同时将我的计算机用于其他事情。
我听说过一种叫:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
但是我有很多困难得到它与样品代码工作我已经如上图所示 - 它仍然没有提供解决方案在非活动窗口上获取像素的颜色。
谢谢。
答
为了激活其他窗口就必须有睡眠鼠标之间上下& UP模拟真实点击
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_ABSOLUTE,
(uint)Convert.ToInt32(position.X * (65535.0/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width)),
(uint)Convert.ToInt32(position.Y * (65535.0/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height)), 0, 0);
Thread.Sleep(400);
mouse_event(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_ABSOLUTE,
(uint)Convert.ToInt32(position.X * (65535.0/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width)),
(uint)Convert.ToInt32(position.Y * (65535.0/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height)), 0, 0);
而且您可能会感兴趣的鼠标移动功能 - 位置应设置与范围内的MOUSEEVENTF_ABSOLUTE标志0 - 65535
mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,
(uint)Convert.ToInt32(position.X * (65535.0/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width)),
(uint)Convert.ToInt32(position.Y * (65535.0/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height)), 0, 0);
+0
我的目的不是通过点击鼠标来激活另一个窗口;我已经能够做到这一点。我想要的是将虚拟输入发送到不活动的窗口(不激活它,并且干扰我正在做的任何其他操作)的方式。 – CHRIS 2014-02-28 21:30:37
没有人有解决方案吗?甚至提示如何解决这个问题? – CHRIS 2013-03-26 17:41:57