如何调用API函数?
发布网友
发布时间:2022-04-26 08:26
我来回答
共1个回答
热心网友
时间:2022-04-26 14:43
该程序演示了如何用鼠标来获得任何像素的RGB颜色。该程序使用了计时器,间隔设置为0.01秒来捕捉事件,使用了GetPixel
,
GetCursorPos
和GetDC
WinAPI调用,来获得屏幕上任何地方的像素的颜色。
option
Explicit
'在表单中加入计时器,使用鼠标移动到屏幕上任何一处,RGB颜色就会显示在表单中的Caption中
'
private
Type
POINTAPI
x
as
Long
y
as
Long
End
Type
'
private
Declare
Function
GetPixel
Lib
"gdi32"
(byval
hdc
as
Long,
_
byval
x
as
Long,
byval
y
as
Long)
as
Long
private
Declare
Function
GetCursorPos
Lib
"user32"
(lpPoint
as
POINTAPI)
as
Long
private
Declare
Function
GetWindowDC
Lib
"user32"
(byval
hwnd
as
Long)
as
Long
'
private
Sub
Form_Load()
Timer1.Interval
=
100
End
Sub
'
private
Sub
Timer1_Timer()
Dim
tPOS
as
POINTAPI
Dim
sTmp
as
string
Dim
lColor
as
Long
Dim
lDC
as
Long
'
lDC
=
GetWindowDC(0)
Call
GetCursorPos(tPOS)
lColor
=
GetPixel(lDC,
tPOS.x,
tPOS.y)
Label2.BackColor
=
lColor
'
sTmp
=
Right$("000000"
&
Hex(lColor),
6)
Caption
=
"R:"
&
Right$(sTmp,
2)
&
"
G:"
&
mid$(sTmp,
3,
2)
&
"
B:"
&
Left$(sTmp,
2)
End
Sub
至于获取当前窗口,可以使用
Public
Declare
Function
GetForegroundWindow
Lib
"user32"
()
As
Long
获取当前活动窗体的句柄,再使用GetWindowText的API能获取到相应的标题啦!
Public
Declare
Function
GetForegroundWindow
Lib
"user32"
()
As
Long
Public
Declare
Function
SendMessage
Lib
"user32"
Alias
"SendMessageA"
(ByVal
hwnd
As
Long,
ByVal
wMsg
As
Long,
ByVal
wParam
As
Long,
lParam
As
Any)
As
Long
Public
Const
WM_GETTEXT
=
&HD
Public
Function
GetWin()
As
String
Dim
tmp
As
Long
Dim
iLen
As
Long
Dim
Txt
As
String
tmp
=
GetForegroundWindow
Txt
=
String(255,
Chr(0))
iLen
=
SendMessage(tmp,
WM_GETTEXT,
Len(Txt),
ByVal
Txt)
Txt
=
Left(Txt,
iLen)
GetWin=Txt
End
Function
以上代码是获取当前窗体的标题代码!(这里没使用GetWindowText的API)