赞 | 0 |
VIP | 13 |
好人卡 | 0 |
积分 | 130 |
经验 | 77844 |
最后登录 | 2024-12-3 |
在线时间 | 1051 小时 |
Lv4.逐梦者
- 梦石
- 0
- 星屑
- 13031
- 在线时间
- 1051 小时
- 注册时间
- 2007-12-15
- 帖子
- 188
|
其实并不麻烦,可以看看这些代码,用 VB6 写的(不是我)。
- Option Explicit
- ' Getpixel sample by Matt Hart - [email protected]
- ' http://matthart.com
- '
- ' This sample shows how to get the pixel color of any point
- ' on the screen. The GetPixel API requires CLIENT coordinates,
- ' so you must first get the window handle and hDC where the
- ' cursor is. Once you get that, you can get the pixel.
- '
- ' However, there's one "gotcha" I found while writing this.
- ' Window titlebars return a "-1" for the pixel color, which
- ' is invalid! So, what I did to get around that was use
- ' BitBlt to copy a pixel from that device to the PictureBox
- ' control I'm using to show the colors, then use the Point
- ' method to check the color.
- Private Type POINTAPI
- x As Long
- y As Long
- End Type
- 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 Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
- Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
- Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
- Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
- Private Sub Timer1_Timer()
- Static lX As Long, lY As Long
- On Local Error Resume Next
- Dim P As POINTAPI, h As Long, hD As Long, r As Long
- GetCursorPos P
- If P.x = lX And P.y = lY Then Exit Sub
- lX = P.x: lY = P.y
- lblData(0).Caption = lX & "," & lY
- h = WindowFromPoint(lX, lY)
- lblData(1).Caption = h
- hD = GetDC(h)
- lblData(2).Caption = hD
- ScreenToClient h, P
- lblData(3).Caption = P.x & "," & P.y
- r = GetPixel(hD, P.x, P.y)
- If r = -1 Then
- BitBlt Picture1.hdc, 0, 0, 1, 1, hD, P.x, P.y, vbSrcCopy
- r = Picture1.Point(0, 0)
- Else
- Picture1.PSet (0, 0), r
- End If
- lblData(4).Caption = Hex$(r)
- Picture1.BackColor = r
- End Sub
复制代码 |
|