WinAPI 鼠标事件监听-C# WinForm应用

在软件开发中往往需要监听鼠标或者键盘功能(全局/局部监听),.Net本身可能无法完成使命,还好C#可以直接调用WinAPI相关的Function,实现监听功能.

以下做了个Demo,通过鼠标点(单击/右键)击窗体,钩子就会捕捉鼠标事件,Code如下

HOOK.cs  文件下载:WindowsFormsApplication1 

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication1
{
internal class HOOK
{
private static int hHook = 0 ;
private static Form HookControlForm;
private static HookProc MouseHookProcedure;

[DllImport(
" user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int CallNextHookEx( int idHook, int nCode, IntPtr wParam, IntPtr lParam);

/// <summary>
/// GetForegroundWindow
///  函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
///  函数原型:HWND GetForegroundWindow(VOID)
///  参数:无。
///  返回值:函数返回前台窗回的句柄。
///  速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
///  摘要:
///  函数功能:该函数返回前台窗口(用户当前工作的窗口)。系统分配给产生前台窗口的线程一个稍高一点的优先级。
///  函数原型:HWND GetForegroundWindow(VOID)
///  参数:无。
///  返回值:函数返回前台窗回的句柄。
///  速查:Windows NT:3.1以上版本;Windows:95以上版本:Windows CE:1.0以上版本:头文件:Winuser.h;库文件:user32.lib。
///
/// </summary>
/// <returns></returns>
[DllImport( " user32.dll " )]
private static extern IntPtr GetForegroundWindow();
/// <summary>
/// GetWindowThreadProcessId这个函数来获得窗口所属进程ID和线程ID
/// </summary>
/// <param name="hwnd"></param>
/// <param name="ID"></param>
/// <returns></returns>
[DllImport( " User32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, int ID);


/// <summary>
/// 消息处理中心
/// </summary>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
private static int HOOKProcReturn( int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 )
{
// 鼠标右键
if (wParam.ToInt32() == 0x205 )
{
IntPtr intpr
= GetForegroundWindow();
if ((intpr == HookControlForm.Handle) )
{
MessageBox.Show(
" ok- 0x205 " );

}
}
else if (wParam.ToInt32() == 0x203 )
{
if (GetForegroundWindow() == HookControlForm.Handle)
{
MessageBox.Show(
" ok- 0x203 " );
}
}
// 鼠标左键
else if (wParam.ToInt32() == 0x201 )
{
if (GetForegroundWindow() == HookControlForm.Handle)
MessageBox.Show(
" ok- 0x201 " );


}
else if (wParam.ToInt32() == 0xa1 )
{
if ((GetForegroundWindow() == HookControlForm.Handle) )
{
MessageBox.Show(
" ok- 0xa1 " );
}
}
else if (wParam.ToInt32() == 0x202 )
{
// MessageBox.Show("ok- 0x202");
}
else if (wParam.ToInt32() == 0x200 )
{
// MessageBox.Show("ok-0x200");
}
}
// 监听下一次事件
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

[DllImport(
" user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
public static bool StartHook(Form HookControl)
{
HookControlForm
= HookControl;
MouseHookProcedure
= new HookProc(HOOK.HOOKProcReturn);
// 监听 事件 发送消息
hHook = SetWindowsHookEx(( int )HookType.WH_MOUSE , MouseHookProcedure, IntPtr.Zero, GetWindowThreadProcessId(HookControlForm.Handle, 0 ));
if (hHook == 0 )
{
return false ;
}
return true ;
}

public static bool StopHook()
{
return UnhookWindowsHookEx(hHook);
}

[DllImport(
" user32.dll " , CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx( int idHook);

public delegate int HookProc( int nCode, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public HOOK.POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}

/// <summary>
/// 钩子类型舰艇鼠标键盘等事件
/// </summary>
private enum HookType : int
{
WH_JOURNALRECORD
= 0 ,
WH_JOURNALPLAYBACK
= 1 ,
WH_KEYBOARD
= 2 ,
WH_GETMESSAGE
= 3 ,
WH_CALLWNDPROC
= 4 ,
WH_CBT
= 5 ,
WH_SYSMSGFILTER
= 6 ,
WH_MOUSE
= 7 , // 局部 线程级别
WH_HARDWARE = 8 ,
WH_DEBUG
= 9 ,
WH_SHELL
= 10 ,
WH_FOREGROUNDIDLE
= 11 ,
WH_CALLWNDPROCRET
= 12 ,
WH_KEYBOARD_LL
= 13 ,
WH_MOUSE_LL
= 14 // 全局
}

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// 开始监听
HOOK.StartHook( this );
}

private void Form1_FormClosing( object sender, FormClosingEventArgs e)
{
// 结束监听
HOOK.StopHook();
}
}
}



转载于:https://www.cnblogs.com/Eric_/archive/2011/02/07/1949592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用`System.Diagnostics.Process`类来启动控制台并将其嵌入到WinForm窗口中。 首先,您需要在WinForm中添加一个`Panel`控件,然后将其命名为`consolePanel`。然后使用以下代码启动控制台并将其嵌入到该面板中: ```csharp // 启动控制台进程 Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.Start(); // 将控制台窗口嵌入到WinForm中 IntPtr handle = process.MainWindowHandle; SetParent(handle, consolePanel.Handle); // 设置控制台窗口位置和大小 SetWindowLong(handle, GWL_STYLE, WS_VISIBLE | WS_CHILD); MoveWindow(handle, 0, 0, consolePanel.Width, consolePanel.Height, true); ``` 请注意,您需要导入以下WinAPI函数: ```csharp [DllImport("user32.dll")] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); [DllImport("user32.dll", SetLastError = true)] static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); [DllImport("user32.dll", SetLastError = true)] static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); const int GWL_STYLE = -16; const uint WS_VISIBLE = 0x10000000; const uint WS_CHILD = 0x40000000; ``` 当您运行应用程序时,将会在WinForm窗口中嵌入一个控制台。您可以使用`process.StandardInput`属性向控制台发送命令,并使用`process.StandardOutput`属性获取控制台输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值