wyj7485 发表于 2007-1-30 15:19:00

VB做外挂编程入门(转)

1.查找目标窗口.需要做外挂,就需要查找目标窗口.然后才做一些其他的动作.比如说鼠标键盘模拟啦.内存修改啦.封包型发送与替换啦什么什么的 <br/>------------------------------------------------------------------------------------------------- <br/>'定义模块 <br/>Declare&nbsp;Function&nbsp;FindWindow&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"FindWindowA"&nbsp;(ByVal&nbsp;lpClassName&nbsp;As&nbsp;String,&nbsp;ByVal&nbsp;lpWindowName&nbsp;As&nbsp;String)&nbsp;As&nbsp;Long <br/>'在窗口中建立一timer(时间控制器),然后在代码窗口输入如下代码: <br/>Private&nbsp;Sub&nbsp;Form_Load() <br/>Timer1.Interval&nbsp;=&nbsp;500 <br/>End&nbsp;Sub <br/><br/>时间控制器的代码如下: <br/>Private&nbsp;Sub&nbsp;Timer1_Timer() <br/>&nbsp;Dim&nbsp;hwnd&nbsp;As&nbsp;Long <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hwnd&nbsp;=&nbsp;FindWindow(vbNullString,&nbsp;"计算器")&nbsp;'抓取"计算器"这个窗口名称. <br/>If&nbsp;(hwnd&nbsp;=&nbsp;0)&nbsp;Then <br/>&nbsp;&nbsp;If&nbsp;MsgBox("你没有打开[计算器]程序!点击“确定”退出。点“取消”继续。",&nbsp;49,&nbsp;"错误!")&nbsp;=&nbsp;1&nbsp;Then&nbsp;End <br/>ElseIf&nbsp;(hwnd&nbsp;&lt;&gt;&nbsp;0)&nbsp;Then <br/>MsgBox&nbsp;"你已经打开了[计算器]程序.点“确定”退出本程序",&nbsp;,&nbsp;"退出" <br/>End <br/>End&nbsp;If <br/>End&nbsp;Sub <br/>

wyj7485 发表于 2007-1-30 15:20:00

2.以下为模拟键盘事件.比如模拟"r"键. <br/>---------------------------------------------------------------------------------------------------------------------------- <br/>&nbsp;'在模块中定义&nbsp; <br/>&nbsp;Public&nbsp;Declare&nbsp;Sub&nbsp;keybd_event&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;bVk&nbsp;As&nbsp;Byte,&nbsp;ByVal&nbsp;Scan&nbsp;As&nbsp;Byte,&nbsp;ByVal&nbsp;dwFlags&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;dwExtraInfo&nbsp;As&nbsp;Long)&nbsp; <br/><br/>在窗口中建立一timer.时间间隔随意.只要不是0就可以了哦.呵呵. <br/>Private&nbsp;Sub&nbsp;Timer1_Timer() <br/>Call&nbsp;keybd_event(82,&nbsp;0,&nbsp;0,&nbsp;0)&nbsp;'模拟按下"R"键 <br/>End&nbsp;Sub<cc></cc>

wyj7485 发表于 2007-1-30 15:21:00

3.以下为快捷键例子.比如按下"ctrl+A"就退出! <br/>'可以设置Form的KeyPreview属性为True,然后在Form_KeyDown事件中添加代码:&nbsp; <br/>&nbsp;Private&nbsp;Sub&nbsp;Form_KeyDown(KeyCode&nbsp;As&nbsp;Integer,&nbsp;Shift&nbsp;As&nbsp;Integer)&nbsp; <br/>If&nbsp;KeyCode&nbsp;=&nbsp;Asc("A")&nbsp;And&nbsp;Shift&nbsp;=&nbsp;vbCtrlMask&nbsp;Then&nbsp;unload&nbsp;me&nbsp;'如果ctrl+A键被按下就退出 <br/>&nbsp;End&nbsp;Sub&nbsp; <br/><br/><br/>例二: <br/>在Form中加入 <br/>Private&nbsp;Declare&nbsp;Function&nbsp;GetAsyncKeyState&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;vkey&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Integer <br/>Private&nbsp;Function&nbsp;MyHotKey(vKeyCode)&nbsp;As&nbsp;Boolean <br/>MyHotKey&nbsp;=&nbsp;(GetAsyncKeyState(vKeyCode)&nbsp;&lt;&nbsp;0) <br/>&nbsp;End&nbsp;Function <br/>'然后在循环中或Timer的Timer事件中检测: <br/>Private&nbsp;Sub&nbsp;Timer1_Timer() <br/>If&nbsp;MyHotKey(vbKeyA)&nbsp;And&nbsp;vbKeyControl&nbsp;Then&nbsp;'ctrl+A <br/>End&nbsp;'关闭 <br/>End&nbsp;If <br/>'其中vbkeyA是键盘〃A〃的常数,其他键可按F1查得。 <br/>End&nbsp;Sub<cc></cc>

wyj7485 发表于 2007-1-30 15:22:00

