这个需要Hook鼠标呢,点击后记录鼠标的坐标,然后再作动作; 需要使用的知识: Win32 Hook; Windows Service programming; Win32 api(about mouse operations)
function click() { document.getelementByid(this).click();} onmouseover=click()
使用鼠标钩子 如下:using System; using System.Text; using System.Runtime.InteropServices; using System.Reflection; using System.Windows.Forms;namespace MouseKeyboardLibrary { /// <summary> /// Abstract base class for Mouse and Keyboard hooks /// </summary> public abstract class GlobalHook { #region Windows API Code [StructLayout(LayoutKind.Sequential)] protected class POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] protected class MouseHookStruct { public POINT pt; public int hwnd; public int wHitTestCode; public int dwExtraInfo; } [StructLayout(LayoutKind.Sequential)] protected class MouseLLHookStruct { public POINT pt; public int mouseData; public int flags; public int time; public int dwExtraInfo; } [StructLayout(LayoutKind.Sequential)] protected class KeyboardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] protected static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] protected static extern int UnhookWindowsHookEx(int idHook); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] protected static extern int CallNextHookEx( int idHook, int nCode, int wParam, IntPtr lParam); [DllImport("user32")] protected static extern int ToAscii( int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState); [DllImport("user32")] protected static extern int GetKeyboardState(byte[] pbKeyState); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] protected static extern short GetKeyState(int vKey); protected delegate int HookProc(int nCode, int wParam, IntPtr lParam); protected const int WH_MOUSE_LL = 14; protected const int WH_KEYBOARD_LL = 13; protected const int WH_MOUSE = 7; protected const int WH_KEYBOARD = 2; protected const int WM_MOUSEMOVE = 0x200; protected const int WM_LBUTTONDOWN = 0x201; protected const int WM_RBUTTONDOWN = 0x204; protected const int WM_MBUTTONDOWN = 0x207; protected const int WM_LBUTTONUP = 0x202; protected const int WM_RBUTTONUP = 0x205; protected const int WM_MBUTTONUP = 0x208; protected const int WM_LBUTTONDBLCLK = 0x203; protected const int WM_RBUTTONDBLCLK = 0x206; protected const int WM_MBUTTONDBLCLK = 0x209; protected const int WM_MOUSEWHEEL = 0x020A; protected const int WM_KEYDOWN = 0x100; protected const int WM_KEYUP = 0x101; protected const int WM_SYSKEYDOWN = 0x104; protected const int WM_SYSKEYUP = 0x105; protected const byte VK_SHIFT = 0x10; protected const byte VK_CAPITAL = 0x14; protected const byte VK_NUMLOCK = 0x90; protected const byte VK_LSHIFT = 0xA0; protected const byte VK_RSHIFT = 0xA1; protected const byte VK_LCONTROL = 0xA2; protected const byte VK_RCONTROL = 0x3; protected const byte VK_LALT = 0xA4; protected const byte VK_RALT = 0xA5; protected const byte LLKHF_ALTDOWN = 0x20; #endregion #region Private Variables protected int _hookType; protected int _handleToHook; protected bool _isStarted; protected HookProc _hookCallback; #endregion #region Properties public bool IsStarted { get { return _isStarted; } } #endregion #region Constructor public GlobalHook() { Application.ApplicationExit += new EventHandler(Application_ApplicationExit); } #endregion #region Methods public void Start() { if (!_isStarted && _hookType != 0) { // Make sure we keep a reference to this delegate! // If not, GC randomly collects it, and a NullReference exception is thrown _hookCallback = new HookProc(HookCallbackProcedure); _handleToHook = SetWindowsHookEx( _hookType, _hookCallback, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); // Were we able to sucessfully start hook? if (_handleToHook != 0) { _isStarted = true; } } } public void Stop() { if (_isStarted) { UnhookWindowsHookEx(_handleToHook); _isStarted = false; } } protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam) {
// This method must be overriden by each extending hook return 0; } protected void Application_ApplicationExit(object sender, EventArgs e) { if (_isStarted) { Stop(); } } #endregion }}
using System; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace MouseKeyboardLibrary { /// <summary> /// Captures global mouse events /// </summary> public class MouseHook : GlobalHook { #region MouseEventType Enum private enum MouseEventType { None, MouseDown, MouseUp, DoubleClick, MouseWheel, MouseMove } #endregion #region Events public event MouseEventHandler MouseDown; public event MouseEventHandler MouseUp; public event MouseEventHandler MouseMove; public event MouseEventHandler MouseWheel; public event EventHandler Click; public event EventHandler DoubleClick; #endregion #region Constructor public MouseHook() { _hookType = WH_MOUSE_LL; } #endregion #region Methods protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam) {
if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null)) { MouseLLHookStruct mouseHookStruct = (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); MouseButtons button = GetButton(wParam); MouseEventType eventType = GetEventType(wParam); MouseEventArgs e = new MouseEventArgs( button, (eventType == MouseEventType.DoubleClick ? 2 : 1), mouseHookStruct.pt.x, mouseHookStruct.pt.y, (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0)); // Prevent multiple Right Click events (this probably happens for popup menus) if (button == MouseButtons.Right && mouseHookStruct.flags != 0) { eventType = MouseEventType.None; } switch (eventType) { case MouseEventType.MouseDown: if (MouseDown != null) { MouseDown(this, e); } break; case MouseEventType.MouseUp: if (Click != null) { Click(this, new EventArgs()); } if (MouseUp != null) { MouseUp(this, e); } break; case MouseEventType.DoubleClick: if (DoubleClick != null) { DoubleClick(this, new EventArgs()); } break; case MouseEventType.MouseWheel: if (MouseWheel != null) { MouseWheel(this, e); } break; case MouseEventType.MouseMove: if (MouseMove != null) { MouseMove(this, e); } break; default: break; }
} return CallNextHookEx(_handleToHook, nCode, wParam, lParam); } private MouseButtons GetButton(Int32 wParam) { switch (wParam) { case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: return MouseButtons.Left; case WM_RBUTTONDOWN: case WM_RBUTTONUP: case WM_RBUTTONDBLCLK: return MouseButtons.Right; case WM_MBUTTONDOWN: case WM_MBUTTONUP: case WM_MBUTTONDBLCLK: return MouseButtons.Middle; default: return MouseButtons.None; } } private MouseEventType GetEventType(Int32 wParam) { switch (wParam) { case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MBUTTONDOWN: return MouseEventType.MouseDown; case WM_LBUTTONUP: case WM_RBUTTONUP: case WM_MBUTTONUP: return MouseEventType.MouseUp; case WM_LBUTTONDBLCLK: case WM_RBUTTONDBLCLK: case WM_MBUTTONDBLCLK: return MouseEventType.DoubleClick; case WM_MOUSEWHEEL: return MouseEventType.MouseWheel; case WM_MOUSEMOVE: return MouseEventType.MouseMove; default: return MouseEventType.None; } } #endregion
}}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;using MouseKeyboardLibrary;namespace SampleApplication { public partial class HookTestForm : Form { MouseHook mouseHook = new MouseHook(); KeyboardHook keyboardHook = new KeyboardHook(); public HookTestForm() { InitializeComponent(); } private void TestForm_Load(object sender, EventArgs e) { mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove); mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown); mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp); mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel); keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown); keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp); keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress); mouseHook.Start(); keyboardHook.Start(); SetXYLabel(MouseSimulator.X, MouseSimulator.Y); } void keyboardHook_KeyPress(object sender, KeyPressEventArgs e) { AddKeyboardEvent( "KeyPress", "", e.KeyChar.ToString(), "", "", "" ); } void keyboardHook_KeyUp(object sender, KeyEventArgs e) { AddKeyboardEvent( "KeyUp", e.KeyCode.ToString(), "", e.Shift.ToString(), e.Alt.ToString(), e.Control.ToString() ); } void keyboardHook_KeyDown(object sender, KeyEventArgs e) { AddKeyboardEvent( "KeyDown", e.KeyCode.ToString(), "", e.Shift.ToString(), e.Alt.ToString(), e.Control.ToString() ); } void mouseHook_MouseWheel(object sender, MouseEventArgs e) { AddMouseEvent( "MouseWheel", "", "", "", e.Delta.ToString() ); } void mouseHook_MouseUp(object sender, MouseEventArgs e) { AddMouseEvent( "MouseUp", e.Button.ToString(), e.X.ToString(), e.Y.ToString(), "" ); } void mouseHook_MouseDown(object sender, MouseEventArgs e) { AddMouseEvent( "MouseDown", e.Button.ToString(), e.X.ToString(), e.Y.ToString(), "" ); } void mouseHook_MouseMove(object sender, MouseEventArgs e) { SetXYLabel(e.X, e.Y); } void SetXYLabel(int x, int y) { curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y); } void AddMouseEvent(string eventType, string button, string x, string y, string delta) { listView1.Items.Insert(0, new ListViewItem( new string[]{ eventType, button, x, y, delta })); } void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control) { listView2.Items.Insert(0, new ListViewItem( new string[]{ eventType, keyCode, keyChar, shift, alt, control })); } private void TestForm_FormClosed(object sender, FormClosedEventArgs e) { // Not necessary anymore, will stop when application exits //mouseHook.Stop(); //keyboardHook.Stop(); } } }上面那两个可以参考下,这一个是正确的;
需要使用的知识:
Win32 Hook;
Windows Service programming;
Win32 api(about mouse operations)
{
document.getelementByid(this).click();}
onmouseover=click()
如下:using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;namespace MouseKeyboardLibrary
{ /// <summary>
/// Abstract base class for Mouse and Keyboard hooks
/// </summary>
public abstract class GlobalHook
{ #region Windows API Code [StructLayout(LayoutKind.Sequential)]
protected class POINT
{
public int x;
public int y;
} [StructLayout(LayoutKind.Sequential)]
protected class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
} [StructLayout(LayoutKind.Sequential)]
protected class MouseLLHookStruct
{
public POINT pt;
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
} [StructLayout(LayoutKind.Sequential)]
protected class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
} [DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
protected static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
int dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
protected static extern int UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
protected static extern int CallNextHookEx(
int idHook,
int nCode,
int wParam,
IntPtr lParam); [DllImport("user32")]
protected static extern int ToAscii(
int uVirtKey,
int uScanCode,
byte[] lpbKeyState,
byte[] lpwTransKey,
int fuState); [DllImport("user32")]
protected static extern int GetKeyboardState(byte[] pbKeyState); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
protected static extern short GetKeyState(int vKey); protected delegate int HookProc(int nCode, int wParam, IntPtr lParam); protected const int WH_MOUSE_LL = 14;
protected const int WH_KEYBOARD_LL = 13; protected const int WH_MOUSE = 7;
protected const int WH_KEYBOARD = 2;
protected const int WM_MOUSEMOVE = 0x200;
protected const int WM_LBUTTONDOWN = 0x201;
protected const int WM_RBUTTONDOWN = 0x204;
protected const int WM_MBUTTONDOWN = 0x207;
protected const int WM_LBUTTONUP = 0x202;
protected const int WM_RBUTTONUP = 0x205;
protected const int WM_MBUTTONUP = 0x208;
protected const int WM_LBUTTONDBLCLK = 0x203;
protected const int WM_RBUTTONDBLCLK = 0x206;
protected const int WM_MBUTTONDBLCLK = 0x209;
protected const int WM_MOUSEWHEEL = 0x020A;
protected const int WM_KEYDOWN = 0x100;
protected const int WM_KEYUP = 0x101;
protected const int WM_SYSKEYDOWN = 0x104;
protected const int WM_SYSKEYUP = 0x105; protected const byte VK_SHIFT = 0x10;
protected const byte VK_CAPITAL = 0x14;
protected const byte VK_NUMLOCK = 0x90; protected const byte VK_LSHIFT = 0xA0;
protected const byte VK_RSHIFT = 0xA1;
protected const byte VK_LCONTROL = 0xA2;
protected const byte VK_RCONTROL = 0x3;
protected const byte VK_LALT = 0xA4;
protected const byte VK_RALT = 0xA5; protected const byte LLKHF_ALTDOWN = 0x20; #endregion #region Private Variables protected int _hookType;
protected int _handleToHook;
protected bool _isStarted;
protected HookProc _hookCallback; #endregion #region Properties public bool IsStarted
{
get
{
return _isStarted;
}
} #endregion #region Constructor public GlobalHook()
{ Application.ApplicationExit += new EventHandler(Application_ApplicationExit); } #endregion #region Methods public void Start()
{ if (!_isStarted &&
_hookType != 0)
{ // Make sure we keep a reference to this delegate!
// If not, GC randomly collects it, and a NullReference exception is thrown
_hookCallback = new HookProc(HookCallbackProcedure); _handleToHook = SetWindowsHookEx(
_hookType,
_hookCallback,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
0); // Were we able to sucessfully start hook?
if (_handleToHook != 0)
{
_isStarted = true;
} } } public void Stop()
{ if (_isStarted)
{ UnhookWindowsHookEx(_handleToHook); _isStarted = false; } } protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)
{
// This method must be overriden by each extending hook
return 0; } protected void Application_ApplicationExit(object sender, EventArgs e)
{ if (_isStarted)
{
Stop();
} } #endregion }}
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace MouseKeyboardLibrary
{ /// <summary>
/// Captures global mouse events
/// </summary>
public class MouseHook : GlobalHook
{ #region MouseEventType Enum private enum MouseEventType
{
None,
MouseDown,
MouseUp,
DoubleClick,
MouseWheel,
MouseMove
} #endregion #region Events public event MouseEventHandler MouseDown;
public event MouseEventHandler MouseUp;
public event MouseEventHandler MouseMove;
public event MouseEventHandler MouseWheel; public event EventHandler Click;
public event EventHandler DoubleClick; #endregion #region Constructor public MouseHook()
{ _hookType = WH_MOUSE_LL; } #endregion #region Methods protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
{
if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
{ MouseLLHookStruct mouseHookStruct =
(MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct)); MouseButtons button = GetButton(wParam);
MouseEventType eventType = GetEventType(wParam); MouseEventArgs e = new MouseEventArgs(
button,
(eventType == MouseEventType.DoubleClick ? 2 : 1),
mouseHookStruct.pt.x,
mouseHookStruct.pt.y,
(eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0)); // Prevent multiple Right Click events (this probably happens for popup menus)
if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
{
eventType = MouseEventType.None;
} switch (eventType)
{
case MouseEventType.MouseDown:
if (MouseDown != null)
{
MouseDown(this, e);
}
break;
case MouseEventType.MouseUp:
if (Click != null)
{
Click(this, new EventArgs());
}
if (MouseUp != null)
{
MouseUp(this, e);
}
break;
case MouseEventType.DoubleClick:
if (DoubleClick != null)
{
DoubleClick(this, new EventArgs());
}
break;
case MouseEventType.MouseWheel:
if (MouseWheel != null)
{
MouseWheel(this, e);
}
break;
case MouseEventType.MouseMove:
if (MouseMove != null)
{
MouseMove(this, e);
}
break;
default:
break;
}
} return CallNextHookEx(_handleToHook, nCode, wParam, lParam); } private MouseButtons GetButton(Int32 wParam)
{ switch (wParam)
{ case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
return MouseButtons.Left;
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
return MouseButtons.Right;
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
return MouseButtons.Middle;
default:
return MouseButtons.None; } } private MouseEventType GetEventType(Int32 wParam)
{ switch (wParam)
{ case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
return MouseEventType.MouseDown;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
return MouseEventType.MouseUp;
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
return MouseEventType.DoubleClick;
case WM_MOUSEWHEEL:
return MouseEventType.MouseWheel;
case WM_MOUSEMOVE:
return MouseEventType.MouseMove;
default:
return MouseEventType.None; }
} #endregion
}}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;using MouseKeyboardLibrary;namespace SampleApplication
{
public partial class HookTestForm : Form
{ MouseHook mouseHook = new MouseHook();
KeyboardHook keyboardHook = new KeyboardHook(); public HookTestForm()
{
InitializeComponent();
} private void TestForm_Load(object sender, EventArgs e)
{ mouseHook.MouseMove += new MouseEventHandler(mouseHook_MouseMove);
mouseHook.MouseDown += new MouseEventHandler(mouseHook_MouseDown);
mouseHook.MouseUp += new MouseEventHandler(mouseHook_MouseUp);
mouseHook.MouseWheel += new MouseEventHandler(mouseHook_MouseWheel); keyboardHook.KeyDown += new KeyEventHandler(keyboardHook_KeyDown);
keyboardHook.KeyUp += new KeyEventHandler(keyboardHook_KeyUp);
keyboardHook.KeyPress += new KeyPressEventHandler(keyboardHook_KeyPress); mouseHook.Start();
keyboardHook.Start(); SetXYLabel(MouseSimulator.X, MouseSimulator.Y); } void keyboardHook_KeyPress(object sender, KeyPressEventArgs e)
{ AddKeyboardEvent(
"KeyPress",
"",
e.KeyChar.ToString(),
"",
"",
""
); } void keyboardHook_KeyUp(object sender, KeyEventArgs e)
{ AddKeyboardEvent(
"KeyUp",
e.KeyCode.ToString(),
"",
e.Shift.ToString(),
e.Alt.ToString(),
e.Control.ToString()
); } void keyboardHook_KeyDown(object sender, KeyEventArgs e)
{
AddKeyboardEvent(
"KeyDown",
e.KeyCode.ToString(),
"",
e.Shift.ToString(),
e.Alt.ToString(),
e.Control.ToString()
); } void mouseHook_MouseWheel(object sender, MouseEventArgs e)
{ AddMouseEvent(
"MouseWheel",
"",
"",
"",
e.Delta.ToString()
); } void mouseHook_MouseUp(object sender, MouseEventArgs e)
{
AddMouseEvent(
"MouseUp",
e.Button.ToString(),
e.X.ToString(),
e.Y.ToString(),
""
); } void mouseHook_MouseDown(object sender, MouseEventArgs e)
{
AddMouseEvent(
"MouseDown",
e.Button.ToString(),
e.X.ToString(),
e.Y.ToString(),
""
);
} void mouseHook_MouseMove(object sender, MouseEventArgs e)
{ SetXYLabel(e.X, e.Y); } void SetXYLabel(int x, int y)
{ curXYLabel.Text = String.Format("Current Mouse Point: X={0}, y={1}", x, y); } void AddMouseEvent(string eventType, string button, string x, string y, string delta)
{ listView1.Items.Insert(0,
new ListViewItem(
new string[]{
eventType,
button,
x,
y,
delta
})); } void AddKeyboardEvent(string eventType, string keyCode, string keyChar, string shift, string alt, string control)
{ listView2.Items.Insert(0,
new ListViewItem(
new string[]{
eventType,
keyCode,
keyChar,
shift,
alt,
control
})); } private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
{ // Not necessary anymore, will stop when application exits //mouseHook.Stop();
//keyboardHook.Stop(); }
}
}上面那两个可以参考下,这一个是正确的;
2。至于模仿鼠标点击,在钩子程序里给捕捉到的窗口发送鼠标消息就可以了钩子程序可以参考(这是个局部钩子程序)
http://download.csdn.net/source/1117522
是在VC下实现的