• C#消息模拟


    C#中消息的工作流程:

     C#中的消息被Application类从应用程序消息队列中取出,然后分发到消息对应的窗体,窗体对象的第一个响应函数是对象中的protected override void WndProc(ref System.Windows.Forms.Message e)方法。
        它再根据消息的类型调用默认的消息响应函数(如OnMouseDown),默认的响应函数然后根据对象的事件字段(如this.MouseDown )中的函数指针列表,调用用户所加入的响应函数(如Form1_MouseDown1和Form1_MouseDown2),而且调用顺序和用户添加顺序一致

    根据这个流程,我做了个模仿程序,有不足的地方还请大家提供更完善的补充。

    using System;

     

    //创建一个委托,返回类型void,两个参数

    public delegate void KeyDownEventHandler(object sender, KeyEventArgs e);

    //数据参数类

    class KeyEventArgs : EventArgs  

    {

        private char keyChar;

        public KeyEventArgs(char keyChar)

            : base()

        {

            this.keyChar = keyChar;

        }

        public char KeyChar

        {

            get { return keyChar; }

        }

    }

     

    //模仿Application类

    class M_Application

    {

     

        public static void Run(M_Form form)

        {

            bool finished = false;

            do

            {

                Console.WriteLine("Input a char");

                string response = Console.ReadLine();

                char responseChar = (response == "") ? ' ' : char.ToUpper(response[0]);

                switch (responseChar)

                {

                    case 'X':

                        finished = true;

                        break;

                    default:

                        //得到按键信息的参数

                        KeyEventArgs keyEventArgs = new KeyEventArgs(responseChar);

                        //向窗体发送一个消息

                        form.WndProc(keyEventArgs);

                        break;

     

                }

            } while (!finished);

        }

    }

    //模仿窗体类

    class M_Form

    {

     

        //定义事件

        public event KeyDownEventHandler KeyDown;

        public M_Form()

        {

            this.KeyDown += new KeyDownEventHandler(this.M_Form_KeyDown);

        }

     

        //事件处理函数

        private void M_Form_KeyDown(object sender, KeyEventArgs e)

        {

            Console.WriteLine("Capture key:{0}", e.KeyChar);

        }

     

        //窗体处理函数

        public void WndProc(KeyEventArgs e)

        {

            KeyDown(this, e);

        }

     

    }

     

    //主程序运行

    class MainEntryPoint

    {

        static void Main()

        {

            M_Application.Run(new M_Form());

        }

    }

  • 相关阅读:
    Angular14 Angular相关命令
    Angular14 Visual Studio Code作为Angular开发工具常用插件安装、json-server安装与使用、angular/cli安装失败问题、emmet安装
    Material使用03 MdCardModule模块、MdInputModule模块
    Material使用02 图标MdIconModule、矢量图作为图标使用及改进
    Material使用01 侧边栏MdSidenavModule、工具栏MdTollbarModule
    阿里巴巴Druid数据库连接池的使用
    利用generator自动生成model(实体)、dao(接口)、mapper(映射)
    c++拷贝函数详解(转)
    c++友元函数友元类
    c++中虚函数与纯虚函数的区别(转)
  • 原文地址:https://www.cnblogs.com/0515offer/p/4191936.html
Copyright © 2020-2023  润新知