VB新手问个问题求大虾帮帮看下

来源:百度知道 编辑:UC知道 时间:2024/06/14 07:54:29
Option Explicit
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 GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private win As Long

Private Sub Command1_Click()
Dim mousecoo As POINTAPI
Dim hdc As Long
mousecoo.x = 60
mousecoo.y = 60
win = FindWindow("SciCalc", "计算器")
hdc = GetDC(win) '取得计算器窗口屏幕的hDC

Form1.BackColor = GetPixel(hdc, mousecoo.x, mousecoo.y) ' '取颜色
ReleaseDC win, hdc '释放hDC(为什么要释放?)

End Sub
问题:我想单击后mousecoo的色值传

的确有错。计算器窗口不再最前面,返回值永远为-1.正确代码如下。见注释。
================
Option Explicit
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 GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Private win As Long

Private Sub Command1_Click()
Dim mousecoo As POINTAPI
Dim hdc As Long, h As Long
mousecoo.x = 60
mousecoo.y = 60
h = Shell("calc") '打开计算器
win = FindWindow("SciCalc", "计算器") '这一句虽然没错,但是不建议这样写,两个参数保留一个即可,建议保留地一个
hdc = GetDC(win) '取得计算器窗口屏幕的hDC
Ap