现在的位置: 首页 > 综合 > 正文

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

2012年04月07日 ⁄ 综合 ⁄ 共 5371字 ⁄ 字号 评论关闭

(一).功能

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

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

(二).代码

  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(88120);
 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(48208);
 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(614);
 80    this.ClientSize = new System.Drawing.Size(292266);
 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 

(三).示例代码下载

   http://www.cnblogs.com/Files/ChengKing/模拟鼠标.rar

(四). 相关文章

          1. 在WebForm(Asp.net页面)中实现拖动效果, 请看:

               http://blog.csdn.net/ChengKing/archive/2006/08/30/1142605.aspx

          2. 在WinForm中实现拖动效果, 请看:

               http://blog.csdn.net/chengking/archive/2005/10/07/496739.aspx

抱歉!评论已关闭.