键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)

原文:http://blog.csdn.net/chengking/archive/2005/10/07/496715.aspx

(一).功能

        实现用键盘模拟鼠标移动的功能,在游戏设计中常用到

       *操作说明:  当运行程序后,放开鼠标,按键盘上的光标键移动,可以代替鼠标. 

(二).代码

  1  using  System;
  2  using  System.Drawing;
  3  using  System.Collections;
  4  using  System.ComponentModel;
  5  using  System.Windows.Forms;
  6  using  System.Data;
  7 
  8  namespace  模拟鼠标
  9  {
 10    ///   <summary>
 11    ///  Form1 的摘要说明。
 12    ///   </summary>
 13    public   class  Form1 : System.Windows.Forms.Form
 14   {
 15     ///   <summary>
 16     ///  必需的设计器变量。
 17     ///   </summary>
 18     private  System.ComponentModel.Container components  =   null ;
 19 
 20     public  Form1()
 21    {
 22      //
 23      //  Windows 窗体设计器支持所必需的
 24      //
 25     InitializeComponent();
 26 
 27      //
 28      //  TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 29      //
 30    }
 31 
 32     ///   <summary>
 33     ///  清理所有正在使用的资源。
 34     ///   </summary>
 35     protected   override   void  Dispose(  bool  disposing )
 36    {
 37      if ( disposing )
 38     {
 39       if  (components  !=   null
 40      {
 41       components.Dispose();
 42      }
 43     }
 44      base .Dispose( disposing );
 45    }
 46 
 47     #region  Windows 窗体设计器生成的代码
 48     ///   <summary>
 49     ///  设计器支持所需的方法 - 不要使用代码编辑器修改
 50     ///  此方法的内容。
 51     ///   </summary>
 52     private   void  InitializeComponent()
 53    {
 54      this .pictureBox1  =   new  System.Windows.Forms.PictureBox();
 55      this .label1  =   new  System.Windows.Forms.Label();
 56      this .SuspendLayout();
 57      //  
 58      //  pictureBox1
 59      //  
 60      this .pictureBox1.BackColor  =  System.Drawing.SystemColors.ActiveCaption;
 61      this .pictureBox1.Location  =   new  System.Drawing.Point( 88 120 );
 62      this .pictureBox1.Name  =   " pictureBox1 " ;
 63      this .pictureBox1.TabIndex  =   0 ;
 64      this .pictureBox1.TabStop  =   false ;
 65      this .pictureBox1.KeyDown  +=   new  System.Windows.Forms.KeyEventHandler( this .pictureBox1_KeyDown);
 66      this .pictureBox1.MouseDown  +=   new  System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseDown);
 67      //  
 68      //  label1
 69      //  
 70      this .label1.BackColor  =  System.Drawing.Color.DarkOliveGreen;
 71      this .label1.FlatStyle  =  System.Windows.Forms.FlatStyle.System;
 72      this .label1.Location  =   new  System.Drawing.Point( 48 208 );
 73      this .label1.Name  =   " label1 " ;
 74      this .label1.TabIndex  =   1 ;
 75      this .label1.Text  =   " label1 " ;
 76      //  
 77      //  Form1
 78      //  
 79      this .AutoScaleBaseSize  =   new  System.Drawing.Size( 6 14 );
 80      this .ClientSize  =   new  System.Drawing.Size( 292 266 );
 81      this .Controls.Add( this .label1);
 82      this .Controls.Add( this .pictureBox1);
 83      this .Name  =   " Form1 " ;
 84      this .Text  =   " Form1 " ;
 85      this .KeyDown  +=   new  System.Windows.Forms.KeyEventHandler( this .Form1_KeyDown);
 86      this .Load  +=   new  System.EventHandler( this .Form1_Load);
 87      this .KeyUp  +=   new  System.Windows.Forms.KeyEventHandler( this .Form1_KeyUp);
 88      this .MouseMove  +=   new  System.Windows.Forms.MouseEventHandler( this .Form1_MouseMove);
 89      this .ResumeLayout( false );
 90 
 91    }
 92     #endregion
 93 
 94     ///   <summary>
 95     ///  应用程序的主入口点。
 96     ///   </summary>
 97    [STAThread]
 98     static   void  Main() 
 99    {
100     Application.Run( new  Form1());
101    }
102    [System.Runtime.InteropServices.DllImport( " user32 " )]
103        private   static   extern   int  mouse_event( int  dwFlags, int  dx, int  dy, int  cButtons, int  dwExtraInfo);
104     const   int  MOUSEEVENTF_MOVE = 0x0001 ;
105     const   int  MOUSEEVENTF_LEFTDOWN = 0X0002 ;
106     const   int  MOUSEEVENTF_LEFTUP = 0X0004 ;
107     const   int  MOUSEEVENTF_RIGHTDOWN = 0X0008 ;
108     const   int  MOUSEEVENTF_RIGHTUP = 0X0010 ;
109     const   int  MOUSEEVENTF_MIDDLEDOWN = 0X0020 ;
110     const   int  MOUSEEVENTF_MIDDLEUP = 0X0040 ;
111     private  System.Windows.Forms.PictureBox pictureBox1;
112     private  System.Windows.Forms.Label label1;
113     const   int  MOUSEEVENTF_ABSOLUTE = 0X8000 ;
114 
115     private   void  Form1_MouseMove( object  sender, System.Windows.Forms.MouseEventArgs e)
116    {
117      // mouse_event(MOUSEEVENTF_MOVE,-10,-10,0,0);
118    }
119 
120     private   void  Form1_KeyDown( object  sender, System.Windows.Forms.KeyEventArgs e)
121    {
122      if (e.KeyCode == Keys.Down)
123      mouse_event(MOUSEEVENTF_MOVE, 0 , 20 , 0 , 0 );
124      if (e.KeyCode == Keys.Up)
125      mouse_event(MOUSEEVENTF_MOVE, 0 , - 20 , 0 , 0 );
126      if (e.KeyCode == Keys.Left)
127      mouse_event(MOUSEEVENTF_MOVE, - 20 , 0 , 0 , 0 );
128      if (e.KeyCode == Keys.Right)
129      mouse_event(MOUSEEVENTF_MOVE, 20 , 0 , 0 , 0 );
130    }
131 
132     public   void  pictureBox1_KeyDown( object  sender, System.Windows.Forms.KeyEventArgs e)
133    {
134      /* if(e.KeyCode==Keys.Down)
135      mouse_event(MOUSEEVENTF_MOVE,0,20,0,0);
136     if(e.KeyCode==Keys.Up)
137      mouse_event(MOUSEEVENTF_MOVE,0,-20,0,0);
138     if(e.KeyCode==Keys.Left)
139      mouse_event(MOUSEEVENTF_MOVE,-20,0,0,0);
140     if(e.KeyCode==Keys.Right)
141      mouse_event(MOUSEEVENTF_MOVE,20,0,0,0);
142       */
143    }
144     private   void  Form1_KeyUp( object  sender, System.Windows.Forms.KeyEventArgs e)
145    {
146    
147    }
148 
149    
150 
151     private   void  pictureBox1_MouseDown( object  sender, System.Windows.Forms.MouseEventArgs e)
152    {
153      if (e.Button == MouseButtons.Right)
154      mouse_event(MOUSEEVENTF_MOVE, 0 , 20 , 0 , 0 );
155      else
156      mouse_event(MOUSEEVENTF_MOVE, 0 , - 20 , 0 , 0 );
157    }
158 
159     private   void  Form1_Load( object  sender, System.EventArgs e)
160    {
161     
162    }  
163   
164   }
165  }
166 
167 

(三).示例代码下载

  模拟鼠标.rar

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值