C # is used to handle messages between a form associated events

             There is such a demand in the project, the main control process run or hide when the main form when the form's controls or other events, and other forms is active, while the main form or the main process is hidden behind of. Use this time to handle and message handling is better to solve these problems, and of course also possible to use other methods. For example, other forms will declare and define the main form, and make it as the main form has been in memory, declared public methods in each form, you can directly call when you need to call the primary process, but this cost a lot of system resources. Now we use the message to solve this problem. Below a small program, the main form to control and change the text color sub Label form through the handle and the message code is as follows:


Windowns API classes

using System;
using System.Runtime.InteropServices;

namespace TestHwnd
{
    public class Win32API
    {
         [DllImport("user32.dll ", CharSet = CharSet.Unicode)]
        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, string wParam, string lParam);

    }

}


The main window procedure (send message):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestHwnd
{
    public partial class Main : Form
    {

        //定义了一个子窗体的句柄
        public IntPtr hwndfrmTest;
        

        public Main()
        {
            InitializeComponent();
        }

     

        private void timer1_Tick(object sender, EventArgs e)
        {

            if(hwndfrmTest!=(IntPtr)0)
            {
                if(DateTime.Now.Second % 3 == 0)
                {
                    Win32API.PostMessage(hwndfrmTest, 0x60"""");
                }
                
                if(DateTime.Now.Second % 5 == 0)
                {
                    Win32API.PostMessage(hwndfrmTest, 0x61"""");
                }
                
            }
            
        }


        void Button2Click(object sender, EventArgs e)
        {
            frmTest frm=new frmTest();
            frm.Show(this);
        }
    }

Subform program (receiving messages)

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TestHwnd
{
     ///  <summary>
     ///  Description of frmTest.
     ///  </summary>
     public  partial  class frmTest : Form
    {
        Main main;
         public  frmTest()
        {
             //
             // The InitializeComponent() call is required for Windows Forms designer support.
             //
             InitializeComponent();
            
             //
             // TODO: Add constructor code after the InitializeComponent() call.
             //
        }
        
         void  FrmTest_Load( object sender, EventArgs e)
        {

            main = this.Owner as Main;

             // Initialize the handle of the form

            = main.hwndfrmTest  the this .handle;
        }
        
         /// rewritable window message processing function DefWndProc, which add their own custom message processing MYMESSAGE detected inlet
         protected   the override   void   DefWndProc ( REF  the Message m)
        {
             Switch  (m.Msg)
            {
                 Case   0x60 :
                    {
                        label1.ForeColor = Color.Red;
                        Label1.Text = the DateTime.Now. the ToString () +  "-"  +  "successful test ...., Oh, the red" ;
                    }
                     BREAK ;
                 Case   0x61 :
                    {
                        label1.ForeColor = Color.Blue;
                        Label1.Text = the DateTime.Now. the ToString () +  "-"  +  "successful test ...., Oh, the blue" ;
                    }
                     BREAK ;
                 default :
                     Base . DefWndProc ( REF  m);
                     BREAK ;
            }
        }
        
        
        
         void   the Button1Click ( Object  SENDER, EventArgs E)
        {
                main.hwndfrmTest = (IntPtr) ( 0);
             this. Close();
        }
    }
}













}

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/10/30/2231525.html

Guess you like

Origin blog.csdn.net/weixin_34318956/article/details/93054111