三个星期了,真TMD的快。
昨天李鹏让我做个类似外挂的按键工具,查下资料,很难看懂的饿。
以为调用API函数就可以解决问题,貌似不可以诶。网络上查了点资料,很多饿,貌似又和钩子(hook)有关系了。
又是一个心技术,得学啊。看了半天找了点资料看着,貌似不怎么懂的饿。记得上个星期自己疯狂的复习以前基础的内容,才算了解了事件的处理机制。看来这个星期日有得忙了。
慢慢看吧虽然上面写的是hook学习,但是貌似自己还不怎么懂,基本就没有怎么学的饿。上网查了下看到这个代码,貌似和自己要的有点关系。
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
![](/Images/OutliningIndicators/None.gif)
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
![](/Images/OutliningIndicators/None.gif)
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow);
![](/Images/OutliningIndicators/None.gif)
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);
![](/Images/OutliningIndicators/None.gif)
[DllImport("user32.dll")]
static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);
![](/Images/OutliningIndicators/None.gif)
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
static extern int GetCursorPos();
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
基本当没看到。搞了半天还是不知道那个user32.dll文件是怎么来的。管它呢先看着吧。好像是调用winform的api函数来着,说只有调用它进行非托管才可以运行的呢。吊得了。不知道连操作系统里面到底有什么都不知道。
看着这个程序更是一头雾水。
先把代码弄上来。免得以后找也找不到的饿。
DemoForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
![](/Images/OutliningIndicators/None.gif)
namespace SimulateMouse
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class DemoForm : Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[StructLayout(LayoutKind.Sequential)]
struct NativeRECT
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public int left;
public int top;
public int right;
public int bottom;
}
![](/Images/OutliningIndicators/InBlock.gif)
[Flags]
enum MouseEventFlag : uint
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string strClass, string strWindow);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("user32.dll")]
static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
const int AnimationCount = 80;
private Point endPosition;
![](/Images/OutliningIndicators/InBlock.gif)
private int count;
![](/Images/OutliningIndicators/InBlock.gif)
public DemoForm()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
}
![](/Images/OutliningIndicators/InBlock.gif)
private void btnStart_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
NativeRECT rect;
![](/Images/OutliningIndicators/InBlock.gif)
IntPtr ptrTaskbar = FindWindow("Shell_TrayWnd", null);
if (ptrTaskbar == IntPtr.Zero)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("No taskbar found.");
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
IntPtr ptrStartBtn = FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "Button", null);
if (ptrStartBtn == IntPtr.Zero)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("No start button found.");
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
GetWindowRect(new HandleRef(this, ptrStartBtn), out rect);
![](/Images/OutliningIndicators/InBlock.gif)
endPosition.X = (rect.left + rect.right) / 2;
endPosition.Y = (rect.top + rect.bottom) / 2;
![](/Images/OutliningIndicators/InBlock.gif)
if (chkAnimation.Checked)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.count = AnimationCount;
movementTimer.Start();
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
private void movementTimer_Tick(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int stepx = (endPosition.X - MousePosition.X) / count;
int stepy = (endPosition.Y - MousePosition.Y) / count;
![](/Images/OutliningIndicators/InBlock.gif)
count--;
if (count == 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
movementTimer.Stop();
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
![](/Images/OutliningIndicators/InBlock.gif)
tbCursor.Text = String.Format("({0}, {1})", MousePosition.X, MousePosition.Y);
![](/Images/OutliningIndicators/InBlock.gif)
mouse_event(MouseEventFlag.Move, stepx, stepy, 0, UIntPtr.Zero);
}
}
}
倒闭光有这个还是不可以的饿。还有呢就是界面的设计
DemoForm.Designer.cs
namespace SimulateMouse
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
partial class DemoForm
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private System.ComponentModel.IContainer components = null;
![](/Images/OutliningIndicators/InBlock.gif)
protected override void Dispose(bool disposing)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (disposing && (components != null))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
base.Dispose(disposing);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer Generated Code#region Windows Form Designer Generated Code
![](/Images/OutliningIndicators/InBlock.gif)
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.components = new System.ComponentModel.Container();
this.btnStart = new System.Windows.Forms.Button();
this.movementTimer = new System.Windows.Forms.Timer(this.components);
this.chkAnimation = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.tbCursor = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(12, 12);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start!";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// movementTimer
//
this.movementTimer.Interval = 30;
this.movementTimer.Tick += new System.EventHandler(this.movementTimer_Tick);
//
// chkAnimation
//
this.chkAnimation.AutoSize = true;
this.chkAnimation.Location = new System.Drawing.Point(93, 15);
this.chkAnimation.Name = "chkAnimation";
this.chkAnimation.Size = new System.Drawing.Size(151, 18);
this.chkAnimation.TabIndex = 1;
this.chkAnimation.Text = "Animate mouse trial";
this.chkAnimation.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 52);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(103, 14);
this.label1.TabIndex = 2;
this.label1.Text = "Cursor Position";
//
// tbCursor
//
this.tbCursor.BackColor = System.Drawing.Color.Lavender;
this.tbCursor.Location = new System.Drawing.Point(121, 49);
this.tbCursor.Name = "tbCursor";
this.tbCursor.ReadOnly = true;
this.tbCursor.Size = new System.Drawing.Size(118, 22);
this.tbCursor.TabIndex = 3;
//
// DemoForm
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(256, 84);
this.Controls.Add(this.tbCursor);
this.Controls.Add(this.label1);
this.Controls.Add(this.chkAnimation);
this.Controls.Add(this.btnStart);
this.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "DemoForm";
this.Text = "Mouse event demo";
this.ResumeLayout(false);
this.PerformLayout();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Timer movementTimer;
private System.Windows.Forms.CheckBox chkAnimation;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tbCursor;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
娟儿说她要来呢我都吧知道自己是怎么想的,貌似还在爱着呢。也许可能是真的还爱着呢吧