Delegate events and send messages

send message

Required references: using System.Runtime.InteropServices;

//Find the handle of the window by the title of the window
		[DllImport("User32.dll", EntryPoint = "FindWindow")]
		private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

//Message Sending API
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(
            IntPtr hWnd,        // Handle of window to which information is sent
            int Msg,            // Message ID
            int wParam,         // Parameter 1
            int lParam          //Parameter 2
        );
        
        //Custom window name, example:
        public enum WindowsName { Resident program, Debugger,Data Synchronization Program}

		//Customize the message to be sent, for example:
       public enum MsgType { Major Update System Messages,General update system messages }
       
       //Interface for sending messages
       public static void SendMsg(WindowsName winname, MsgType msgtype)
        {
            IntPtr hwnd = FindWindow(null, winname.ToString());
            if(hwnd != IntPtr.Zero)
            {
                SendMessage(hwnd, 0x61, 5,(int)msgtype);
            }
        }

receive messages

/// rewrite the message processing function DefWndProc of the form, and add the processing entry for the detection of its own definition message.
        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
                //Receive the custom message MYMESSAGE and display its parameters

                case 0x61:
                    {
                     		switch ((Win32APIMessage.MsgType)int.Parse(m.LParam.ToString()))
                            {
                                case Win32APIMessage.MsgType.Receiving System Message for Major Update of Debugger:
                                   //Operational Processing
                                    break;
                            }
                    }
                    break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

Entrustments and incidents

The so-called delegation is actually an observer, who cares about an event and acts when it is triggered.
For example, we first instantiate an observer class, dDownload Progress, to connect events to the observer we defined, so that whenever an onDownLoadProgress event is triggered, we notify the observer that we are not connecting using the downloader_onDownLoadProgress() method in the direct Observer class instance, but a delegation, and at this point The downloader_onDownLoadProgress () method is passed in the delegate. That is, I have a method, but I entrust you to help me relate to the event, because the event will only deal directly with the delegation, rather than a specific method.

public void StartDownload()
        {
            Downloader downloader = new Downloader();
            downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
            downloader.Start();
            isExit = false;
        }
        
public class Downloader
        {
            //Entrust
            public delegate void dDownloadProgress(long total, long current);
            //Event
            public event dDownloadProgress onDownLoadProgress;
            //Start the simulation
            public void Start()
            {
                for (int i = 0; i < 100; i++)
                {
                    bool isExit = Setup.isExit;
                    if (!isExit)
                    {
                        if (onDownLoadProgress != null)
                            onDownLoadProgress(100, i);
                        System.Threading.Thread.Sleep(100);
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
        
void downloader_onDownLoadProgress(long total, long current)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            if (this.InvokeRequired)
            {
                this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
                long test = current + 1;
                label6.Text = test.ToString() + "%";
                label6.Show();
            }
            else
            {
                this.progressBar1.Maximum = (int)total;
                this.progressBar1.Value = (int)current + 1;
            }
        }

Added by mediasix on Sun, 06 Oct 2019 09:15:13 +0300