取得窗口的句柄.类.名称等&nbsp; <br/><br/>建立三个label1/label2/lebel3/ <br/>名称分别为窗口句柄/类/标题/ <br/>建立一个text窗口 <br/>建立二个command按钮,一为开始抓取。一为退出 <br/><br/>声明: <br/>Private&nbsp;Type&nbsp;POINTAPI <br/>&nbsp;x&nbsp;As&nbsp;Long <br/>&nbsp;y&nbsp;As&nbsp;Long <br/>End&nbsp;Type <br/>Private&nbsp;Declare&nbsp;Function&nbsp;GetCursorPos&nbsp;Lib&nbsp;"user32"&nbsp;(lpPoint&nbsp;As&nbsp;POINTAPI)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;SetWindowPos&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;hWndInsertAfter&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;x&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;y&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;cx&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;cy&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wFlags&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;GetClassName&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"GetClassNameA"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;lpClassName&nbsp;As&nbsp;String,&nbsp;ByVal&nbsp;nMaxCount&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;SendMessage&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"SendMessageA"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wMsg&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wParam&nbsp;As&nbsp;Long,&nbsp;lParam&nbsp;As&nbsp;Any)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;WindowFromPoint&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;xPoint&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;yPoint&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;GetWindowLong&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"GetWindowLongA"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;nIndex&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;GetWindowText&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"GetWindowTextA"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;lpString&nbsp;As&nbsp;String,&nbsp;ByVal&nbsp;cch&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/><br/>Private&nbsp;Sub&nbsp;Command1_Click() <br/>If&nbsp;Command1.Caption&nbsp;=&nbsp;"开始抓取(&amp;S)"&nbsp;Then <br/>Timer1.Enabled&nbsp;=&nbsp;True <br/>Command1.Caption&nbsp;=&nbsp;"停止抓取(&amp;S)" <br/>Else <br/>Timer1.Enabled&nbsp;=&nbsp;False <br/>Command1.Caption&nbsp;=&nbsp;"开始抓取(&amp;S)" <br/>End&nbsp;If <br/>End&nbsp;Sub <br/><br/>Private&nbsp;Sub&nbsp;Command2_Click() <br/>End <br/>End&nbsp;Sub <br/><br/>Private&nbsp;Sub&nbsp;Form_Load() <br/>SetWindowPos&nbsp;Me.hwnd,&nbsp;-1,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;&amp;H1&nbsp;Or&nbsp;&amp;H2&nbsp;'使窗体位于最顶端 <br/>End&nbsp;Sub <br/><br/>Private&nbsp;Sub&nbsp;Timer1_Timer() <br/>On&nbsp;Error&nbsp;Resume&nbsp;Next <br/>Dim&nbsp;tPoint&nbsp;As&nbsp;POINTAPI <br/>Dim&nbsp;hWin&nbsp;As&nbsp;Long <br/>Dim&nbsp;str&nbsp;As&nbsp;String&nbsp;*&nbsp;255 <br/>Dim&nbsp;Abc&nbsp;As&nbsp;String&nbsp;*&nbsp;64000 <br/>Dim&nbsp;Txt(64000)&nbsp;As&nbsp;Byte <br/>GetCursorPos&nbsp;tPoint&nbsp;'获得当前鼠标位置 <br/>hWin&nbsp;=&nbsp;WindowFromPoint(tPoint.x,&nbsp;tPoint.y)&nbsp;'获得窗口名柄 <br/>If&nbsp;hWin&nbsp;=&nbsp;Me.hwnd&nbsp;Or&nbsp;hWin&nbsp;=&nbsp;Command1.hwnd&nbsp;Or&nbsp;hWin&nbsp;=&nbsp;Command2.hwnd&nbsp;Or&nbsp;hWin&nbsp;=&nbsp;Text1.hwnd&nbsp;Then&nbsp;Exit&nbsp;Sub&nbsp;'确定窗口不在&nbsp;Form1&nbsp;中 <br/>GetClassName&nbsp;hWin,&nbsp;str,&nbsp;255&nbsp;'获得窗口类 <br/>SendMessage&nbsp;hWin,&nbsp;&amp;HD,&nbsp;64000,&nbsp;Txt(0)&nbsp;'获得窗口标题(也可使用&nbsp;API&nbsp;函数:GetWindowText,但效果不佳) <br/>Label1.Caption&nbsp;=&nbsp;"窗口名柄:&nbsp;"&nbsp;&amp;&nbsp;hWin <br/>Label2.Caption&nbsp;=&nbsp;"窗口类:&nbsp;"&nbsp;&amp;&nbsp;str <br/>Text1.Text&nbsp;=&nbsp;StrConv(Txt,&nbsp;vbUnicode) <br/>End&nbsp;Sub<cc></cc>

wyj7485 发表于 2007-1-30 15:25:00

