c#建立在Framework之上,好像不能执行钩子,这里给个简单的代码例子,供大家学习和参考
鼠标钩子
ptOrigin.X = e.X;
ptOrigin.Y = e.Y;
//勾-股-弦
double temp = Math.Pow(ptOld.X - ptOrigin.X,2)+Math.Pow( ptOld.Y - ptOrigin.Y,2);
//计算公式:实际屏幕宽度/分辨率横坐标值 * 移动的像素
this.distance += distpara/screen*Math.Pow(temp,0.5);
label3.Text = distance.ToString("F2") + " 米";
ptOld.X = ptOrigin.X;
ptOld.Y = ptOrigin.Y;
/// 统计各个按键按下的次数 每一个keydown事件作为按键一次的判断
cc ++;
this.label4.Text = cc.ToString()+" 次";
int i =findKeyIndex(e.KeyCode);
if(i!=-1)
addKeyPress(i);
public void addKeyPress(int id)
{
counter[id].Count++;
}
public int findKeyIndex(Keys k)
{
for(int i=0;i<counter.Length;i++)
{
if(counter[i].Key == k.ToString())
return i;
}
return -1;
} #region "无标题窗口拖动相关"
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam);
public const int WM_SYSCOMMAND=0x0112;
public const int SC_MOVE=0xF010;
public const int HTCAPTION=0x0002;
private void Form1_MouseDown_1(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0);
}
最新评论