返回首页 - 当前索引 -
主  题:
在VB.NET中怎样控制鼠标?

请问在VB.NET中怎样控制鼠标的移动和点击.是指手不动鼠标,而由程序自动
控制鼠标移动和点击.
回复人: dongbeiren() ( ) 信誉:100 2003-09-24 13:25:42Z 得分:0
?

没明白
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-25 00:50:14Z 得分:0
?

比如,有的论坛网站要求积分300以上才能发帖。增长积分的方法就是在线并且不时点击链接。
它的积分增长很慢,为了尽快达到300分,我采取整晚挂在网上,人去睡觉,问题是只在线而不点击链接,分数并不会涨。所以我就想用VB.NET编一个程序,只要点击一个按钮,接下来不用人工干预,鼠标就会在屏幕上自动移动和点击.
Top
回复人: cnhgj(戏子 ■ 学习C++) ( ) 信誉:100 2003-09-25 01:36:22Z 得分:20
?

你的问题涉及到鼠标HOOK

参考一下
http://www.18ie.com/pmzhz.php
Top
回复人: yufenfeila(雨纷飞啦) ( ) 信誉:100 2003-09-25 01:53:18Z 得分:0
?

有没有关于鼠标HOOK的.NET源码或者讲解
Top
回复人: ganenpingsohucom(倾城之恋) ( ) 信誉:100 2003-09-25 11:28:52Z 得分:55
?

调用api
在Visual Baisc.net中的声明: 

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)



Public Const MOUSEEVENTF_LEFTDOWN = &H2 '模拟鼠标左键按下
Public Const MOUSEEVENTF_LEFTUP = &H4 ’模拟鼠标左键释放
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 '模拟鼠标中间键按下
Public Const MOUSEEVENTF_MIDDLEUP = &H40 '模拟鼠标中间键释放
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 '模拟鼠标右键按下
Public Const MOUSEEVENTF_RIGHTUP = &H10 '模拟鼠标右键释放
Public Const MOUSEEVENTF_MOVE = &H1 '模拟鼠标指针移动

例:

mouse_event MOUSEEVENTF_LEFTDOWN,10,10,0,0 
'在(10,10)模拟鼠标左键按下



Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-25 22:22:30Z 得分:0
?

哦,用API,我还以为VB.NET中不需要用到API就可实现呢.
原来VB.NET还是得依赖API
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-25 22:28:37Z 得分:0
?

如果用C#是不是就可以简单地直接操纵鼠标了呢?(只需:鼠标对象.某方法(参数1,
参数2,...))?
Top
回复人: cnhgj(戏子 ■ 学习C++) ( ) 信誉:100 2003-09-25 23:44:11Z 得分:0
?

今天刚收藏的一贴



namespace ClassLibrary.Hardware 
{ 

public class Mouse 
{ 
 internal const byte SM_MOUSEPRESENT = 19; 
 internal const byte SM_CMOUSEBUTTONS = 43; 
 internal const byte SM_MOUSEWHEELPRESENT = 75; 

 internal struct POINTAPI 
 { 
  internal int x; 
  internal int y; 
 } 

 internal struct RECT 
 { 
  internal int left ; 
  internal int top ; 
  internal int right ; 
  internal int bottom ; 
 } 

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")] 
 internal extern static int SwapMouseButton ( int bSwap ); 

 [System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")] 
 internal extern static int ClipCursor(ref RECT lpRect); 

 [System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )] 
 internal extern static int GetCursorPos( ref POINTAPI lpPoint ); 

 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")] 
 internal extern static bool ShowCursor ( bool bShow ) ; 

 [System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )] 
 internal extern static int EnableWindow( int hwnd , int fEnable ); 

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")]  
 internal extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ; 

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")]  
 internal extern static int SetCursorPos ( int x , int y ) ; 

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")] 
 internal extern static int GetSystemMetrics( int nIndex ); 

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")] 
 internal extern static int SetDoubleClickTime ( int wCount ); 

