VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#控制鼠标操作

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

  控制鼠标操作包括很多种,如限定鼠标的移动范围、设置鼠标的左右键、控制鼠标的显示和隐藏等。本节中将通过两个具体的示例来介绍有关控制鼠标操作方面的知识。

  1.限定鼠标的移动范围

  利用API函数ClipCursor和GetWindowRect可以实现限定鼠标移动范围的功能。API函数声明如下:

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

  示例 控制鼠标移动

  本示例通过API函数ClipCursor和GetWindowRect实现了限定鼠标移动范围的功能。

  程序主要代码如下。

  单击【控制鼠标移动】按钮,鼠标只能在窗体中移动,关键代码如下:public struct RECT//声明参数的值
    {
      public int left;
      public int top;
      public int right;
      public int bottom;
    } 
    public void Lock(System.Windows.Forms.Form ObjectForm)
    {
      RECT _FormRect = new RECT();
      GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
      ClipCursor(ref _FormRect);
    }
单击【恢复移动】按钮,鼠标恢复移动,关键代码如下:    public 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);    }

 

  2.鼠标设置

  设置鼠标包括设置鼠标的左右键、显示与隐藏鼠标和设置双击鼠标的时间间隔等。通常使用API函数SwapMouseButton、ShowCursor、SetDoubleClickTime和GetDoubleClickTime对鼠标进行设置。这几个函数的声明如下:

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
    public extern static int SwapMouseButton(int bSwap);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
    public extern static bool ShowCursor(bool bShow);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
    public extern static int SetDoubleClickTime(int wCount);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
    public extern static int GetDoubleClickTime();

  示例 鼠标设置

  本示例通过API函数对鼠标进行设置,通过SwapMouseButton函数实现隐藏鼠标光标, ShowCursor函数实现显示鼠标光标,SetDoubleClickTime函数设置鼠标双击时间,GetDoubleClickTime函数获取鼠标双击时间。

  程序主要代码如下。

  单击【获取鼠标双击时间】按钮,获取双击时间,并在消息框中显示,关键代码如下:public string DoubleClickTime_Get()
    {
      return GetDoubleClickTime().ToString();
    }
单击【设置鼠标双击时间】按钮,在文本框中输入设置时间,关键代码如下:   public void DoubleClickTime_Set(int MouseDoubleClickTime)
    {
      SetDoubleClickTime(MouseDoubleClickTime);
    }
单击【隐藏鼠标】按钮,鼠标在窗体上隐藏,关键代码如下:    public void Hide()
    {
      ShowCursor(false);    }

 

  单击【显示鼠标】按钮,鼠标显示,关键代码如下:

    public void Show()
    {
      ShowCursor(true);    }

  单击【鼠标左键】按钮,鼠标用左键控制鼠标单击事件,关键代码如下:

   private void bntLeft_Click(object sender, EventArgs e)
    {
      this.DefaultLeftButton();    }

  单击【鼠标右键】按钮,鼠标用右键控制鼠标单击事件,关键代码如下:

    private void bntRight_Click(object sender, EventArgs e)
    {
      this.DefaultRightButton();    }

 

 

  2.鼠标设置

  设置鼠标包括设置鼠标的左右键、显示与隐藏鼠标和设置双击鼠标的时间间隔等。通常使用API函数SwapMouseButton、ShowCursor、SetDoubleClickTime和GetDoubleClickTime对鼠标进行设置。这几个函数的声明如下:

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
    public extern static int SwapMouseButton(int bSwap);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
    public extern static bool ShowCursor(bool bShow);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
    public extern static int SetDoubleClickTime(int wCount);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
    public extern static int GetDoubleClickTime();

  示例 鼠标设置

  本示例通过API函数对鼠标进行设置,通过SwapMouseButton函数实现隐藏鼠标光标, ShowCursor函数实现显示鼠标光标,SetDoubleClickTime函数设置鼠标双击时间,GetDoubleClickTime函数获取鼠标双击时间。

  程序主要代码如下。

  单击【获取鼠标双击时间】按钮,获取双击时间,并在消息框中显示,关键代码如下:public string DoubleClickTime_Get()
    {
      return GetDoubleClickTime().ToString();
    }
单击【设置鼠标双击时间】按钮,在文本框中输入设置时间,关键代码如下:   public void DoubleClickTime_Set(int MouseDoubleClickTime)
    {
      SetDoubleClickTime(MouseDoubleClickTime);
    }
单击【隐藏鼠标】按钮,鼠标在窗体上隐藏,关键代码如下:    public void Hide()
    {
      ShowCursor(false);    }

 

  单击【显示鼠标】按钮,鼠标显示,关键代码如下:

    public void Show()
    {
      ShowCursor(true);    }

  单击【鼠标左键】按钮,鼠标用左键控制鼠标单击事件,关键代码如下:

   private void bntLeft_Click(object sender, EventArgs e)
    {
      this.DefaultLeftButton();    }

  单击【鼠标右键】按钮,鼠标用右键控制鼠标单击事件,关键代码如下:

    private void bntRight_Click(object sender, EventArgs e)
    {
      this.DefaultRightButton();    }

 



相关教程