c#消息传递简单实现

消息传递在c#中是基于windows内部的,即是由系统内部进行处理的,编程人员也都是看不到的,只能用他封装出来的方法。现举一个简单小实例。

现有两个窗体,Form2,Form1,Form2上有一个button1,textBox1,Form1上有一个label1和一个button1,当点击Form1的button1按扭时,弹出Form2窗体,在textBox1输入的内容,将会显示到Form1上的Lable1上,具体代码实现如下:

Form1.cs

 

  public partial class Form1 : Form
    {
        //自定义的消息

        public const int USER = 0x500;
        public const int MYMESSAGE = USER + 1;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2(this.Handle);
            f.Show();
        }

        ///重写窗体的消息处理函数DefWndProc,从中加入自己定义消息 MYMESSAGE 的检测的处理入口
        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                //接收自定义消息MYMESSAGE,并显示其参数
                case MYMESSAGE:

                    Form2.My_lParam ml = new Form2.My_lParam();
                    Type t = ml.GetType();
                    ml = (Form2.My_lParam)m.GetLParam(t);
                    label1.Text = ml.s;

                    //SendCustomMessage.SENDDATASTRUCT myData = new SendCustomMessage.SENDDATASTRUCT();//这是创建自定义信息的结构
                    //Type mytype = myData.GetType();
                    //myData = (SendCustomMessage.SENDDATASTRUCT)m.GetLParam(mytype);//这里获取的就是作为LParam参数发送来的信息的结构
                    //textBox1.Text = myData.lpData; //显示收到的自定义信息
                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }

        }

 

Form2代码如下:

 public partial class Form2 : Form
    {
        //自定义的消息
        public const int USER = 0x500;
        public const int MYMESSAGE = USER + 1;

        public Form2()
        {
            InitializeComponent();
        }

        IntPtr HD;

        public Form2(IntPtr hd)
        {
            InitializeComponent();
            HD = hd;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr ptr = FindWindow(null, "Form1");//获取接收消息的窗体句柄
            //消息构建
            My_lParam m = new My_lParam();
            m.s = textBox1.Text;
            m.i = m.s.Length;
            SendMessage(ptr, MYMESSAGE, 1, ref m);//发送消息
        }

        public  struct My_lParam
        {
            public int i;
            public string s;
        }

        //消息发送API
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(
        IntPtr hWnd,        // 信息发往的窗口的句柄
        int Msg,            // 消息ID
        int wParam,         // 参数1
        ref My_lParam lParam
        );

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
    }

发送消息时,可以发送单个的信息,也可以发送一个类,一个结构,若是一个结构,发送函数像上面那样写就行了,若是发送单个的通知信息,刚如下:

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(
        IntPtr hWnd,        // 信息发往的窗口的句柄
        int Msg,            // 消息ID
        int wParam,         // 参数1
        ref object lParam
        );

发送消息如下:object mm = new object();
                  SendMessage(ptr, MYMESSAGE,, 1, ref mm);//发送消息

若是发送一个类

[DllImport("User32.dll", EntryPoint = "SendMessage")]

        public static extern int SendMessage(
       IntPtr hWnd,        // 信息发往的窗口的句柄
       int Msg,            // 消息ID
       int wParam,         // 参数1
       IntPtr lParam
       );

发送消息如下:

GCHandle gch = GCHandle.Alloc(类实例);
                            if (ptr!= null)
                            {
                               SendMessage(ptr, MYMESSAGE, 1, (IntPtr)gch);
                                gch.Free();

                             }

posted @ 2011-01-05 15:39  orchids  阅读(3512)  评论(4)    收藏  举报