using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace RemotePrint
{
/// <summary>
/// SaveActiveX 的摘要说明。
/// </summary>
public class SaveActiveX : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox txtsave;
/// <summary> 
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; internal delegate IntPtr HookProc(int code, IntPtr wparam, IntPtr lparam); 
public SaveActiveX()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// <summary> 
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} internal enum HookType //枚举,钩子的类型 
{  Keyboard = 2,  };   IntPtr _nextHookPtr; //记录Hook编号  [DllImport("kernel32.dll")] 
static extern int GetCurrentThreadId(); //取得当前线程编号的API  [DllImport("User32.dll")]  internal extern static void UnhookWindowsHookEx(IntPtr handle); //取消Hook的API   [DllImport("User32.dll")]  internal extern static IntPtr SetWindowsHookEx(int idHook, [MarshalAs(UnmanagedType.FunctionPtr)] HookProc lpfn, IntPtr hinstance, int threadID); //设置Hook的API   [DllImport("User32.dll")]  internal extern static IntPtr CallNextHookEx(IntPtr handle, int code, IntPtr wparam, IntPtr lparam); //取得下一个Hook的API  IntPtr MyHookProc(int code, IntPtr wparam, IntPtr lparam) 
{
try
{
if( code < 0 ) 
{
return CallNextHookEx(_nextHookPtr,code, wparam, lparam); //返回,让后面的程序处理该消息 
}
else if( wparam.ToInt32() == 98 || wparam.ToInt32() == 66 ) //如果用户输入的是 b 
{  a += 1;
this.txtsave.Text = a.ToString();
// this.txtsave.Text = "w";
return CallNextHookEx(_nextHookPtr,code, wparam, lparam); //直接返回了,该消息就处理结束了  } 
else 
{
return IntPtr.Zero; //返回,让后面的程序处理该消息 
}
}
catch( Exception exp )
{
throw( new Exception( "错误!!" + exp.Message ));
}
}  public void SetHook() 

if( _nextHookPtr == IntPtr.Zero ) //是否勾过
{
HookProc myhookProc = new HookProc(MyHookProc); //声明一个自己的Hook实现函数的委托对象 
// int d = (int)HookType.Keyboard;
_nextHookPtr = SetWindowsHookEx((int)HookType.Keyboard, myhookProc , IntPtr.Zero , GetCurrentThreadId()); //加到Hook链中 
}
}  public void UnHook()  {  if( _nextHookPtr != IntPtr.Zero )  { 
UnhookWindowsHookEx(_nextHookPtr); //从Hook链中取消 
_nextHookPtr = IntPtr.Zero; 
}  }  // 7、在Form1的Load事件中添加 SetHook() , 在Form1的closing 事件中添加 UnHook()  #region 组件设计器生成的代码
/// <summary> 
/// 设计器支持所需的方法 - 不要使用代码编辑器 
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtsave = new System.Windows.Forms.TextBox();
this.SuspendLayout();
// 
// txtsave
// 
this.txtsave.Location = new System.Drawing.Point(0, 0);
this.txtsave.Name = "txtsave";
this.txtsave.TabIndex = 0;
this.txtsave.Text = "";
// 
// SaveActiveX
// 
this.Controls.Add(this.txtsave);
this.Name = "SaveActiveX";
this.Size = new System.Drawing.Size(184, 72);
this.Load += new System.EventHandler(this.SaveActiveX_Load);
this.Paint +=new PaintEventHandler(SaveActiveX_Paint);
this.LostFocus +=new EventHandler(SaveActiveX_LostFocus);
this.HandleDestroyed +=new EventHandler(SaveActiveX_HandleDestroyed);
this.ResumeLayout(false); }
#endregion
private int a = 0;
private void SaveActiveX_Load(object sender, System.EventArgs e)
{
SetHook();
// this.txtsave.Text = "1212";
}// private void txtsave_LostFocus(object sender, EventArgs e)
// {
// this.txtsave.Text = "1212";
// } private void SaveActiveX_Paint(object sender, PaintEventArgs e)
{
try
{
// this.txtsave.Text = "1212";
// Random rd = new Random();
// this.txtsave.Text = Convert.ToString( rd.Next( 200 ) );
}
catch( Exception exp )
{
throw ( new Exception( exp.Message ));
}
} private void SaveActiveX_LostFocus(object sender, EventArgs e)
{
try
{
// this.txtsave.Text = "1212";
// Random rd = new Random();
// this.txtsave.Text = Convert.ToString( rd.Next( 200 ) );
}
catch( Exception exp )
{
throw ( new Exception( exp.Message ));
}
} private void SaveActiveX_HandleDestroyed(object sender, EventArgs e)
{
try
{
this.UnHook();
}
catch( Exception exp )
{
throw ( new Exception( exp.Message ));
}
}
}
}“DefaultDomain”: 已加载“c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll”,未加载符号。
“SaveTest”: 已加载“C:\Documents and Settings\zk\桌面\mydocment\PrintActiveX\SaveTest\bin\Debug\SaveTest.exe”,符号已加载。
“SaveTest.exe”: 已加载“c:\windows\assembly\gac\system.windows.forms\1.0.5000.0__b77a5c561934e089\system.windows.forms.dll”,未加载符号。
“SaveTest.exe”: 已加载“c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll”,未加载符号。
“SaveTest.exe”: 已加载“c:\windows\assembly\gac\system.drawing\1.0.5000.0__b03f5f7f11d50a3a\system.drawing.dll”,未加载符号。
“SaveTest.exe”: 已加载“c:\documents and settings\zk\桌面\mydocment\printactivex\savetest\bin\debug\remoteprint.dll”,符号已加载。
“SaveTest.exe”: 已加载“c:\windows\assembly\gac\mscorlib.resources\1.0.5000.0_zh-chs_b77a5c561934e089\mscorlib.resources.dll”,未加载符号。
未处理的“System.NullReferenceException”类型的异常出现在 未知模块 中。其他信息: 未将对象引用设置到对象的实例。程序“[3480] SaveTest.exe”已退出,返回值为 0 (0x0)。