用以学习EnumWindowsProc、SendMessage、WindowFromPoint、WindowFromPoint、ShowWindow、EnableWindow、FindWindow等等API的用法
共有3个窗体,分别为form1,form2,form3,和一个模块Module1
form1代码:form1有label(0~5)标签,text1、text2.。。。text5等5个textbox
Private Sub Form_Load()
Success = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)
Me.Left = Screen.Width - 1900
Me.Top = Screen.Height - 1710
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Label1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
If Button And 2 Then PopupMenu Form3.tfile
End Sub
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF2) Then
Form2.ListView1.ListItems.Clear
EnumChildWindows Text1.Text, AddressOf EnumWindowsProc, ByVal 0&
Form2.Show
End If
If GetAsyncKeyState(vbKeyF3) Then
Clipboard.Clear
Clipboard.SetText Text4.Text
MsgBox "窗口标题已保存至剪贴板。", vbOKOnly, "提示"
End If
Static lX As Long, lY As Long, stayTime As Long
On Local Error Resume Next
Dim P As POINTAPI, h As Long, hD As Long, r As Long
GetCursorPos P
lX = P.x: lY = P.y
Px.x = P.x
Px.y = P.y
'设备环境是一个结构,它定义了一系列图形对象及其相关的属性,以及会影响输出结果的绘图方式。这些图形对象包括:画笔(用于画直线),
'笔刷(用于绘图和填充),位图(用于屏幕的拷贝或滚动),调色板(用于定义可用的颜色集),剪裁区(用于剪裁和其他操作),路径(用
'于绘图和画图操作)。设备环境函数用于对设备环境进行创建、删除或获取信息
h = WindowFromPoint(lX, lY) '取得点所在控件的句柄
hD = GetDC(h)
ScreenToClient h, P
r = GetPixel(hD, P.x, P.y)
ReleaseDC h, hD
Text1.Text = h
Text2.Text = hD
Text3.Text = Hex(r)
Text3.BackColor = r
Dim WindowText As String, ClassName As String
WindowText = String(64, 0)
GetWindowText Text1.Text, WindowText, Len(WindowText)
ClassName = String(64, 0)
GetClassName Text1.Text, ClassName, 64
Text4.Text = Left(WindowText, InStr(1, WindowText, vbNullChar) - 1)
Text5.Text = Left(ClassName, InStr(1, ClassName, vbNullChar) - 1)
End Sub
form2代码:form2只有一个listview1控件
Private Sub ListView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Trim(ListView1.SelectedItem.ListSubItems(2).Text) = "可用" Then
Form3.tenable = False
Form3.tdisable = True
Else
Form3.tenable = True
Form3.tdisable = False
End If
If Trim(ListView1.SelectedItem.ListSubItems(3).Text) = "可见" Then
Form3.tshow = False
Form3.thide = True
Else
Form3.tshow = True
Form3.thide = False
End If
If Button And 2 Then PopupMenu Form3.tedit
End Sub
form3是菜单,无控件,菜单内容请看代码:
Private Sub tdisable_Click()
Dim i As Long
i = EnableWindow(Form2.ListView1.SelectedItem.Text, 0) '第2参非0即为ENABLE
Form2.ListView1.SelectedItem.ListSubItems(2).Text = "禁用"
End Sub
Private Sub tenable_Click()
Dim i As Long
Dim s As String
i = EnableWindow(Form2.ListView1.SelectedItem.Text, 1) '第2参非0即为ENABLE
Form2.ListView1.SelectedItem.ListSubItems(2).Text = "可用"
End Sub
Private Sub tshow_Click()
Dim i As Long
i = ShowWindow(Form2.ListView1.SelectedItem.Text, 1) '第2参非0即为ENABLE
Form2.ListView1.SelectedItem.ListSubItems(3).Text = "可见"
End Sub
Private Sub thide_Click()
Dim i As Long
i = ShowWindow(Form2.ListView1.SelectedItem.Text, 0) '第2参非0即为ENABLE
Form2.ListView1.SelectedItem.ListSubItems(3).Text = "隐藏"
End Sub
Private Sub tquit_Click()
End
End Sub
Private Sub tunlock_Click()
Const WM_USER = &H400
Const EM_SETREADONLY = (WM_USER + 31)
Dim i As Long
i = SendMessage(Form2.ListView1.SelectedItem.Text, EM_SETREADONLY, True, ByVal 0&)
End Sub
module1的代码:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Public Declare Function SetWindowPos Lib "user32" (ByVal h&, ByVal hb%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal f%) As Integer
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function IsWindowEnabled Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function ReleaseCapture 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 Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Public Type POINTAPI
x As Long
y As Long
End Type
Public Function EnumWindowsProc(ByVal hnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Dim isEnabled As Boolean, IsEn As String
Dim isVisable As Boolean, IsVi As String
isEnabled = IsWindowEnabled(hnd)
isVisable = IsWindowVisible(hnd)
If isEnabled = True Then
IsEn = " 可用"
Else
IsEn = " 禁用"
End If
If isVisable = True Then
IsVi = " 可见"
Else
IsVi = " 隐藏"
End If
Ret = GetWindowTextLength(hnd)
sSave = Space(Ret)
GetWindowText hnd, sSave, Ret + 1
Dim ClassName As String
ClassName = String(64, 0)
GetClassName hnd, ClassName, 64
ClassName = Left(ClassName, InStr(1, ClassName, vbNullChar) - 1)
Dim ListTemp As Variant
Set ListTemp = Form2.ListView1.ListItems.Add(, , hnd)
ListTemp.SubItems(1) = sSave
ListTemp.SubItems(2) = IsEn
ListTemp.SubItems(3) = IsVi
ListTemp.SubItems(4) = ClassName
EnumWindowsProc = True '=true继续列举到结束,=false则停止列举
End Function
仅供大家参考学习,偶尔用于解除某些软件中按钮的限制,呵呵,当然不要报太大希望,人家都有防止此类情况的措施的。
代码下载链接:http://download.csdn.net/detail/icbyboy/5005114