程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB綜合教程 >> 如何自動移動Mouse

如何自動移動Mouse

編輯:VB綜合教程
事實上是使用SetCursorPos()便可以了,而它的參數是對應於螢的座標,而不是對應某一個Window的Logic座標。這個例子中的MoveCursor()所傳入的POINTAPI也是相對於螢屏的座標,指的是從點FromP移動到ToP。最後面我也付了Showje的文章,使用的方式全部不同,不管是他的或我的,都有一個地方要解決才能做為Mouse自動導引的程式,那就是Mouse在自動Move時,如何讓使用者不能移動Mouse,而這個問題就要使用JournalPlayBackHook,底下的程式中,使用EnableHook,FreeHook,這兩個函數是Copy自如何使鍵盤、Mouse失效。

'以下程式在.bas
  TypeRECT
  LeftAsLong
  ToPAsLong
  RightAsLong
  BottomAsLong
  EndType
  TypePOINTAPI
  XAsLong
  YAsLong
  EndType

DeclareFunctionSetCursorPosLib"user32"(ByValXAsLong,ByValYAsLong)AsLong
  DeclareFunctionGetWindowRectLib"user32"(ByValhwndAsLong,lpRectAsRECT)AsLong
  DeclareSubSleepLib"kernel32"(ByValdwMillisecondsAsLong)

PublicSubMoveCursor(FromPAsPOINTAPI,ToPAsPOINTAPI)
  DimstepxAsLong,stepyAsLong,kAsLong
  DimiAsLong,jAsLong,sDelayAsLong
  stepx=1
  stepy=1
  i=(ToP.X-FromP.X)
  Ifi<0Thenstepx=-1
  i=(ToP.Y-FromP.Y)
  Ifi<0Thenstepy=-1
  'CallEnableHook'如果有Includehtmapi53.htm的.bas時,會DisableMouse
  Fori=FromP.XToToP.XStepstepx
  CallSetCursorPos(i,FromP.Y)
  Sleep(1)'讓Mouse的移動慢一點,這樣效果較好
  Nexti
  Fori=FromP.YToToP.YStepstepy
  CallSetCursorPos(ToP.X,i)
  Sleep(1)
  Nexti
  'CallFreeHook'EnableMouse
  EndSub
  '以下程式在Form中,需3個Command按鍵
  PrivateSubCommand3_Click()
  Dimrect5AsRECT
  Dimp1AsPOINTAPI,p2AsPOINTAPI
  CallGetWindowRect(Command1.hwnd,rect5)'取得Command1相對於Screen的座標
  p1.X=(rect5.Left rect5.Right)2
  p1.Y=(rect5.ToP rect5.Bottom)2
  CallGetWindowRect(Command2.hwnd,rect5)
  p2.X=(rect5.Left rect5.Right)2
  p2.Y=(rect5.ToP rect5.Bottom)2

CallMoveCursor(p1,p2)'Mouse由Command1->Command2
  EndSub

另外從Showje的站有Copy以下的程式碼,也是做相同的果,只是使用的API全部不同

'以下程式在Form中,需2個Command按鍵
  '以下置於form的一般宣告區
  PrivateDeclareSubmouse_eventLib"user32"_
  (_
  ByValdwFlagsAsLong,_
  ByValdxAsLong,_
  ByValdyAsLong,_
  ByValcButtonsAsLong,_
  ByValdwExtraInfoAsLong_
  )

PrivateDeclareFunctionClientToScreenLib"user32"_
  (_
  ByValhwndAsLong,_
  lpPointAsPOINTAPI_
  )AsLong

PrivateDeclareFunctionGetSystemMetricsLib"user32"_
  (_
  ByValnIndexAsLong_
  )AsLong
  PrivateDeclareFunctionGetCursorPosLib"user32"_
  (_
  lpPointAsPOINTAPI_
  )AsLong
  

PrivateTypePOINTAPI
  xAsLong
  yAsLong
  EndType

PrivateTypeOSVERSIONINFO
  dwOSVersionInfoSizeAsLong
  dwMajorVersionAsLong
  dwMinorVersionAsLong
  dwBuildNumberAsLong
  dwPlatformIdAsLong
  szCSDVersionAsString*128
  EndType
  

PrivateConstMOUSEEVENTF_MOVE=&H1'mousemove
  PrivateConstMOUSEEVENTF_LEFTDOWN=&H2'leftbuttondown
  PrivateConstMOUSEEVENTF_LEFTUP=&H4'leftbuttonup
  PrivateConstMOUSEEVENTF_ABSOLUTE=&H8000'absolutemove
  

PrivateSubCommand1_Click()

DimptAsPOINTAPI
  Dimdl&
  Dimdestx&,desty&,curx&,cury&
  Dimdistx&,disty&
  Dimscreenx&,screeny&
  Dimfinished
  Dimptsperx&,ptspery&

pt.x=10
  pt.y=10
  dl&=ClientToScreen(Command2.hwnd,pt)

screenx&=GetSystemMetrics(0)'0表x軸

screeny&=GetSystemMetrics(1)'1表y軸

destx&=pt.x*&HFFFF&/screenx&
  desty&=pt.y*&HFFFF&/screeny&
  

ptsperx&=&HFFFF&/screenx&
  ptspery&=&HFFFF&/screeny&

'Nowmoveit
  Do
  dl&=GetCursorPos(pt)
  curx&=pt.x*&HFFFF&/screenx&
  cury&=pt.y*&HFFFF&/screeny&
  distx&=destx&-curx&
  disty&=desty&-cury&
  If(Abs(distx&)<2*ptsperx&AndAbs(disty&)<2*ptspery)Then
  'Closeenough,gotherestoftheway
  curx&=destx&
  cury&=desty&
  finished=True
  Else
  'Movecloser
  curx&=curx& Sgn(distx&)*ptsperx*2
  cury&=cury& Sgn(disty&)*ptspery*2
  EndIf
  mouse_eventMOUSEEVENTF_ABSOLUTE_
  OrMOUSEEVENTF_MOVE,curx,cury,0,0
  LoopWhileNotfinished

'到家了,按上右鍵吧!注:是左鍵,Showje的筆誤
  '以下是在(curx,cury)的座標下,模擬Mouse左鍵的downandup
  mouse_eventMOUSEEVENTF_ABSOLUTEOr_
  MOUSEEVENTF_LEFTDOWN,curx,cury,0,0

mouse_eventMOUSEEVENTF_ABSOLUTEOr_
  MOUSEEVENTF_LEFTUP,curx,cury,0,0

EndSub

PrivateSubCommand2_Click()
  MsgBox"看你往哪兒逃!哈!!"
  EndSub

->

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved