2013-03-23 40 views
0

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

但是我有很多困難得到它與樣品代碼工作我已經如上圖所示 - 它仍然沒有提供解決方案在非活動窗口上獲取像素的顏色。

謝謝。

+0

沒有人有解決方案嗎?甚至提示如何解決這個問題? – CHRIS 2013-03-26 17:41:57

回答

0

爲了激活其他窗口就必須有睡眠鼠標之間上下& 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