 [System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")] 
 internal extern static int GetDoubleClickTime() ; 

 [System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")] 
 internal extern static void Sleep ( int dwMilliseconds ) ; 

 //得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系 

 public static int FullScreenPosition_X 
 { 
  get 
  { 
  POINTAPI _POINTAPI = new POINTAPI(); 

  GetCursorPos ( ref _POINTAPI ); 
   
  return _POINTAPI.x; 
  } 
 } 
  
 public static int FullScreenPosition_Y 
 { 
  get 
  { 
  POINTAPI _POINTAPI = new POINTAPI(); 

  GetCursorPos ( ref _POINTAPI ); 
   
  return _POINTAPI.y; 
  } 
 } 

 // 隐藏 显示 鼠标 

 public static void Hide() 
 { 
  ShowCursor( false ) ; 
 } 
  
 public static void Show() 
 { 
  ShowCursor( true ) ; 
 } 

 // 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了 

 public static void Lock( System.Windows.Forms.Form ObjectForm ) 
 { 
  RECT _FormRect = new RECT (); 
  
  GetWindowRect( ObjectForm.Handle.ToInt32() , ref _FormRect ); 
  
  ClipCursor( ref _FormRect ); 
 } 
  
 public static void UnLock() 
 { 
  RECT _ScreenRect = new RECT (); 
  
  _ScreenRect.top = 0; 
  _ScreenRect.left = 0; 
  _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom; 
  _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right; 
   
  ClipCursor( ref _ScreenRect ); 
 } 

 // 鼠标失效,不过失效的好像不只是鼠标,小心哦 

 public static void Disable( System.Windows.Forms.Form ObjectForm ) 
 { 
  EnableWindow( ObjectForm.Handle.ToInt32() , 0 ) ; 
 } 

 public static void Enable( System.Windows.Forms.Form ObjectForm ) 
 { 
  EnableWindow( ObjectForm.Handle.ToInt32() , 1 ) ; 
 } 

 // 鼠标自己移动 很想动画哦 参数是2个控件的handle 
 // 看这个方法前,先用凉水擦把脸。。。 反正我写的时候 头晕 

 public static void Move ( int From_Handle_ToInt32 , int To_Handle_ToInt32 ) 
 { 
  RECT rectFrom = new RECT () ; 
  RECT rectTo = new RECT () ; 
   
  int i ; 
  
  GetWindowRect( From_Handle_ToInt32 , ref rectFrom ) ; 
  GetWindowRect( To_Handle_ToInt32 , ref rectTo ) ; 

  if ( ( rectFrom.left + rectFrom.right ) / 2 - ( rectTo.left + rectTo.right ) / 2 > 0 ) 
  { 
  for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i >= ( rectTo.left + rectTo.right ) / 2 ; i-- ) 
  { 
   SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ; 
   Sleep ( 1 ) ; 
  } 
  } 
  else 
  { 
  for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i <= ( rectTo.left + rectTo.right ) / 2 ; i++ ) 
  { 
   SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ; 
   Sleep ( 1 ) ; 
  } 
  } 

  if ( ( rectFrom.top + rectFrom.bottom ) / 2 - ( rectTo.top + rectTo.bottom ) / 2 > 0 ) 
  { 
  for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i >= ( rectTo.top + rectTo.bottom ) / 2 ; i-- ) 
  { 
   SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ; 
   Sleep ( 1 ) ; 
  } 
  } 
  else 
  { 
  for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i <= ( rectTo.top + rectTo.bottom ) / 2 ; i++ ) 
  { 
   SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ; 
   Sleep ( 1 ) ; 
  } 
  } 
 } 
  
 // 得到你的鼠标类型 

 public static string Type 
 { 
  get 
  { 
  if ( GetSystemMetrics( SM_MOUSEPRESENT ) == 0 ) 
  { 
   return "本计算机尚未安装鼠标" ; 
  } 
  else 
  { 
   if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 ) 
   { 
   return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ; 
   } 
   else 
   { 
   return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键鼠标" ; 
   } 
  } 
  } 
 } 

 // 设置鼠标双击时间 
  
 public static void DoubleClickTime_Set( int MouseDoubleClickTime ) 
 { 
  SetDoubleClickTime( MouseDoubleClickTime ); 
 } 
  
 public static string DoubleClickTime_Get() 
 { 
  return GetDoubleClickTime().ToString() ; 
 } 

 // 设置鼠标默认主键 我是没有见过谁左手用鼠标 

 public static void DefaultRightButton() 
 { 
  SwapMouseButton ( 1 ) ; 
 } 
  
 public static void DefaultLeftButton() 
 { 
  SwapMouseButton ( 0 ) ; 
 } 
} 
}
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-26 19:20:34Z 得分:0
?

楼上的方法比用API还麻烦哦,看得不是很明白,不过还是谢谢啦!
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-26 19:25:08Z 得分:0
?

等有时间调试看看再结帖。
Top
回复人: csdnzl(郁闷中~) ( ) 信誉:100 2003-09-27 01:31:24Z 得分:10
?

最简单的方法,用宏(非VB.net)
Top
回复人: benlee(寂寞成双) ( ) 信誉:99 2003-09-27 07:56:32Z 得分:10
?

最简单的方法,鼠标精灵
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-27 15:06:42Z 得分:0
?

csdnzl(郁闷中~) :宏--不是指WORD里面的宏吧?还是--EXPLORER浏览器也有宏?
benlee(寂寞成双):鼠标精灵,软件吗?哪里可以下载?
Top
回复人: csdnzl(郁闷中~) ( ) 信誉:100 2003-09-27 17:25:45Z 得分:0
?

