472,099 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,099 software developers and data experts.

eventcode all 0, Hook Edit change

I download the demo
http://msdn.microsoft.com/msdnmag/is...0/cuttingedge/. I
inherite the demo, and write my code.
I want to use Hook to monitor C++ Edit change. I use a C# form
containing a Edit(textbox) and button to test at first. When I change
text in Edit or press button to setText value, I want to monitor the
change event.

But the event code always return 0, why?
#######################################WindowsHook .cs:

using System;
using System.Runtime.InteropServices;

namespace MsdnMag
{
#region Class HookEventArgs
public class HookEventArgs : EventArgs
{
public int HookCode; // Hook code
public IntPtr wParam; // WPARAM argument
public IntPtr lParam; // LPARAM argument
}
#endregion

#region Enum HookType
// Hook Types
public 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
}
#endregion

#region Class LocalWindowsHook
public class LocalWindowsHook
{
//
************************************************** **********************
// Filter function delegate
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
//
************************************************** **********************

//
************************************************** **********************
// Internal properties
protected IntPtr m_hhook = IntPtr.Zero;
protected HookProc m_filterFunc = null;
protected HookType m_hookType;
//
************************************************** **********************

//
************************************************** **********************
// Event delegate
public delegate void HookEventHandler(object sender, HookEventArgs
e);
//
************************************************** **********************

//
************************************************** **********************
// Event: HookInvoked
public event HookEventHandler HookInvoked;
protected void OnHookInvoked(HookEventArgs e)
{
if (HookInvoked != null)
HookInvoked(this, e);
}
//
************************************************** **********************

//
************************************************** **********************
// Class constructor(s)
public LocalWindowsHook(HookType hook)
{
m_hookType = hook;
m_filterFunc = new HookProc(this.CoreHookProc);
}
public LocalWindowsHook(HookType hook, HookProc func)
{
m_hookType = hook;
m_filterFunc = func;
}
//
************************************************** **********************

//
************************************************** **********************
// Default filter function
protected int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
{
Console.Write("coreHookPro=");
Console.WriteLine(code);

if (code < 0)
return CallNextHookEx(m_hhook, code, wParam, lParam);

// Let clients determine what to do
HookEventArgs e = new HookEventArgs();
e.HookCode = code;
e.wParam = wParam;
e.lParam = lParam;
OnHookInvoked(e);

// Yield to the next hook in the chain
return CallNextHookEx(m_hhook, code, wParam, lParam);
}
//
************************************************** **********************

//
************************************************** **********************
// Install the hook
public void Install()
{
m_hhook = SetWindowsHookEx(
m_hookType,
m_filterFunc,
IntPtr.Zero,
(int) AppDomain.GetCurrentThreadId());
}
//
************************************************** **********************

//
************************************************** **********************
// Uninstall the hook
public void Uninstall()
{
UnhookWindowsHookEx(m_hhook);
}
//
************************************************** **********************
#region Win32 Imports
//
************************************************** **********************
// Win32: SetWindowsHookEx()
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code,
HookProc func,
IntPtr hInstance,
int threadID);
//
************************************************** **********************

//
************************************************** **********************
// Win32: UnhookWindowsHookEx()
[DllImport("user32.dll")]
protected static extern int UnhookWindowsHookEx(IntPtr hhook);
//
************************************************** **********************

//
************************************************** **********************
// Win32: CallNextHookEx()
[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook,
int code, IntPtr wParam, IntPtr lParam);
//
************************************************** **********************
#endregion
}
#endregion
}

