473,513 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2477

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3341
by: ayiiq180 | last post by:
my hook already in a dll and the handle is shared,but the hook cant work well,when i run the application,My mouse click the application's view,the hook work well,but when i click the other...
5
7069
by: Logan Mckinley | last post by:
I need to know where the cursor is (x,y) and when it moves even when it is not over my form. I know i can get the current location with: System.Windows.Forms.Cursor.Position.X...
1
1855
by: | last post by:
Give an example:When user pressed "B" or "b",HOOK get that message and let TextBox1.text display "a". The executing founction is as following: **************************************** IntPtr...
3
8398
by: june | last post by:
Hi, I am coding for an application with dialog window. I need intercept mouse input. I need catch raw input, pretty much everything for WM_INPUT: such as Left/Middle/Right button down/up,...
1
7658
by: blueturtle | last post by:
Hi, I've implemented a hook (WH_CALLWNDPRO), using the sample posted at MSDN magazine. ( see url: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/ ) I succeed in intercepting the...
7
26516
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
6
2512
by: ewolfman | last post by:
Hi, Is there any way in which I can monitor / hook the Registry, and upon a call to a specific key from a specific application - swap the returned value? I was thinking of using this method...
22
6387
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.