protected override void defwndproc(ref system.windows.forms.message m) { switch(m.msg) { case ...: break; default: base.defwndproc(ref m); break; } }
下面是我的c#实践例程。 ------------------------------------ ///////////////////////////////////////// ///file name: note.cs /// public class note { //声明 api 函数
[dllimport("user32.dll",entrypoint="sendmessage")] private static extern int sendmessage( int hwnd, // handle to destination window int msg, // message int wparam, // first message parameter int lparam // second message parameter ); [dllimport("user32.dll",entrypoint="findwindow")] private static extern int findwindow(string lpclassname,string lpwindowname); //定义消息常数 public const int user = 0x500; public const int test = user + 1;
//向窗体发送消息的函数
private void sendmsgtomainform(int msg) { int window_handler = findwindow(null,@"note pad"); if(window_handler == 0) { throw new exception("could not find main window!"); } sendmessage(window_handler,msg,100,200); } }
///////////////////////////////////////// /// file name : form1.cs /// 接收消息的窗体 ///
public class form1 : system.windows.forms.form { public form1() { // // required for windows form designer support // initializecomponent(); // // todo: add any constructor code after initializecomponent call // } /// 重写窗体的消息处理函数 protected override void defwndproc(ref system.windows.forms.message m) { switch(m.msg) { //接收自定义消息 user,并显示其参数 case note.user: string message = string.format ("received message! parameters are :{0},{1}",m.wparam ,m.lparam); messagebox.show (message); break; default: base.defwndproc(ref m); break; } //console.writeline(m.lparam); }