VB做外挂编程入门(转)
1.查找目标窗口.需要做外挂,就需要查找目标窗口.然后才做一些其他的动作.比如说鼠标键盘模拟啦.内存修改啦.封包型发送与替换啦什么什么的 <br/>------------------------------------------------------------------------------------------------- <br/>'定义模块 <br/>Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long <br/>'在窗口中建立一timer(时间控制器),然后在代码窗口输入如下代码: <br/>Private Sub Form_Load() <br/>Timer1.Interval = 500 <br/>End Sub <br/><br/>时间控制器的代码如下: <br/>Private Sub Timer1_Timer() <br/> Dim hwnd As Long <br/> hwnd = FindWindow(vbNullString, "计算器") '抓取"计算器"这个窗口名称. <br/>If (hwnd = 0) Then <br/> If MsgBox("你没有打开[计算器]程序!点击“确定”退出。点“取消”继续。", 49, "错误!") = 1 Then End <br/>ElseIf (hwnd <> 0) Then <br/>MsgBox "你已经打开了[计算器]程序.点“确定”退出本程序", , "退出" <br/>End <br/>End If <br/>End Sub <br/> 2.以下为模拟键盘事件.比如模拟"r"键. <br/>---------------------------------------------------------------------------------------------------------------------------- <br/> '在模块中定义 <br/> Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) <br/><br/>在窗口中建立一timer.时间间隔随意.只要不是0就可以了哦.呵呵. <br/>Private Sub Timer1_Timer() <br/>Call keybd_event(82, 0, 0, 0) '模拟按下"R"键 <br/>End Sub<cc></cc> 3.以下为快捷键例子.比如按下"ctrl+A"就退出! <br/>'可以设置Form的KeyPreview属性为True,然后在Form_KeyDown事件中添加代码: <br/> Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) <br/>If KeyCode = Asc("A") And Shift = vbCtrlMask Then unload me '如果ctrl+A键被按下就退出 <br/> End Sub <br/><br/><br/>例二: <br/>在Form中加入 <br/>Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer <br/>Private Function MyHotKey(vKeyCode) As Boolean <br/>MyHotKey = (GetAsyncKeyState(vKeyCode) < 0) <br/> End Function <br/>'然后在循环中或Timer的Timer事件中检测: <br/>Private Sub Timer1_Timer() <br/>If MyHotKey(vbKeyA) And vbKeyControl Then 'ctrl+A <br/>End '关闭 <br/>End If <br/>'其中vbkeyA是键盘〃A〃的常数,其他键可按F1查得。 <br/>End Sub<cc></cc> 取得窗口的句柄.类.名称等 <br/><br/>建立三个label1/label2/lebel3/ <br/>名称分别为窗口句柄/类/标题/ <br/>建立一个text窗口 <br/>建立二个command按钮,一为开始抓取。一为退出 <br/><br/>声明: <br/>Private Type POINTAPI <br/> x As Long <br/> y As Long <br/>End Type <br/>Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long <br/>Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long <br/>Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long <br/>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long <br/>Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long <br/>Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long <br/>Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long <br/><br/>Private Sub Command1_Click() <br/>If Command1.Caption = "开始抓取(&S)" Then <br/>Timer1.Enabled = True <br/>Command1.Caption = "停止抓取(&S)" <br/>Else <br/>Timer1.Enabled = False <br/>Command1.Caption = "开始抓取(&S)" <br/>End If <br/>End Sub <br/><br/>Private Sub Command2_Click() <br/>End <br/>End Sub <br/><br/>Private Sub Form_Load() <br/>SetWindowPos Me.hwnd, -1, 0, 0, 0, 0, &H1 Or &H2 '使窗体位于最顶端 <br/>End Sub <br/><br/>Private Sub Timer1_Timer() <br/>On Error Resume Next <br/>Dim tPoint As POINTAPI <br/>Dim hWin As Long <br/>Dim str As String * 255 <br/>Dim Abc As String * 64000 <br/>Dim Txt(64000) As Byte <br/>GetCursorPos tPoint '获得当前鼠标位置 <br/>hWin = WindowFromPoint(tPoint.x, tPoint.y) '获得窗口名柄 <br/>If hWin = Me.hwnd Or hWin = Command1.hwnd Or hWin = Command2.hwnd Or hWin = Text1.hwnd Then Exit Sub '确定窗口不在 Form1 中 <br/>GetClassName hWin, str, 255 '获得窗口类 <br/>SendMessage hWin, &HD, 64000, Txt(0) '获得窗口标题(也可使用 API 函数:GetWindowText,但效果不佳) <br/>Label1.Caption = "窗口名柄: " & hWin <br/>Label2.Caption = "窗口类: " & str <br/>Text1.Text = StrConv(Txt, vbUnicode) <br/>End Sub<cc></cc> 2.模拟鼠标显示.隐藏 <br/>隐藏/显示鼠标. <br/><br/>Public Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long <br/><br/>'forml中函数如下 <br/>'隐藏鼠标(需要事件击活,比如窗体事件等) <br/>ShowCursor False <br/>'显示鼠标(需要事件击活,比如窗体事件等) <br/>ShowCursor True <br/> 3.定位鼠标,使之不能移动 <br/>定位鼠标。 <br/><br/>Type rect <br/> sbleft As Long <br/> sbtop As Long <br/> sbright As Long <br/> sbbottom As Long <br/>End Type <br/><br/>Public Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long <br/><br/><br/>'鼠标定位 <br/>Private Sub Form_Load() <br/>'定位鼠标 <br/> Dim x As Long, y As Long <br/> Dim newrect As rect <br/> x& = Screen.TwipsPerPixelX <br/> y& = Screen.TwipsPerPixelY <br/> <br/> With newrect '鼠标只能在500,500-500,500这个范围内移动,如果四个数一样也可以说锁定鼠标了.如果加在记时器里的话就移动不了啦. <br/> .sbleft = 500 <br/> .sbtop = 500 <br/> .sbright = 500 <br/> .sbbottom = 500 <br/> End With <br/> <br/> ClipCursor newrect <br/>如果鼠标被锁定,不能恢复怎么办?不用担心.看如下代码. <br/>'使鼠标恢复(设定一个事件.才好击活这个代码.) <br/> Dim newrect As rect <br/> With newrect '这样鼠标又可以在0,0-屏幕的最右角,屏幕的最右下脚移动了 <br/> .sbleft = 0 <br/> .sbtop = 0 <br/> .sbright = Screen.Width / Screen.TwipsPerPixelX <br/> .sbbottom = Screen.Height / Screen.TwipsPerPixelY <br/> End With <br/> <br/> ClipCursor newrect <br/> End Sub<cc></cc> 一些控制鼠标的例子! <br/> 1.模拟鼠标击键过程 <br/>'声明: <br/>Option Explicit <br/> Private Declare Sub mouse_event Lib "user32" ( ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long ) <br/><br/>'对变量的定义 <br/>Const MOUSEEVENTF_LEFTDOWN = &H2 <br/>Const MOUSEEVENTF_LEFTUP = &H4 <br/>Const MOUSEEVENTF_MIDDLEDOWN = &H20 <br/>Const MOUSEEVENTF_MIDDLEUP = &H40 <br/>Const MOUSEEVENTF_MOVE = &H1 <br/>Const MOUSEEVENTF_ABSOLUTE = &H8000 <br/>Const MOUSEEVENTF_RIGHTDOWN = &H8 <br/>Const MOUSEEVENTF_RIGHTUP = &H10 <br/><br/> '这里是 鼠标左键按下 和松开两个事件的组合即一次单击 <br/> mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 <br/><br/> '模拟鼠标右键单击事件 <br/> mouse_event MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 <br/><br/> '两次连续的鼠标左键单击事件 构成一次鼠标双击事件 <br/> mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 <br/> mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0<cc></cc> 窗口类函数 <br/><br/><br/>SetWindowPos函数:使窗口停留在屏幕最顶层。 <br/>声明: <br/>Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _ <br/>ByVal hWndinsertAfter As Long, ByVal x As Long, ByVal y As Long, _ <br/>ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long <br/>调用如: <br/>Private Sub Form_Load() <br/>SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, &H2 + &H1 <br/>End Sub 窗口form1将保留在屏幕表面。 <br/>该函数功能是为窗口指定1个新的位置和状态。参数: <br/>hwnd:欲定位的窗口。 <br/>hwndinsertAfter:指定窗口的位置。 可能选用下述值之一: <br/>HWND_BOTTOM 将窗口置于窗口列表底部 <br/>HWND_TOP 将窗口置于Z序列的顶部;Z序列代表在分级结构中,窗口针对一个给定级别的窗口显示的顺序 <br/>HWND_TOPMOST(值-1) 将窗口置于列表顶部,并位于任何最顶部窗口的前面 <br/>HWND_NOTOPMOST(值-2) 将窗口置于列表顶部,并位于任何最顶部窗口的后面 <br/>x: 窗口新的x坐标。如hwnd是一个子窗口,则x用父窗口的客户区坐标表示 <br/>y: 窗口新的y坐标。如hwnd是一个子窗口,则y用父窗口的客户区坐标表示 <br/>cx:指定新的窗口宽度 <br/>cy:指定新的窗口高度 <br/>wFlags:包含了游标的一个整数,可能为下述值或其组合。 <br/>SWP_DRAWFRAME 围绕窗口画一个框 <br/>SWP_HIDEWINDOW 隐藏窗口 <br/>SWP_NOACTIVATE 不激活窗口 <br/>SWP_NOMOVE 保持当前位置(x和y设定将被忽略) <br/>SWP_NOREDRAW 窗口不自动重画 <br/>SWP_NOSIZE 保持当前大小(cx和cy会被忽略) <br/>SWP_NOZORDER 保持窗口在列表的当前位置(hWndInsertAfter将被忽略) <br/>SWP_SHOWWINDOW 显示窗口 <br/>SWP_FRAMECHANGED 强迫一条WM_NCCALCSIZE消息进入窗口,即使窗口的大小没有改变 <br/>====== <br/>移动无标题栏的窗口:在标准模块中声明 <br/>Declare Function ReleaseCapture Lib "user32" () As Long <br/>ReleaseCapture函数:为当前程序释放鼠标捕获。 <br/>Declare Function SendMessage Lib "user32" _ <br/>Alias "SendMessageA" ( _ <br/>ByVal hwnd As Long, ByVal wMsg As Long, _ <br/>ByVal wParam As Long, lParam As Any) As Long <br/>Public Const HTCAPTION = 2 <br/>Public Const WM_NCLBUTTONDOWN = &HA1 此消息指在窗口的非客户区域内按下左键 <br/>在FORM_mousedown事件中写: <br/>Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) <br/>ReleaseCapture (此句为释放鼠标本来在Form客户区的捕获) <br/>SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0& <br/>End Sub <br/>==== <br/>创建椭圆(不规则)窗口:SetWindowRgn函数结合CreateEllipticRgn椭圆函数 <br/>SetWindowRgn函数用来创建不规则窗口,如椭圆(结合CreateEllipticRgn),多边形(结合CreatePolygonRgn),矩形(结合CreateRectRgn),圆角矩形(结合CreateRoundRectRgn)等。 <br/>声明: <br/>Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long <br/>Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _ <br/>ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long <br/>调用如: <br/>Private Sub Form_Load() <br/>SetWindowRgn hWnd, CreateEllipticRgn(0, 0, 300, 200), True <br/>End Sub <br/>SetWindowRgn函数用于创建不规则窗口,可创建任何几何形状的窗口,只要用Create…Rgn函数返回值传入各种形状区域句柄,参数: <br/>参数 类型及说明 <br/>hWnd:将设置其区域的窗口的句柄。 <br/>hRgn:设置好的区域的句柄,一旦设置了该区域,就不能使用或修改该区域句柄,也不要删除它 <br/>bRedraw:是否立即重画窗口,若为TRUE,则立即重画窗口 <br/>注:为区域指定的所有坐标都以窗口坐标(和客户坐标不完全相同)表示,它们以整个窗口(包括标题栏和边框,而客户坐标是指不包括标题栏的窗口内部有效区域)的左上角为起点 <br/><cc></cc> 椭圆CreateEllipticRgn函数:创建一个椭圆,该椭圆以X1,Y1和X2,Y2坐标点确定的矩形内切。参数: <br/>X1,Y1:内切矩形左上角X,Y坐标 <br/>X2,Y2:内切矩形右下角X,Y坐标 <br/>==== <br/>得到屏幕有效区大小(除去任务条):SystemParametersInfoA <br/>声明: <br/>Private Type RECT <br/>Left As Long <br/>top As Long <br/>Right As Long <br/>Botton As Long <br/>End Type <br/>Private Declare Function SystemParametersInfoA Lib "user32" _ <br/>(ByVal uAction As Long, ByVal uParam As Long, ByRef lpvparam _ <br/>As Any, ByVal fuWinIni As Long) As Long <br/>调用如:将窗体移到屏幕有效区中央。 <br/>Private Sub Command2_Click() <br/>Dim ScreenWidth& <br/>Dim ScreenHeight& <br/>Dim ScreenLeft& <br/>Dim ScreenTop& <br/>Dim DesktopArea As RECT <br/>Const SPI_GETWORKAREA = 48 <br/>Call SystemParametersInfoA(SPI_GETWORKAREA, 0, DesktopArea, 0) <br/>ScreenHeight = (DesktopArea.Botton - DesktopArea.top) * Screen.TwipsPerPixelY <br/>ScreenWidth = (DesktopArea.Right - DesktopArea.Left) * Screen.TwipsPerPixelX <br/>ScreenLeft = DesktopArea.Left * Screen.TwipsPerPixelX <br/>ScreenTop = DesktopArea.top * Screen.TwipsPerPixelY <br/>Form1.Move (ScreenWidth - Form1.Width) / 2 + ScreenLeft, (ScreenHeight - Form1.Height) / 2 + ScreenTop <br/>End Sub <br/>SystemParametersInfoA函数可用来获取和设置数量众多的windows系统参数。 <br/>参数请查看资料。 <br/>==== <br/>获得窗口在屏幕上的范围:GetWindowRect函数 <br/>包括窗口的边框,标题栏,滚动条及菜单,客户区等在内,即整个窗口在屏幕上所占的范围.声明: <br/>Declare Function GetWindowRect Lib "user32"(ByVal hwnd As Long, lpRect As RECT) As Long <br/>第一个参数是窗口句柄,第二个参数装载窗口范围的坐标值,为一个结构类型,声明如下: <br/>Public Type RECT <br/>Left As Long <br/>Top As Long <br/>Right As Long <br/>Bottom As Long <br/>End Type <br/>该参数返回窗口在屏幕中的范围值,单位为象素。调用如: <br/>Private Sub Command1_Click() <br/>GetWindowRect Command1.hwnd, lxx <br/>Print lxx.Left, lxx.Top, lxx.Right, lxx.Bottom <br/>End Sub <br/>=========================================== <br/>改变指定窗口的位置和大小:MoveWindow函数 <br/>相当于VB内置的Move方法,但作为API,功能当然更强大,它可对任何非本进程的窗口进行改变,声明: <br/>Declare Function MoveWindow Lib "user32"(ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long <br/>第一个参数为要移动的窗口句柄,第二,三,四,五个参数为窗口移动后的新横坐标,新纵坐标,新宽度,新高度,第六个参数为是否立即对窗口进行重画,用True或False。调用如: <br/>MoveWindow Command1.hwnd, 0, 0, 100, 100, True <br/>因为改变的是对象在父窗口中的位置,所以Command1按纽被移到窗体Form1的客户区左上角去了。 <br/>===== <br/>判断屏幕上1指定点的客户区坐标:ScreenToClient函数 <br/>判断屏幕上某点相对于指定窗口内的坐标。声明: <br/>Private Type POINTAPI <br/>x As Long <br/>y As Long <br/>End Type <br/>Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long <br/>该函数用以测量点lpPoint在句柄为hwnd的窗口内的坐标(如超越此窗口则为负数)。调用如: <br/>Private Sub Command1_Click() <br/>Dim lxn As POINTAPI <br/>lxn.x = 100: lxn.y = 100 <br/>Call ScreenToClient(Form1.hwnd, lxn) <br/>Print lxn.x, lxn.y <br/>End Sub <br/>上例在调用前的lxn参数100,100是屏幕坐标,调用函数后lxn的值是"屏幕坐标为(100,100)的点在form1中的客户坐标是多少。如返回lxn.x=41,lxn.y=38,单位仍为象素,不会变为form1内部的缇。 <br/><cc></cc> 获得窗口内以客户坐标表示的1个点的屏幕坐标 : ClientToScreen函数 <br/>该函数与上面那个正好相反。这里是已知客户坐标求屏幕坐标。注:客户坐标单位须先转为象素。 <br/>声明: <br/>Declare Function ClientToScreen Lib "user32"(ByVal hwnd As Long, lpPoint As POINTAPI) As Long <br/>参数hwnd:点所在的客户区窗口的句柄。 <br/>参数lpPoint:传入点的客户区坐标(单位要为象素),并返回点的屏幕坐标(象素)。 <br/>调用如: <br/>Dim m As POINTAPI <br/>m.X = 50: m.Y = 70 <br/>a = ClientToScreen(Form1.hwnd, m) <br/>Print m.X, m.Y <br/>该函数应用的是参数lpPoint返回值。 <br/>====== <br/>获得屏幕上某指定点所在的窗口的句柄 : WindowFromPoint函数 <br/>声明: <br/>Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long <br/>参数xPoint,yPoint是某点的"屏幕坐标"。函数返回值为包含该点的窗口句柄。 <br/>调用如: <br/>Private Sub Command2_Click() <br/>Dim hwnd As Long <br/>hwnd = WindowFromPoint(1, 1) <br/>Print hwnd <br/>End Sub <br/>上例表示屏幕上点(1,1)处在句柄为hwnd的窗口内。 <br/>此函数返回的句柄不包含隐藏、屏蔽、透明窗口的。如果要指出屏幕上某点所属的所有窗口,就请用ChildWindowFromPoint函数。 <br/>=========== 获取屏幕上某个窗口内某点的颜色值: GetPixel函数 <br/>该函数在指定设备场景中取得1个象素的颜色RGB值。 <br/>声明: <br/>Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long <br/>参数: <br/>hDC--指定一个设备场景(即显示设备描述表)的句柄。 <br/>x,y--某点在该设备场景中的(x,y)坐标。以该设备场景的坐标系统来度量。 <br/>调用如: <br/>…… <br/>hDC=GetDC(hwnd) <br/>lxn=GetPixel(hDC,50,50) <br/>Picture1.BackColor=lxn <br/>注:指定的点不能位于设备场景的区域外。如上例(50,50)在hDC所属窗口区域内 <br/>==== <br/>EnableWindow函数:让窗口拒绝接受鼠标和键盘事件 <br/>使用此函数,可以让1个窗口不响应任何鼠标键盘操作。如果是VB内部的窗体或控件,则相当于它的Enabled属性。 <br/>声明: <br/>Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long <br/>调用如: Call EnableWindow(Form1.hwnd, 0)为拒绝接受鼠标和键盘事件, <br/>调用如: Call EnableWindow(Form1.hwnd, 1)为允许接受键盘和鼠标事件。 <br/>参数:hwnd不用说,就是一个窗口句柄,fEnable参数:窗口是否响应操作。为零禁止操作窗口,非零允许操作窗口。 <br/>===== <br/>ShowWindow函数:显示或隐藏指定句柄的窗口 <br/>将1个隐藏的窗口显示出来,有意思,相当于VB自身内部控件的visible属性。而且超越于此,它还能指定显示时是否最大化,最小化等,声明如下: <br/>Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long <br/>调用如: <br/>Const SW_HIDE = 0 参数nCmdShow之一,隐藏窗口 <br/>Const SW_SHOW = 5 显示窗口 <br/>i = ShowWindow(form1.hwnd, SW_HIDE) <br/>参数: <br/>hwnd: Long,窗口句柄,要向这个窗口应用由nCmdShow指定的命令 <br/>nCmdShow: Long,为窗口指定显示或隐藏的一个命令。请用下述任何一个常数 <br/>SW_HIDE 隐藏窗口,活动状态给令一个窗口 <br/>SW_MINIMIZE 最小化窗口,活动状态给令一个窗口 <br/>SW_RESTORE 用原来的大小和位置显示一个窗口,同时令其进入活动状态 <br/>SW_SHOW=5 用当前的大小和位置显示一个窗口,同时令其进入活动状态 <br/>SW_SHOWMAXIMIZED=3 最大化窗口,并将其激活 <br/>SW_SHOWMINIMIZED=2 最小化窗口,并将其激活 <br/>SW_SHOWDEFAULT=10 按窗口本来的方式显示,并激活。 <br/>SW_SHOWMINNOACTIVE 最小化一个窗口,同时不改变活动窗口 <br/>SW_SHOWNA 用当前的大小和位置显示一个窗口,不改变活动窗口 SW_SHOWNOACTIVATE 用最近的大小和位置显示一个窗口,同时不改变活动窗口 <br/>SW_SHOWNORMAL 与SW_RESTORE相同 <br/>注:各常数的值请查阅API浏览器中的constants(常数列表)。 <br/>====== <br/>关闭指定句柄的窗口:SendMessageA的WM_CLOSE消息 <br/>SendMessageA的声明在前面有,消息WM_CLOSE的值为&H10,后两个参数为空,用0&。函数返回值若为0表示程序处理了此消息。调用如下: <br/>Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long <br/>Const WM_CLOSE = &H10 <br/>Private Sub Command1_Click() <br/>SendMessage Form1.hwnd, WM_CLOSE, 0&, 0& <br/>End Sub <br/>还有一个强制从内存中清除窗口的函数:DestroyWindow函数 <br/>其功能是直接清除掉内存中的1个窗口,返回非0表示成功,返回0表示失败。声明: <br/>Public Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long) <br/>很简单,参数只是指定1个窗口句柄。调用如:DestroyWindow Form1.hwnd。 <br/>===== <br/>寻找窗口列表中第一个符合条件的父窗口: FindWindowA函数 <br/>Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long <br/>函数返回找到的窗口的句柄。比如要查找标题为"未命名-记事本"的窗口句柄: <br/>hWndTmp = FindWindowA(vbNullString,"未命名-记事本") 二个参数均为字符串,如果某个为空,则用vbNullString或0&补上。 <br/>参数: <br/>lpClassName:某窗口类名,或设为零或vbNullString,表示接收任何类 <br/>lpWindowName:某窗口标题,或设为零或vbNullString,表示接收任何窗口标题 。 <br/>该函数常用来查找ThunderRTMain窗口类的隐藏窗口的句柄。例如: <br/>Dim hw&, cnt& <br/>Dim rttitle As String * 256 <br/>hw& = FindWindowA("ThunderRT5Main", vbNullString) <br/>cnt = GetWindowText(hw&, rttitle, 255) <br/>MsgBox Left$(rttitle, cnt), 0, "RTMain title" <br/>===== <br/>根据1个窗口句柄返回另1个相关窗口句柄 :GetWindow函数 <br/>传入一个源窗口句柄,返回另1个与之有关的窗口句柄,如源窗口的下1个兄弟窗口,第1个子窗口等。声明: <br/>Declare Function GetWindow Lib "user32"(ByVal hwnd As Long, ByVal wCmd As Long) As Long <br/>调用如: <br/>Const GW_HWNDNEXT = 2 <br/>hWndlxn = GetWindow(hWndTmp, GW_HWNDNEXT) <br/>参数:hwnd:源窗口句柄。 <br/>wCmd参数:指定结果窗口与源窗口的关系,它们建立在下述常数基础上: <br/>GW_CHILD 寻找源窗口的第一个子窗口 <br/>GW_HWNDFIRST 为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口 <br/>GW_HWNDLAST 为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口 <br/>GW_HWNDNEXT 为源窗口寻找下一个兄弟窗口 <br/>GW_HWNDPREV 为源窗口寻找前一个兄弟窗口 <br/>GW_OWNER 寻找窗口的所有者 <br/>注解 <br/>兄弟或同级是指在整个分级结构中位于同一级别的窗口。如某个窗口有五个子窗口,那五个窗口就是兄弟窗口。尽管GetWindow可用于枚举窗口,但倘若要在枚举过程中重新定位、创建和清除窗口,那么EnumWindows和EnumChildWindows更为可靠。 <br/>===== <br/>GetWindowLongA函数:获得指定窗口某方面的结构信息(返回长整数)。 <br/>问题:什么叫"窗口的结构数据信息"?就是1个窗口的诸方面情况吧,象人有姓名性别年龄等一样,窗口有"扩展样式(包含标题栏,如在Form1中表现为BorderStyle属性,有标题栏缩小、无标题栏等的组合值)","样式(包含滚动条、系统菜单、边框等可设置)","父窗口","子窗口","窗口函数"等诸多方面,见下面nIndex参数值。这些方面的内容(每1方面只能有1个当前值)就构成了窗口的结构信息。 <br/>该函数从附加窗口内存中返回1个长整数值。 <br/>
页:
[1]
2