问题
I'm designing a player application to accompany our phone system. As our calltakers take calls, it makes recordings of each call. They can go to a list module, find a recording and double click, which opens my player. The issue i have is that if the calltaker gets another call, my player doesn't know it and will continue playing. I'm looking for a way to monitor the screen in a particular area and when it sees yellow or red instead of blue, it will pause my player.
The phone system does not have any API's that I can hook onto, so I have to try it another way.
The screen resolution never changes and the queue buttons where they receive calls will always be static. When they get a call, a small area changes from the background color blue to yellow or red to indicate a call.
Any suggestions?
**EDIT Final Code based on answers below and question Memory Leak using GetPixel/GetDC in Visual Basic
Private Function CheckforCall()
Dim hDC As IntPtr = GetDC(0)
Try
Dim queue1 As Integer = GetPixel(hDC, 40, 573)
Dim queue2 As Integer = GetPixel(hDC, 140, 573)
Dim queue3 As Integer = GetPixel(hDC, 240, 573)
Dim queue4 As Integer = GetPixel(hDC, 340, 573)
Dim queue5 As Integer = GetPixel(hDC, 440, 573)
If queue1 <> 9990727 Then
lblRinger.Text = "In Calls GOT CALL"
Return True
ElseIf queue2 <> 9990727 Then
lblRinger.Text = "Admin GOT CALL"
Return True
ElseIf queue3 <> 9990727 Then
lblRinger.Text = "Overflow GOT CALL"
Return True
ElseIf queue4 <> 9990727 Then
lblRinger.Text = "Bi-Lingual GOT CALL"
Return True
ElseIf queue5 <> 9990727 Then
lblRinger.Text = "Intercom GOT CALL"
Return True
Else
lblRinger.Text = "No Call"
Return False
End If
Catch ex As Exception
Return False
Finally
ReleaseDC(0, hDC)
End Try
End Function
回答1:
I'm pretty sure that this is what you want:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx
Good luck!
EDIT:
I forgot you have to give a device context (hDC
) to have GetPixel
work. It's sometimes hard to deal with the window handles (hWnd
) that GetDC
requires, so you can simply get the device context for the entire screen with GetDC(0)
.
Code shamelessly stolen from http://www.vbforums.com/showthread.php?t=491397 :
Declare Auto Function FindWindow Lib "user32" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
Declare Function GetDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32) As Int32
Public Function GetColorAt(ByVal X As Int32, ByVal Y As Int32) As Int32
Dim hWnd As IntPtr
Dim hDC As IntPtr
hWnd = FindWindow(vbNullString, "RagII")
hDC = GetDC(hWnd)
Dim lColor As Int32 = GetPixel(hDC, X, Y)
ReleaseDC(hWnd, hDC)
Return lColor
End Function
回答2:
You can use the PrintWindow
function from the Win32 API to get the bitmap for a specific window. You can then read the pixels from this bitmap with Bitmap.GetPixel
:
You need to import this (and probably more functions) using DllImport
:
<DllImport("user32.dll")> _
Private Shared Function PrintWindow(hwnd As IntPtr, hdcBlt As IntPtr, nFlags As UInteger) As Boolean
Here is a small sample code to capture a window:
Using bm As New Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555)
Dim g As Graphics = Graphics.FromImage(bm)
Dim hdc As IntPtr = g.GetHdc()
PrintWindow(hwnd, hdc, 0) 'hwnd is the window handle of the phone application
g.ReleaseHdc(hdc)
g.Flush()
Return Image.FromHbitmap(bm.GetHbitmap())
End Using
However: Try to see if there really isn't another way to solve this. Capturing the screen will be a fragile solution which is only waiting to break in the next update of the calling software.
来源:https://stackoverflow.com/questions/10035140/monitor-an-area-of-the-screen-for-a-certain-color-in-visual-basic