############################################# EditChange.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MsdnMag
{
#region Enum EditHookAction
// CBT hook actions
public enum EditHookAction : int
{
EN_CHANGE = 0x300, //编辑框*的文本己更新
EN_ERRSPACE=0x0500, //编辑框内*不足
EN_HSCROLL=0x0601, //用户点击了水平滚动条
EN_KILLFOCUS =0x0200,//编辑框*在失去输入焦点
EN_MAXTEXT=0x0501, //插入的内容被截*
EN_SETFOCUS=0x0100, //编辑框获得输入焦点
EN_UPDATE=0x0400, //编辑框*的文本将要更新
EN_VSCROLL=0x0602 //用户点击了垂直滚动条消息含义

//EMN_CHANGE=0x00000001

}
#endregion

public class EditEventArgs : EventArgs
{
public IntPtr Handle; // Win32 handle of the window
public string Title; // caption of the window
public string ClassName; // class of the window
public string textValue;

}

#region Class EditChange
public class EditChange : LocalWindowsHook
{
//
************************************************** **********************
// Event delegate
public delegate void EditEventHandler(object sender,
EditEventArgs e);
//
************************************************** **********************
const int WM_GETTEXT = 0x000D;
StringBuilder Buff = new StringBuilder(90240);

//
************************************************** **********************
// Events
public event EditEventHandler TextEditChange;
//public event EditEventHandler EditSetFocus;

//
************************************************** **********************
//
************************************************** **********************
// Internal properties
protected IntPtr m_hwnd = IntPtr.Zero;
protected string m_title = "";
protected string m_class = "";
protected string textValue = "";

//
************************************************** **********************
//
************************************************** **********************
// Class constructor(s)
public EditChange() : base(HookType.WH_CALLWNDPROC)
{
this.HookInvoked += new HookEventHandler(EditHookInvoked);
}
public EditChange(HookProc func)
: base(HookType.WH_CALLWNDPROC, func)
{
this.HookInvoked += new HookEventHandler(EditHookInvoked);
}
//
************************************************** **********************
//
************************************************** **********************
// Handles the hook event
private void EditHookInvoked(object sender, HookEventArgs e)
{
EditHookAction code = (EditHookAction)e.HookCode;
Console.WriteLine(sender);
Console.Write("Event hook code=");
Console.WriteLine(code);
Console.WriteLine(e.ToString());
Console.Write("wparam=");
Console.WriteLine(e.wParam);
Console.Write("lparam=");
Console.WriteLine(e.lParam);
IntPtr wParam = e.wParam;
IntPtr lParam = e.lParam;

// Handle hook events (only a few of available actions)
switch (code)
{
case EditHookAction.EN_CHANGE:
Console.Write("EN_CHANGE");
HandleEditChangeEvent(wParam, lParam);
break;
}

return;
}
//
************************************************** **********************
//
************************************************** **********************
// Handle the CREATEWND hook event
private void HandleEditChangeEvent(IntPtr wParam, IntPtr
lParam)
{
// Cache some information
UpdateWindowData(wParam,lParam);

// raise event
OnTextEditChange();
}
//
************************************************** **********************

//
************************************************** **********************
// Read and store some information about the window
private void UpdateWindowData(IntPtr wParam, IntPtr lParam)
{
Console.Write("WPARAM=");
Console.WriteLine(wParam);
// Cache the window handle
m_hwnd = lParam;

// Cache the window's class name
StringBuilder sb1 = new StringBuilder();
sb1.Capacity = 40;
GetClassName(m_hwnd, sb1, 40);
m_class = sb1.ToString();

// Cache the window's title bar
StringBuilder sb2 = new StringBuilder();
sb2.Capacity = 256;
GetWindowText(m_hwnd, sb2, 256);
m_title = sb2.ToString();

// get Text value
SendMessage(m_hwnd, WM_GETTEXT, Buff.Capacity, Buff);
textValue = Buff.ToString();
Console.Write(" Text=");
Console.Write(Buff.ToString());
}
//
************************************************** **********************
//
************************************************** **********************
// Helper functions that fire events by executing user code
protected virtual void OnTextEditChange()
{
if (TextEditChange != null)
{
EditEventArgs e = new EditEventArgs();
PrepareEventData(e);
TextEditChange(this, e);
}
}
//
************************************************** **********************
//
************************************************** **********************
// Prepare the event data structure
private void PrepareEventData(EditEventArgs e)
{
e.Handle = m_hwnd;
e.Title = m_title;
e.ClassName = m_class;
e.textValue = textValue;
}
//
************************************************** **********************

#region Win32 Imports
//
************************************************** **********************
// Win32: GetClassName
[DllImport("user32.dll")]
protected static extern int GetClassName(IntPtr hwnd,
StringBuilder lpClassName, int nMaxCount);
//
************************************************** **********************

//
************************************************** **********************
// Win32: GetWindowText
[DllImport("user32.dll")]
protected static extern int GetWindowText(IntPtr hwnd,
StringBuilder lpString, int nMaxCount);
//
************************************************** **********************
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int Msg, int
wParam, StringBuilder lParam);
#endregion
}
#endregion
}
#############################################HookF orm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MsdnMag;

namespace TestCbtHook_CS
{
public partial class HookForm : Form
{
private EditChange cbt;
public HookForm()
{
InitializeComponent();
cbt = new EditChange();
cbt.TextEditChange += new
EditChange.EditEventHandler(EditTextChange);
cbt.Install();
//MessageBox.Show("Hello, world!", "Courtesy of Cutting
Edge");
}
//
************************************************** ****************************
// Prepare the hook object to work
//
************************************************** ****************************
private void button1_Click(object sender, EventArgs e)
{

textBox1.Text = "new text";
}
override
protected void OnFormClosing(FormClosingEventArgs e)
{
cbt.Uninstall();
base.OnFormClosing(e);
Console.WriteLine("onFormClosing unstall");
}
public void EditTextChange(object sender, EditEventArgs e)
{
Console.WriteLine("HookForm EditeTextChange");
Console.WriteLine(sender.ToString());
Console.WriteLine(e.ClassName);
}

[STAThread]
static void Main()
{
Application.Run(new HookForm());
}
}
}

using System;
namespace TestCbtHook_CS
{
partial class HookForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
Console.WriteLine("Disponse");

if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(98, 130);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(98, 76);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 25);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "1";
//
// HookForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F,
15F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 258);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "HookForm";
this.Text = "HookForm";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
}
}

Jun 4 '06 #1
0 2331

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by ayiiq180 | last post: by
5 posts views Thread by Logan Mckinley | last post: by
3 posts views Thread by june | last post: by
1 post views Thread by blueturtle | last post: by
7 posts views Thread by jpierson | last post: by
22 posts views Thread by schneider | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.