黑马程序员技术交流社区

标题: C#中问题 [打印本页]

作者: 心ゝ疼ㄚòひ    时间: 2012-4-16 23:00
标题: C#中问题
求高手解救  C#中怎么调用HookProc ??
作者: 林其响    时间: 2012-4-17 08:30
CommonDialog.HookProc 方法
命名空间:System.Windows.Forms
C#
protected virtual IntPtr HookProc (
    IntPtr hWnd,
    int msg,
    IntPtr wparam,
    IntPtr lparam
)

参数
hWnd
对话框窗口的句柄。

msg
正在接收的消息。

wparam
关于消息的附加信息。

lparam
关于消息的附加信息。

下面的代码示例演示如何重写 HookProc 方法。此示例由继承 CommonDialog 类的类组成。在该类的 HookProc 重写中,示例根据特定的 Windows 消息的常数值来计算方法的 msg 参数。如果 msg 参数等于指定的常数,则示例写入跟踪输出(它标识被传递到 HookProc 方法的 Windows 消息)。此示例假定在其中声明 HookProc 方法的类继承 CommonDialog 类。

// Defines the constants for Windows messages.
const int WM_SETFOCUS = 0x0007;
const int WM_INITDIALOG = 0x0110;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_RBUTTONDOWN = 0x0204;
const int WM_MOVE = 0x0003;
// Overrides the base class hook procedure...
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
{
    // Evaluates the message parameter to determine the user action.
    switch(msg)
    {
        case WM_INITDIALOG:
            System.Diagnostics.Trace.Write("The WM_INITDIALOG message was received.");
            break;
        case WM_SETFOCUS:
            System.Diagnostics.Trace.Write("The WM_SETFOCUS message was received.");
            break;
        case WM_LBUTTONDOWN:
            System.Diagnostics.Trace.Write("The WM_LBUTTONDOWN message was received.");
            break;
        case WM_RBUTTONDOWN:
            System.Diagnostics.Trace.Write("The WM_RBUTTONDOWN message was received.");
            break;
        case WM_MOVE:
            System.Diagnostics.Trace.Write("The WM_MOVE message was received.");
            break;
    }
    // Always call the base class hook procedure.
    return base.HookProc(hWnd, msg, wParam, lParam);
}

作者: 马跃    时间: 2012-4-17 16:43
你需要自己定义一个hook类,然后添加一个代理。
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2