连云港新奥燃气营业厅:写个vb程序知道屏幕上是否有红色或者别的颜色

来源:百度文库 编辑:高考问答 时间:2024/04/20 04:42:17

呵呵,以下测试通过

加入一个commandbutton,一个picturebox,属性默认

加入以下代码就OK了,测试过了:

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long

Private Const SRCCOPY = &HCC0020

Private lngDesktopHwnd As Long
Private lngDesktopDC As Long

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long

Private Sub jp(Picture1 As PictureBox)

lngDesktopHwnd = GetDesktopWindow
lngDesktopDC = GetDC(lngDesktopHwnd)

Call BitBlt(Picture1.hdc, 0, 0, Screen.Width, Screen.Height, lngDesktopDC, 0, 0, SRCCOPY)
Picture1.Picture = Picture1.Image
Call ReleaseDC(lngDesktopHwnd, lngDesktopDC)
End Sub

Private Sub Command1_Click()
For i = 1 To Screen.Width / Screen.TwipsPerPixelX
For j = 1 To Screen.Height / Screen.TwipsPerPixelY
a = GetPixel(Picture1.hdc, i, j)
If a = RGB(255, 0, 0) Then '颜色值在RGB里改
Debug.Print i, j, "红点"
End If
Next j
Next i
End Sub

Private Sub Form_Load()
Me.ScaleMode = vbPixels
Picture1.AutoSize = True
Picture1.AutoRedraw = True
Picture1.ScaleMode = vbPixels
Picture1.BorderStyle = 0
Picture1.Width = Screen.Width / Screen.TwipsPerPixelX
Picture1.Height = Screen.Height / Screen.TwipsPerPixelY
jp Picture1
End Sub

'经典代码,赶紧收藏吧,呵呵!