• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

C# 中如何定义和接收消息?

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网 Wason

    业余学习成果: 终于把自定义消息给搞定,好事多多分享!

    在C#中目前我还没有找到发送消息的类成员函数,所以只能采用通过调用WIN 32 API 的 SendMessage() 函数实现。由于 SendMessage的参数中需要得到窗体的句柄(handler) ,所以又要调用另一个API FindWindow(), 两者配合使用,达到在不同窗体之间的消息发送和接收功能。

    另外一个要点是,需要通过重写(Override) 窗体的 DefWndProc() 过程来接收自定义的消息。DefWndProc 的重写:

     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);
}

--

Wilson Wei

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网