等待下班放假之hook学习

三个星期了,真TMD的快。
昨天李鹏让我做个类似外挂的按键工具,查下资料,很难看懂的饿。
以为调用API函数就可以解决问题,貌似不可以诶。网络上查了点资料,很多饿,貌似又和钩子(hook)有关系了。
又是一个心技术,得学啊。看了半天找了点资料看着,貌似不怎么懂的饿。记得上个星期自己疯狂的复习以前基础的内容,才算了解了事件的处理机制。看来这个星期日有得忙了。
慢慢看吧虽然上面写的是hook学习,但是貌似自己还不怎么懂,基本就没有怎么学的饿。上网查了下看到这个代码,貌似和自己要的有点关系。
[DllImport("user32.dll")]
        
static extern bool SetCursorPos(int X, int Y);

        [DllImport(
"user32.dll")]
        
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        [DllImport(
"user32.dll")]
        
static extern IntPtr FindWindow(string strClass, string strWindow);

        [DllImport(
"user32.dll")]
        
static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);

        [DllImport(
"user32.dll")]
        
static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);

        [DllImport(
"user32.dll", EntryPoint = "GetCursorPos")]
        
static extern int GetCursorPos();


基本当没看到。搞了半天还是不知道那个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;

namespace SimulateMouse
{
    
public partial class DemoForm : Form
    
{
        [StructLayout(LayoutKind.Sequential)]
        
struct NativeRECT
        
{
            
public int left;
            
public int top;
            
public int right;
            
public int bottom;
        }


        [Flags]
        
enum MouseEventFlag : uint
        
{
            Move        
= 0x0001,
            LeftDown    
= 0x0002,
            LeftUp      
= 0x0004,
            RightDown   
= 0x0008,
            RightUp     
= 0x0010,
            MiddleDown  
= 0x0020,
            MiddleUp    
= 0x0040,
            XDown       
= 0x0080,
            XUp         
= 0x0100,
            Wheel       
= 0x0800,
            VirtualDesk 
= 0x4000,
            Absolute    
= 0x8000
        }


        [DllImport(
"user32.dll")]
        
static extern bool SetCursorPos(int X, int Y);  

        [DllImport(
"user32.dll")]
        
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);

        [DllImport(
"user32.dll")]
        
static extern IntPtr FindWindow(string strClass, string strWindow);

        [DllImport(
"user32.dll")]
        
static extern IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter, string strClass, string strWindow);

        [DllImport(
"user32.dll")]
        
static extern bool GetWindowRect(HandleRef hwnd, out NativeRECT rect);


        
const int AnimationCount = 80;
        
        
private Point endPosition;

        
private int count;

        
public DemoForm()
        
{
            InitializeComponent();
        }


        
private void btnStart_Click(object sender, EventArgs e)
        
{
            NativeRECT rect;

            IntPtr ptrTaskbar 
= FindWindow("Shell_TrayWnd"null);
            
if (ptrTaskbar == IntPtr.Zero)
            
{
                MessageBox.Show(
"No taskbar found.");
                
return;
            }


            IntPtr ptrStartBtn 
= FindWindowEx(new HandleRef(this, ptrTaskbar), new HandleRef(this, IntPtr.Zero), "Button"null);
            
if (ptrStartBtn == IntPtr.Zero)
            
{
                MessageBox.Show(
"No start button found.");
                
return;
            }


            GetWindowRect(
new HandleRef(this, ptrStartBtn), out rect);

            endPosition.X 
= (rect.left + rect.right) / 2;
            endPosition.Y 
= (rect.top + rect.bottom) / 2;

            
if (chkAnimation.Checked)
            
{
                
this.count = AnimationCount;
                movementTimer.Start();
            }

            
else
            
{
                SetCursorPos(endPosition.X, endPosition.Y);
                mouse_event(MouseEventFlag.LeftDown, 
000, UIntPtr.Zero);
                mouse_event(MouseEventFlag.LeftUp, 
000, UIntPtr.Zero);
            }

        }


        
private void movementTimer_Tick(object sender, EventArgs e)
        
{
            
int stepx = (endPosition.X - MousePosition.X) / count;
            
int stepy = (endPosition.Y - MousePosition.Y) / count;

            count
--;
            
if (count == 0)
            
{
                movementTimer.Stop();
                mouse_event(MouseEventFlag.LeftDown, 
000, UIntPtr.Zero);
                mouse_event(MouseEventFlag.LeftUp, 
000, UIntPtr.Zero);
            }


            tbCursor.Text 
= String.Format("({0}, {1})", MousePosition.X, MousePosition.Y);

            mouse_event(MouseEventFlag.Move, stepx, stepy, 
0, UIntPtr.Zero);
        }

    }

}

倒闭光有这个还是不可以的饿。还有呢就是界面的设计
DemoForm.Designer.cs
namespace SimulateMouse
{
    
partial class DemoForm
    
{
        
private System.ComponentModel.IContainer components = null;

        
protected override void Dispose(bool disposing)
        
{
            
if (disposing && (components != null))
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


        
Windows Form Designer Generated Code

        
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;
    }

}



娟儿说她要来呢我都吧知道自己是怎么想的,貌似还在爱着呢。也许可能是真的还爱着呢吧
posted @ 2008-03-07 17:06  系咪噶  阅读(263)  评论(0编辑  收藏  举报