Analysis of. Net winform control Keyboard Message Processing

Source: Internet
Author: User

On the winform control, we can see many Keyboard Message processing methods, such as onkeydown, onkeypress, processcmdkey, processdialogkey, and isinputkey. How are these methods organized, what is the specific meaning of each method? This article will focus on these issues and analyze the Keyboard Message Processing Process of the. NET winform control.


1.WinformMessage Loop

    As we all know, winform also relies on the underlying message mechanism. Generally, our winform application will start as follows:

     Application.Run(new Form());

     

    The above code starts a message loop in the current thread and displays the specified form.

    Decompile the run method of the application class. We can see this:

      public static void Run(Form mainForm)
    {
    ThreadContext.FromCurrent().RunMessageLoop(-1,new ApplicationContext(mainForm));
    }

     

    After the message loop is started, the operating system converts the UI input of the current application into a Windows message and sends it to the current thread for processing. This article focuses not on the Windows message mechanism, but on how the winform control handles the underlying messages after they reach the. NET layer.

     

    2.Message Processing

      We can see from the above that the message loop is constructed through the runmessageloop method of threadcontext type. So how does threadcontext process a specific windows message?

       

      By analyzing the threadcontext code, we can find that the call relationship is as follows:

      In the localmodalmessageloop method, we can see the processing of Windows messages:

       private bool LocalModalMessageLoop()
      {
      // ...
      if(!PreTranslateMessage(ref msg))
      {
      // ...
      UnsafeNativeMethods.TranslateMessage(ref msg);
      UnsafeNativeMethods.DispatchMessage(ref msg);
      }
      // ...
      }

       

      It can be found that a specific windows message is processed in two stages:

      • Pretranslatemessage
      • Dispatchmessage

      The processing of winform Control Messages starts from these two points.

       

      2.1Pretranslatemessage

      Pretranslatemessage provides a time to determine whether to dispatch the message. If the returned value is false, the message is sent to the winform control.

       

      Pretranslatemessage is divided into two layers. The first step is to call the current application's imessagefilter for processing. You can perform message preprocessing or message filtering at this layer. If the message is not filtered out, call the preprocessmessage method of the current control for message preprocessing.

       

      The control-type preprocessmessage processing process is as follows:

       

      For wm_keydown messages, the preprocessing control class has three opportunities: processcmdkey, isinputkey, and processdialogkey. For wm_keychar messages, the preprocessing has two opportunities: isinputchar and processdialogchar.

       

      Processcmdkey is used by default to process shortcuts and menu shortcuts. This method recursively calls the parent control. If the returned value is false, call isinputkey to determine whether to trigger the keydown event. If it is not an inputkey, call processdialogkey to check whether the key is a navigation key or perform some special processing. This method recursively calls the processing of the parent control.

       

      Isinputchar determines whether the input character is a common character. If the returned value is true, a keypress event is triggered. If the returned value is false, processdialogchar is called. processdialogchar is used to process the mnemonic key by default. For example, the control text is "& OK" and the char "O" is processed.

       

      2.2Dispatchmessage

      If the pretranslatemessage does not filter out the windows message, the message is distributed to the control and processed by the wndproc function of the control.

       

      Is the control processing process:

      After a message arrives at wnproc, it is processed by processkeymessage, processkeypreview, and processkeyeventargs. Each method returns a Boolean value indicating whether the control has processed the message.

       

      Processkeymessage processes all key messages from wndproc. It first calls the processkeypreview function of the parent control. If true is returned, the parent control has been processed. Otherwise, processkeyeventargs is called to trigger the keydown, keypress, and keyup events of the control.

       

       
      3.Conclusion

      This article focuses on the processing of keyboard messages by the winform control, and analyzes the functions of message preprocessing and processing in two stages. During the development of third-party controls, you can reload these functions as needed. In addition, you can get more inspiration from the Design and Implementation ideas.

        Contact Us

        The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

        If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

        A Free Trial That Lets You Build Big!

        Start building with 50+ products and up to 12 months usage for Elastic Compute Service

        • Sales Support

          1 on 1 presale consultation

        • After-Sales Support

          24/7 Technical Support 6 Free Tickets per Quarter Faster Response

        • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.