2.模拟鼠标显示.隐藏&nbsp; <br/>隐藏/显示鼠标. <br/><br/>Public&nbsp;Declare&nbsp;Function&nbsp;ShowCursor&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;bShow&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/><br/>'forml中函数如下 <br/>'隐藏鼠标(需要事件击活,比如窗体事件等) <br/>ShowCursor&nbsp;False <br/>'显示鼠标(需要事件击活,比如窗体事件等) <br/>ShowCursor&nbsp;True <br/>&nbsp;3.定位鼠标,使之不能移动&nbsp; <br/>定位鼠标。 <br/><br/>Type&nbsp;rect <br/>&nbsp;sbleft&nbsp;As&nbsp;Long <br/>&nbsp;sbtop&nbsp;As&nbsp;Long <br/>&nbsp;sbright&nbsp;As&nbsp;Long <br/>&nbsp;sbbottom&nbsp;As&nbsp;Long <br/>End&nbsp;Type <br/><br/>Public&nbsp;Declare&nbsp;Function&nbsp;ClipCursor&nbsp;Lib&nbsp;"user32"&nbsp;(lpRect&nbsp;As&nbsp;Any)&nbsp;As&nbsp;Long <br/><br/><br/>'鼠标定位 <br/>Private&nbsp;Sub&nbsp;Form_Load() <br/>'定位鼠标 <br/>&nbsp;Dim&nbsp;x&nbsp;As&nbsp;Long,&nbsp;y&nbsp;As&nbsp;Long <br/>&nbsp;Dim&nbsp;newrect&nbsp;As&nbsp;rect <br/>&nbsp;x&amp;&nbsp;=&nbsp;Screen.TwipsPerPixelX <br/>&nbsp;y&amp;&nbsp;=&nbsp;Screen.TwipsPerPixelY <br/>&nbsp; <br/>&nbsp;With&nbsp;newrect&nbsp;'鼠标只能在500,500-500,500这个范围内移动,如果四个数一样也可以说锁定鼠标了.如果加在记时器里的话就移动不了啦. <br/>&nbsp;.sbleft&nbsp;=&nbsp;500 <br/>&nbsp;.sbtop&nbsp;=&nbsp;500 <br/>&nbsp;.sbright&nbsp;=&nbsp;500 <br/>&nbsp;.sbbottom&nbsp;=&nbsp;500 <br/>&nbsp;End&nbsp;With <br/>&nbsp; <br/>&nbsp;ClipCursor&nbsp;newrect <br/>如果鼠标被锁定,不能恢复怎么办?不用担心.看如下代码. <br/>'使鼠标恢复(设定一个事件.才好击活这个代码.) <br/>&nbsp;Dim&nbsp;newrect&nbsp;As&nbsp;rect <br/>&nbsp;With&nbsp;newrect&nbsp;'这样鼠标又可以在0,0-屏幕的最右角,屏幕的最右下脚移动了 <br/>&nbsp;.sbleft&nbsp;=&nbsp;0 <br/>&nbsp;.sbtop&nbsp;=&nbsp;0 <br/>&nbsp;.sbright&nbsp;=&nbsp;Screen.Width&nbsp;/&nbsp;Screen.TwipsPerPixelX <br/>&nbsp;.sbbottom&nbsp;=&nbsp;Screen.Height&nbsp;/&nbsp;Screen.TwipsPerPixelY <br/>&nbsp;End&nbsp;With <br/>&nbsp; <br/>&nbsp;ClipCursor&nbsp;newrect <br/>&nbsp;End&nbsp;Sub<cc></cc>

wyj7485 发表于 2007-1-30 15:27:00

一些控制鼠标的例子!&nbsp; <br/>&nbsp;1.模拟鼠标击键过程&nbsp; <br/>'声明: <br/>Option&nbsp;Explicit <br/>&nbsp;Private&nbsp;Declare&nbsp;Sub&nbsp;mouse_event&nbsp;Lib&nbsp;"user32"&nbsp;(&nbsp;ByVal&nbsp;dwFlags&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;dx&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;dy&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;cButtons&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;dwExtraInfo&nbsp;As&nbsp;Long&nbsp;) <br/><br/>'对变量的定义 <br/>Const&nbsp;MOUSEEVENTF_LEFTDOWN&nbsp;=&nbsp;&amp;H2 <br/>Const&nbsp;MOUSEEVENTF_LEFTUP&nbsp;=&nbsp;&amp;H4 <br/>Const&nbsp;MOUSEEVENTF_MIDDLEDOWN&nbsp;=&nbsp;&amp;H20 <br/>Const&nbsp;MOUSEEVENTF_MIDDLEUP&nbsp;=&nbsp;&amp;H40 <br/>Const&nbsp;MOUSEEVENTF_MOVE&nbsp;=&nbsp;&amp;H1 <br/>Const&nbsp;MOUSEEVENTF_ABSOLUTE&nbsp;=&nbsp;&amp;H8000 <br/>Const&nbsp;MOUSEEVENTF_RIGHTDOWN&nbsp;=&nbsp;&amp;H8 <br/>Const&nbsp;MOUSEEVENTF_RIGHTUP&nbsp;=&nbsp;&amp;H10 <br/><br/>&nbsp;'这里是&nbsp;鼠标左键按下&nbsp;和松开两个事件的组合即一次单击 <br/>&nbsp;mouse_event&nbsp;MOUSEEVENTF_LEFTDOWN&nbsp;Or&nbsp;MOUSEEVENTF_LEFTUP,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0 <br/><br/>&nbsp;'模拟鼠标右键单击事件 <br/>&nbsp;mouse_event&nbsp;MOUSEEVENTF_RIGHTDOWN&nbsp;Or&nbsp;MOUSEEVENTF_RIGHTUP,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0 <br/><br/>&nbsp;'两次连续的鼠标左键单击事件&nbsp;构成一次鼠标双击事件 <br/>&nbsp;mouse_event&nbsp;MOUSEEVENTF_LEFTDOWN&nbsp;Or&nbsp;MOUSEEVENTF_LEFTUP,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0 <br/>&nbsp;mouse_event&nbsp;MOUSEEVENTF_LEFTDOWN&nbsp;Or&nbsp;MOUSEEVENTF_LEFTUP,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0<cc></cc>

