【问题标题】:Input to non-active window输入到非活动窗口
【发布时间】:2013-03-23 16:16:54
【问题描述】:

我有以下 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

获取屏幕上的某个按钮是否显示并处于活动状态(如果为true,则像素650、728将为白色),如果是,则单击它。

这很好用。我的问题是,如果我能以某种方式扩展代码,以便获取像素颜色的函数和单击鼠标的代码都可以应用于非活动窗口。这样我就可以让我的应用程序运行并在必要时单击按钮,同时我还可以将我的计算机用于其他事情。

我听说过一种叫做:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

但是,我很难让它与上面显示的示例代码一起工作 - 它仍然没有提供在非活动窗口上获取像素颜色的解决方案。

谢谢。

【问题讨论】:

  • 没人有解决办法吗?甚至是如何解决问题的提示?

标签: .net vb.net visual-studio-2010


【解决方案1】:

为了激活其他窗口,必须在鼠标向下和向上之间有睡眠以模拟真实点击

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);

【讨论】:

  • 我的意图不是通过鼠标点击来激活另一个窗口;我已经能够做到这一点。我想要的是一种将虚拟输入发送到非活动窗口的方法(不激活它,并破坏我正在做的任何其他事情)。
猜你喜欢
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多