宏不是WORD特有的,你可以用WORD实现
Top
回复人: lzg530(小刚) ( ) 信誉:100 2003-09-27 19:38:56Z 得分:0
?

鼠标精灵你去google搜索一下就可以了
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-09-27 20:45:58Z 得分:0
?

我的鼠标是要在EXPLORER浏览器中移动和点击哦,如何用WORD中的宏实现?
Top
回复人: sakurako(最爱API) ( ) 信誉:100 2003-10-01 04:28:58Z 得分:5
?

用api可以控制鼠标的移动和点击动作等
另外可以用现成的宏软件来实现
例如 按键精灵
他不但能完成键盘鼠标的动作
还可以输入文本 等等等等
Top
回复人: fankun(虾虾) ( ) 信誉:100 2003-10-01 11:29:59Z 得分:0
?

你学会了,可以写注册机了,
向你学习
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-10-01 19:09:00Z 得分:0
?

我正在试ganenpingsohucom(倾城之恋)讲的方法,(用API)。
ganenpingsohucom(倾城之恋),能解释一下Sub mouse_event 的各个参数的意思吗?
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-10-01 23:03:09Z 得分:0
?

我用VB.NET写了一个程序,请指点:(程序清单列于最后)---本帖已加分!sorry,系统限制只能加到100分。
项目包含窗体FORM1,按钮BTN1,BTN2,及计时器TIME1,TIME2
有三个问题:
1)调试语句:Private Sub Timer2_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer2.Tick
        window.close()
    End Sub
 时出现:
    "system.windows.forms.control.window"是private,因此它在此上下文中无法访问.
2)调试语句:While Console.Read() <> CInt("q")
           End While
  时出现:
  未处理的“System.InvalidCastException”类型的异常出现在 microsoft.visualbasic.dll 中
其他信息:从字符串“q”到类型“Integer”的强制转换无效。
3)调试语句:  Private Sub object_KeyPress(ByVal keyascii As Integer)
               If keyascii = 59 Then
                 Timer1.Enabled = False
                 Timer2.Enabled = False
               End If
             End Sub
  时,按ESC键(ASCII码为59)并没有反应.

程序清单如下:
Imports System.Timers
Public Class Form1
    Inherits System.Windows.Forms.Form
    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)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 '模拟鼠标左键按下
    Public Const MOUSEEVENTF_LEFTUP = &H4 '模拟鼠标左键释放
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 '模拟鼠标中间键按下
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 '模拟鼠标中间键释放
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 '模拟鼠标右键按下
    Public Const MOUSEEVENTF_RIGHTUP = &H10 '模拟鼠标右键释放
    Public Const MOUSEEVENTF_MOVE = &H1 '模拟鼠标指针移动
    Public x, y As Integer
#Region " Windows 窗体设计器生成的代码 "(略)
#End Region

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 

btn2.Click
        Timer1.Enabled = False
        Timer2.Enabled = False
        'End
    End Sub

    Private Sub object_KeyPress(ByVal keyascii As Integer)
        If keyascii = 59 Then
            Timer1.Enabled = False
            Timer2.Enabled = False
        End If
    End Sub

    Private Sub InitializeTimer()
        Timer1.Interval = 4000
        Timer2.Interval = 20000
    End Sub

    Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
        x = CInt(Int((600 * Rnd()) + 1))
        y = CInt(Int((800 * Rnd()) + 1))
        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        '在k(x,y)模拟鼠标左键按下
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    End Sub

    Private Sub Timer2_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer2.Tick
        'window.close()
    End Sub

    'Public Shared Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
    '    Dim x, y As Integer
    '    x = CInt(Int((600 * Rnd()) + 1))
    '    y = CInt(Int((800 * Rnd()) + 1))
    '    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
    '    '在(x,y)模拟鼠标左键按下
    '    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)
    'End Sub

    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 

btn1.Click
        'Dim Timer1 As New System.Timers.Timer()
        'Dim Timer2 As New System.Timers.Timer()
        'Timer1.Interval = 4000
        'Timer2.Interval = 4000
        Timer1.Enabled = True
        Timer2.Enabled = True
        Me.WindowState = FormWindowState.Minimized
        'Press 'q' to quit the sample.
        'While Console.Read() <> CInt("q")
        'End While
    End Sub

    Friend WithEvents Timer1 As System.Windows.Forms.Timer
End Class
Top
回复人: citylamp(路灯) ( ) 信誉:100 2003-10-02 20:04:20Z 得分:0
?

收藏。
Top
回复人: cjq001(威猛先生) ( ) 信誉:100 2003-10-02 23:29:37Z 得分:0
?

请各位大哥帮忙指点指点,要分数尽管说,一定送上!
Top

结贴 ,得分记录: cnhgj (20)、 ganenpingsohucom (55)、 csdnzl (10)、 benlee (10)、 sakurako (5)、