wyj7485 发表于 2007-1-30 15:28:00

窗口类函数&nbsp; <br/><br/><br/>SetWindowPos函数:使窗口停留在屏幕最顶层。 <br/>声明: <br/>Private&nbsp;Declare&nbsp;Function&nbsp;SetWindowPos&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;_ <br/>ByVal&nbsp;hWndinsertAfter&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;x&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;y&nbsp;As&nbsp;Long,&nbsp;_ <br/>ByVal&nbsp;cx&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;cy&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wFlags&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>调用如: <br/>Private&nbsp;Sub&nbsp;Form_Load() <br/>SetWindowPos&nbsp;hwnd,&nbsp;HWND_TOPMOST,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;0,&nbsp;&amp;H2&nbsp;+&nbsp;&amp;H1 <br/>End&nbsp;Sub&nbsp;窗口form1将保留在屏幕表面。 <br/>该函数功能是为窗口指定1个新的位置和状态。参数: <br/>hwnd:欲定位的窗口。 <br/>hwndinsertAfter:指定窗口的位置。&nbsp;可能选用下述值之一:&nbsp; <br/>HWND_BOTTOM&nbsp;将窗口置于窗口列表底部&nbsp; <br/>HWND_TOP&nbsp;将窗口置于Z序列的顶部;Z序列代表在分级结构中,窗口针对一个给定级别的窗口显示的顺序&nbsp; <br/>HWND_TOPMOST(值-1)&nbsp;将窗口置于列表顶部,并位于任何最顶部窗口的前面&nbsp; <br/>HWND_NOTOPMOST(值-2)&nbsp;将窗口置于列表顶部,并位于任何最顶部窗口的后面&nbsp; <br/>x:&nbsp;窗口新的x坐标。如hwnd是一个子窗口,则x用父窗口的客户区坐标表示&nbsp; <br/>y:&nbsp;窗口新的y坐标。如hwnd是一个子窗口,则y用父窗口的客户区坐标表示&nbsp; <br/>cx:指定新的窗口宽度&nbsp; <br/>cy:指定新的窗口高度&nbsp; <br/>wFlags:包含了游标的一个整数,可能为下述值或其组合。&nbsp; <br/>SWP_DRAWFRAME&nbsp;围绕窗口画一个框&nbsp; <br/>SWP_HIDEWINDOW&nbsp;隐藏窗口&nbsp; <br/>SWP_NOACTIVATE&nbsp;不激活窗口&nbsp; <br/>SWP_NOMOVE&nbsp;保持当前位置(x和y设定将被忽略)&nbsp; <br/>SWP_NOREDRAW&nbsp;窗口不自动重画&nbsp; <br/>SWP_NOSIZE&nbsp;保持当前大小(cx和cy会被忽略)&nbsp; <br/>SWP_NOZORDER&nbsp;保持窗口在列表的当前位置(hWndInsertAfter将被忽略)&nbsp; <br/>SWP_SHOWWINDOW&nbsp;显示窗口&nbsp; <br/>SWP_FRAMECHANGED&nbsp;强迫一条WM_NCCALCSIZE消息进入窗口,即使窗口的大小没有改变&nbsp; <br/>====== <br/>移动无标题栏的窗口:在标准模块中声明 <br/>Declare&nbsp;Function&nbsp;ReleaseCapture&nbsp;Lib&nbsp;"user32"&nbsp;()&nbsp;As&nbsp;Long <br/>ReleaseCapture函数:为当前程序释放鼠标捕获。 <br/>Declare&nbsp;Function&nbsp;SendMessage&nbsp;Lib&nbsp;"user32"&nbsp;_ <br/>Alias&nbsp;"SendMessageA"&nbsp;(&nbsp;_ <br/>ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wMsg&nbsp;As&nbsp;Long,&nbsp;_ <br/>ByVal&nbsp;wParam&nbsp;As&nbsp;Long,&nbsp;lParam&nbsp;As&nbsp;Any)&nbsp;As&nbsp;Long <br/>Public&nbsp;Const&nbsp;HTCAPTION&nbsp;=&nbsp;2 <br/>Public&nbsp;Const&nbsp;WM_NCLBUTTONDOWN&nbsp;=&nbsp;&amp;HA1&nbsp;此消息指在窗口的非客户区域内按下左键 <br/>在FORM_mousedown事件中写: <br/>Private&nbsp;Sub&nbsp;Form_MouseDown(Button&nbsp;As&nbsp;Integer,&nbsp;Shift&nbsp;As&nbsp;Integer,&nbsp;X&nbsp;As&nbsp;Single,&nbsp;Y&nbsp;As&nbsp;Single) <br/>ReleaseCapture&nbsp;(此句为释放鼠标本来在Form客户区的捕获) <br/>SendMessage&nbsp;hwnd,&nbsp;WM_NCLBUTTONDOWN,&nbsp;HTCAPTION,&nbsp;0&amp; <br/>End&nbsp;Sub <br/>==== <br/>创建椭圆(不规则)窗口:SetWindowRgn函数结合CreateEllipticRgn椭圆函数 <br/>SetWindowRgn函数用来创建不规则窗口,如椭圆(结合CreateEllipticRgn),多边形(结合CreatePolygonRgn),矩形(结合CreateRectRgn),圆角矩形(结合CreateRoundRectRgn)等。&nbsp; <br/>声明: <br/>Private&nbsp;Declare&nbsp;Function&nbsp;CreateEllipticRgn&nbsp;Lib&nbsp;"gdi32"&nbsp;(ByVal&nbsp;X1&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;Y1&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;X2&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;Y2&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>Private&nbsp;Declare&nbsp;Function&nbsp;SetWindowRgn&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hWnd&nbsp;As&nbsp;Long,&nbsp;_ <br/>ByVal&nbsp;hRgn&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;bRedraw&nbsp;As&nbsp;Boolean)&nbsp;As&nbsp;Long <br/>调用如: <br/>Private&nbsp;Sub&nbsp;Form_Load() <br/>SetWindowRgn&nbsp;hWnd,&nbsp;CreateEllipticRgn(0,&nbsp;0,&nbsp;300,&nbsp;200),&nbsp;True <br/>End&nbsp;Sub <br/>SetWindowRgn函数用于创建不规则窗口,可创建任何几何形状的窗口,只要用Create…Rgn函数返回值传入各种形状区域句柄,参数: <br/>参数&nbsp;类型及说明&nbsp; <br/>hWnd:将设置其区域的窗口的句柄。&nbsp; <br/>hRgn:设置好的区域的句柄,一旦设置了该区域,就不能使用或修改该区域句柄,也不要删除它&nbsp; <br/>bRedraw:是否立即重画窗口,若为TRUE,则立即重画窗口&nbsp; <br/>注:为区域指定的所有坐标都以窗口坐标(和客户坐标不完全相同)表示,它们以整个窗口(包括标题栏和边框,而客户坐标是指不包括标题栏的窗口内部有效区域)的左上角为起点 <br/><cc></cc>

wyj7485 发表于 2007-1-30 15:32:00

椭圆CreateEllipticRgn函数:创建一个椭圆,该椭圆以X1,Y1和X2,Y2坐标点确定的矩形内切。参数: <br/>X1,Y1:内切矩形左上角X,Y坐标&nbsp; <br/>X2,Y2:内切矩形右下角X,Y坐标&nbsp; <br/>==== <br/>得到屏幕有效区大小(除去任务条):SystemParametersInfoA <br/>声明: <br/>Private&nbsp;Type&nbsp;RECT&nbsp; <br/>Left&nbsp;As&nbsp;Long <br/>top&nbsp;As&nbsp;Long <br/>Right&nbsp;As&nbsp;Long <br/>Botton&nbsp;As&nbsp;Long <br/>End&nbsp;Type <br/>Private&nbsp;Declare&nbsp;Function&nbsp;SystemParametersInfoA&nbsp;Lib&nbsp;"user32"&nbsp;_ <br/>(ByVal&nbsp;uAction&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;uParam&nbsp;As&nbsp;Long,&nbsp;ByRef&nbsp;lpvparam&nbsp;_ <br/>As&nbsp;Any,&nbsp;ByVal&nbsp;fuWinIni&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>调用如:将窗体移到屏幕有效区中央。 <br/>Private&nbsp;Sub&nbsp;Command2_Click() <br/>Dim&nbsp;ScreenWidth&amp; <br/>Dim&nbsp;ScreenHeight&amp; <br/>Dim&nbsp;ScreenLeft&amp; <br/>Dim&nbsp;ScreenTop&amp; <br/>Dim&nbsp;DesktopArea&nbsp;As&nbsp;RECT <br/>Const&nbsp;SPI_GETWORKAREA&nbsp;=&nbsp;48&nbsp; <br/>Call&nbsp;SystemParametersInfoA(SPI_GETWORKAREA,&nbsp;0,&nbsp;DesktopArea,&nbsp;0) <br/>ScreenHeight&nbsp;=&nbsp;(DesktopArea.Botton&nbsp;-&nbsp;DesktopArea.top)&nbsp;*&nbsp;Screen.TwipsPerPixelY <br/>ScreenWidth&nbsp;=&nbsp;(DesktopArea.Right&nbsp;-&nbsp;DesktopArea.Left)&nbsp;*&nbsp;Screen.TwipsPerPixelX <br/>ScreenLeft&nbsp;=&nbsp;DesktopArea.Left&nbsp;*&nbsp;Screen.TwipsPerPixelX <br/>ScreenTop&nbsp;=&nbsp;DesktopArea.top&nbsp;*&nbsp;Screen.TwipsPerPixelY <br/>Form1.Move&nbsp;(ScreenWidth&nbsp;-&nbsp;Form1.Width)&nbsp;/&nbsp;2&nbsp;+&nbsp;ScreenLeft,&nbsp;(ScreenHeight&nbsp;-&nbsp;Form1.Height)&nbsp;/&nbsp;2&nbsp;+&nbsp;ScreenTop <br/>End&nbsp;Sub <br/>SystemParametersInfoA函数可用来获取和设置数量众多的windows系统参数。 <br/>参数请查看资料。 <br/>==== <br/>获得窗口在屏幕上的范围:GetWindowRect函数 <br/>包括窗口的边框,标题栏,滚动条及菜单,客户区等在内,即整个窗口在屏幕上所占的范围.声明: <br/>Declare&nbsp;Function&nbsp;GetWindowRect&nbsp;Lib&nbsp;"user32"(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;lpRect&nbsp;As&nbsp;RECT)&nbsp;As&nbsp;Long <br/>第一个参数是窗口句柄,第二个参数装载窗口范围的坐标值,为一个结构类型,声明如下: <br/>Public&nbsp;Type&nbsp;RECT <br/>Left&nbsp;As&nbsp;Long <br/>Top&nbsp;As&nbsp;Long <br/>Right&nbsp;As&nbsp;Long <br/>Bottom&nbsp;As&nbsp;Long <br/>End&nbsp;Type <br/>该参数返回窗口在屏幕中的范围值,单位为象素。调用如: <br/>Private&nbsp;Sub&nbsp;Command1_Click() <br/>GetWindowRect&nbsp;Command1.hwnd,&nbsp;lxx <br/>Print&nbsp;lxx.Left,&nbsp;lxx.Top,&nbsp;lxx.Right,&nbsp;lxx.Bottom&nbsp; <br/>End&nbsp;Sub <br/>=========================================== <br/>改变指定窗口的位置和大小:MoveWindow函数 <br/>相当于VB内置的Move方法,但作为API,功能当然更强大,它可对任何非本进程的窗口进行改变,声明: <br/>Declare&nbsp;Function&nbsp;MoveWindow&nbsp;Lib&nbsp;"user32"(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;x&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;y&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;nWidth&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;nHeight&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;bRepaint&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>第一个参数为要移动的窗口句柄,第二,三,四,五个参数为窗口移动后的新横坐标,新纵坐标,新宽度,新高度,第六个参数为是否立即对窗口进行重画,用True或False。调用如: <br/>MoveWindow&nbsp;Command1.hwnd,&nbsp;0,&nbsp;0,&nbsp;100,&nbsp;100,&nbsp;True <br/>因为改变的是对象在父窗口中的位置,所以Command1按纽被移到窗体Form1的客户区左上角去了。&nbsp; <br/>===== <br/>判断屏幕上1指定点的客户区坐标:ScreenToClient函数&nbsp; <br/>判断屏幕上某点相对于指定窗口内的坐标。声明: <br/>Private&nbsp;Type&nbsp;POINTAPI <br/>x&nbsp;As&nbsp;Long <br/>y&nbsp;As&nbsp;Long <br/>End&nbsp;Type <br/>Private&nbsp;Declare&nbsp;Function&nbsp;ScreenToClient&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;lpPoint&nbsp;As&nbsp;POINTAPI)&nbsp;As&nbsp;Long <br/>该函数用以测量点lpPoint在句柄为hwnd的窗口内的坐标(如超越此窗口则为负数)。调用如: <br/>Private&nbsp;Sub&nbsp;Command1_Click() <br/>Dim&nbsp;lxn&nbsp;As&nbsp;POINTAPI <br/>lxn.x&nbsp;=&nbsp;100:&nbsp;lxn.y&nbsp;=&nbsp;100 <br/>Call&nbsp;ScreenToClient(Form1.hwnd,&nbsp;lxn) <br/>Print&nbsp;lxn.x,&nbsp;lxn.y <br/>End&nbsp;Sub <br/>上例在调用前的lxn参数100,100是屏幕坐标,调用函数后lxn的值是"屏幕坐标为(100,100)的点在form1中的客户坐标是多少。如返回lxn.x=41,lxn.y=38,单位仍为象素,不会变为form1内部的缇。 <br/><cc></cc>

wyj7485 发表于 2007-1-30 15:34:00

获得窗口内以客户坐标表示的1个点的屏幕坐标&nbsp;:&nbsp;ClientToScreen函数 <br/>该函数与上面那个正好相反。这里是已知客户坐标求屏幕坐标。注:客户坐标单位须先转为象素。 <br/>声明: <br/>Declare&nbsp;Function&nbsp;ClientToScreen&nbsp;Lib&nbsp;"user32"(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;lpPoint&nbsp;As&nbsp;POINTAPI)&nbsp;As&nbsp;Long <br/>参数hwnd:点所在的客户区窗口的句柄。 <br/>参数lpPoint:传入点的客户区坐标(单位要为象素),并返回点的屏幕坐标(象素)。 <br/>调用如:&nbsp; <br/>Dim&nbsp;m&nbsp;As&nbsp;POINTAPI <br/>m.X&nbsp;=&nbsp;50:&nbsp;m.Y&nbsp;=&nbsp;70 <br/>a&nbsp;=&nbsp;ClientToScreen(Form1.hwnd,&nbsp;m) <br/>Print&nbsp;m.X,&nbsp;m.Y <br/>该函数应用的是参数lpPoint返回值。&nbsp; <br/>====== <br/>获得屏幕上某指定点所在的窗口的句柄&nbsp;:&nbsp;WindowFromPoint函数&nbsp; <br/>声明: <br/>Private&nbsp;Declare&nbsp;Function&nbsp;WindowFromPoint&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;xPoint&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;yPoint&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>参数xPoint,yPoint是某点的"屏幕坐标"。函数返回值为包含该点的窗口句柄。 <br/>调用如: <br/>Private&nbsp;Sub&nbsp;Command2_Click() <br/>Dim&nbsp;hwnd&nbsp;As&nbsp;Long <br/>hwnd&nbsp;=&nbsp;WindowFromPoint(1,&nbsp;1) <br/>Print&nbsp;hwnd <br/>End&nbsp;Sub <br/>上例表示屏幕上点(1,1)处在句柄为hwnd的窗口内。 <br/>此函数返回的句柄不包含隐藏、屏蔽、透明窗口的。如果要指出屏幕上某点所属的所有窗口,就请用ChildWindowFromPoint函数。 <br/>===========&nbsp;获取屏幕上某个窗口内某点的颜色值:&nbsp;GetPixel函数 <br/>该函数在指定设备场景中取得1个象素的颜色RGB值。 <br/>声明: <br/>Public&nbsp;Declare&nbsp;Function&nbsp;GetPixel&nbsp;Lib&nbsp;"gdi32"&nbsp;Alias&nbsp;"GetPixel"&nbsp;(ByVal&nbsp;hdc&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;x&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;y&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>参数: <br/>hDC--指定一个设备场景(即显示设备描述表)的句柄。 <br/>x,y--某点在该设备场景中的(x,y)坐标。以该设备场景的坐标系统来度量。 <br/>调用如: <br/>…… <br/>hDC=GetDC(hwnd) <br/>lxn=GetPixel(hDC,50,50)&nbsp; <br/>Picture1.BackColor=lxn <br/>注:指定的点不能位于设备场景的区域外。如上例(50,50)在hDC所属窗口区域内 <br/>==== <br/>EnableWindow函数:让窗口拒绝接受鼠标和键盘事件 <br/>使用此函数,可以让1个窗口不响应任何鼠标键盘操作。如果是VB内部的窗体或控件,则相当于它的Enabled属性。 <br/>声明: <br/>Private&nbsp;Declare&nbsp;Function&nbsp;EnableWindow&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;fEnable&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>调用如:&nbsp;Call&nbsp;EnableWindow(Form1.hwnd,&nbsp;0)为拒绝接受鼠标和键盘事件, <br/>调用如:&nbsp;Call&nbsp;EnableWindow(Form1.hwnd,&nbsp;1)为允许接受键盘和鼠标事件。 <br/>参数:hwnd不用说,就是一个窗口句柄,fEnable参数:窗口是否响应操作。为零禁止操作窗口,非零允许操作窗口。 <br/>===== <br/>ShowWindow函数:显示或隐藏指定句柄的窗口 <br/>将1个隐藏的窗口显示出来,有意思,相当于VB自身内部控件的visible属性。而且超越于此,它还能指定显示时是否最大化,最小化等,声明如下: <br/>Public&nbsp;Declare&nbsp;Function&nbsp;ShowWindow&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;nCmdShow&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long&nbsp; <br/>调用如: <br/>Const&nbsp;SW_HIDE&nbsp;=&nbsp;0&nbsp;参数nCmdShow之一,隐藏窗口 <br/>Const&nbsp;SW_SHOW&nbsp;=&nbsp;5&nbsp;显示窗口&nbsp; <br/>i&nbsp;=&nbsp;ShowWindow(form1.hwnd,&nbsp;SW_HIDE) <br/>参数: <br/>hwnd:&nbsp;Long,窗口句柄,要向这个窗口应用由nCmdShow指定的命令&nbsp; <br/>nCmdShow:&nbsp;Long,为窗口指定显示或隐藏的一个命令。请用下述任何一个常数&nbsp; <br/>SW_HIDE&nbsp;隐藏窗口,活动状态给令一个窗口&nbsp; <br/>SW_MINIMIZE&nbsp;最小化窗口,活动状态给令一个窗口&nbsp; <br/>SW_RESTORE&nbsp;用原来的大小和位置显示一个窗口,同时令其进入活动状态&nbsp; <br/>SW_SHOW=5&nbsp;用当前的大小和位置显示一个窗口,同时令其进入活动状态&nbsp; <br/>SW_SHOWMAXIMIZED=3&nbsp;最大化窗口,并将其激活&nbsp; <br/>SW_SHOWMINIMIZED=2&nbsp;最小化窗口,并将其激活&nbsp; <br/>SW_SHOWDEFAULT=10&nbsp;按窗口本来的方式显示,并激活。 <br/>SW_SHOWMINNOACTIVE&nbsp;最小化一个窗口,同时不改变活动窗口&nbsp; <br/>SW_SHOWNA&nbsp;用当前的大小和位置显示一个窗口,不改变活动窗口&nbsp;

wyj7485 发表于 2007-1-30 15:35:00

SW_SHOWNOACTIVATE&nbsp;用最近的大小和位置显示一个窗口,同时不改变活动窗口&nbsp; <br/>SW_SHOWNORMAL&nbsp;与SW_RESTORE相同&nbsp; <br/>注:各常数的值请查阅API浏览器中的constants(常数列表)。 <br/>====== <br/>关闭指定句柄的窗口:SendMessageA的WM_CLOSE消息&nbsp; <br/>SendMessageA的声明在前面有,消息WM_CLOSE的值为&amp;H10,后两个参数为空,用0&amp;。函数返回值若为0表示程序处理了此消息。调用如下: <br/>Private&nbsp;Declare&nbsp;Function&nbsp;SendMessage&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"SendMessageA"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wMsg&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wParam&nbsp;As&nbsp;Long,&nbsp;lParam&nbsp;As&nbsp;Any)&nbsp;As&nbsp;Long <br/>Const&nbsp;WM_CLOSE&nbsp;=&nbsp;&amp;H10 <br/>Private&nbsp;Sub&nbsp;Command1_Click() <br/>SendMessage&nbsp;Form1.hwnd,&nbsp;WM_CLOSE,&nbsp;0&amp;,&nbsp;0&amp; <br/>End&nbsp;Sub <br/>还有一个强制从内存中清除窗口的函数:DestroyWindow函数 <br/>其功能是直接清除掉内存中的1个窗口,返回非0表示成功,返回0表示失败。声明: <br/>Public&nbsp;Declare&nbsp;Function&nbsp;DestroyWindow&nbsp;Lib&nbsp;"user32"&nbsp;(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long) <br/>很简单,参数只是指定1个窗口句柄。调用如:DestroyWindow&nbsp;Form1.hwnd。 <br/>===== <br/>寻找窗口列表中第一个符合条件的父窗口:&nbsp;FindWindowA函数&nbsp; <br/>Declare&nbsp;Function&nbsp;FindWindow&nbsp;Lib&nbsp;"user32"&nbsp;Alias&nbsp;"FindWindowA"&nbsp;(ByVal&nbsp;lpClassName&nbsp;As&nbsp;String,&nbsp;ByVal&nbsp;lpWindowName&nbsp;As&nbsp;String)&nbsp;As&nbsp;Long <br/>函数返回找到的窗口的句柄。比如要查找标题为"未命名-记事本"的窗口句柄: <br/>hWndTmp&nbsp;=&nbsp;FindWindowA(vbNullString,"未命名-记事本")&nbsp;二个参数均为字符串,如果某个为空,则用vbNullString或0&amp;补上。 <br/>参数: <br/>lpClassName:某窗口类名,或设为零或vbNullString,表示接收任何类&nbsp; <br/>lpWindowName:某窗口标题,或设为零或vbNullString,表示接收任何窗口标题&nbsp;。 <br/>该函数常用来查找ThunderRTMain窗口类的隐藏窗口的句柄。例如: <br/>Dim&nbsp;hw&amp;,&nbsp;cnt&amp; <br/>Dim&nbsp;rttitle&nbsp;As&nbsp;String&nbsp;*&nbsp;256 <br/>hw&amp;&nbsp;=&nbsp;FindWindowA("ThunderRT5Main",&nbsp;vbNullString)&nbsp; <br/>cnt&nbsp;=&nbsp;GetWindowText(hw&amp;,&nbsp;rttitle,&nbsp;255) <br/>MsgBox&nbsp;Left$(rttitle,&nbsp;cnt),&nbsp;0,&nbsp;"RTMain&nbsp;title"&nbsp; <br/>===== <br/>根据1个窗口句柄返回另1个相关窗口句柄&nbsp;:GetWindow函数 <br/>传入一个源窗口句柄,返回另1个与之有关的窗口句柄,如源窗口的下1个兄弟窗口,第1个子窗口等。声明: <br/>Declare&nbsp;Function&nbsp;GetWindow&nbsp;Lib&nbsp;"user32"(ByVal&nbsp;hwnd&nbsp;As&nbsp;Long,&nbsp;ByVal&nbsp;wCmd&nbsp;As&nbsp;Long)&nbsp;As&nbsp;Long <br/>调用如: <br/>Const&nbsp;GW_HWNDNEXT&nbsp;=&nbsp;2&nbsp; <br/>hWndlxn&nbsp;=&nbsp;GetWindow(hWndTmp,&nbsp;GW_HWNDNEXT) <br/>参数:hwnd:源窗口句柄。&nbsp; <br/>wCmd参数:指定结果窗口与源窗口的关系,它们建立在下述常数基础上:&nbsp; <br/>GW_CHILD&nbsp;寻找源窗口的第一个子窗口&nbsp; <br/>GW_HWNDFIRST&nbsp;为一个源子窗口寻找第一个兄弟(同级)窗口,或寻找第一个顶级窗口&nbsp; <br/>GW_HWNDLAST&nbsp;为一个源子窗口寻找最后一个兄弟(同级)窗口,或寻找最后一个顶级窗口&nbsp; <br/>GW_HWNDNEXT&nbsp;为源窗口寻找下一个兄弟窗口&nbsp; <br/>GW_HWNDPREV&nbsp;为源窗口寻找前一个兄弟窗口&nbsp; <br/>GW_OWNER&nbsp;寻找窗口的所有者&nbsp; <br/>注解&nbsp; <br/>兄弟或同级是指在整个分级结构中位于同一级别的窗口。如某个窗口有五个子窗口,那五个窗口就是兄弟窗口。尽管GetWindow可用于枚举窗口,但倘若要在枚举过程中重新定位、创建和清除窗口,那么EnumWindows和EnumChildWindows更为可靠。 <br/>===== <br/>GetWindowLongA函数:获得指定窗口某方面的结构信息(返回长整数)。 <br/>问题:什么叫"窗口的结构数据信息"?就是1个窗口的诸方面情况吧,象人有姓名性别年龄等一样,窗口有"扩展样式(包含标题栏,如在Form1中表现为BorderStyle属性,有标题栏缩小、无标题栏等的组合值)","样式(包含滚动条、系统菜单、边框等可设置)","父窗口","子窗口","窗口函数"等诸多方面,见下面nIndex参数值。这些方面的内容(每1方面只能有1个当前值)就构成了窗口的结构信息。 <br/>该函数从附加窗口内存中返回1个长整数值。 <br/>
页: [1] 2
查看完整版本: VB做外挂编程入门(转)