C # code editing box (1) receiving input,
In this article, I only want to write my first blog about this project. I have to worry about typographical wording.
When it comes to text editing, the box must be a control that can accept input.
Create a control first
1 public class CodeEdit : UserControl 2 { 3 }
View Code
Our controls should not only accept English input, but also Chinese Input. Therefore, we use ImmAssociateContext to obtain the input.
Create an Imm class to complete receiving input.
1 class ImmManage 2 {3 # region event 4 // <summary> 5 // input text event 6 /// </summary> 7 public delegate void EventInputText (char ch); 8 /// <summary> 9 /// input text event 10 /// </summary> 11 public event EventInputText InputText; 12 # endregion 13 14 # region private field 15 IntPtr hIMC; 16 IntPtr handle; 17 private const int WM_IME_SETCONTEXT = 0x0281; 18 private const int WM_CHAR = 0x0102; 19 # endregion 20 21 # region constructor 22 public ImmManage (IntPtr handle) 23 {24 hIMC = ImmGetContext (handle); 25 this. handle = handle; 26} 27 # endregion 28 29 # region method 30 // <summary> 31 // enter event 32 /// </summary> 33 // <param name =" m "> </param> 34 public void ImmOperation (Message m) 35 {36 if (m. msg = ImmManage. WM_IME_SETCONTEXT & m. WParam. toInt32 () = 1) 37 {38 this. immAssociateContext (handle); 39} 40 if (m. msg = WM_CHAR) 41 {42 KeyEventArgs e = new KeyEventArgs (Keys) (int) (long) m. WParam) | Control. modifierKeys); 43 InputText (char) e. keyData ); // trigger input event 44} 45} 46 # endregion 47 48 # region Win Api 49 // <summary> 50 // create an association between the specified input environment and window 51/ // </summary> 52 // <param name = "hWnd"> </param> 53 // <returns> </returns> 54 private IntPtr ImmAssociateContext (IntPtr hWnd) 55 {56 return ImmManage. immAssociateContext (hWnd, hIMC); 57} 58 59 [DllImport ("Imm32.dll")] 60 private static extern IntPtr ImmGetContext (IntPtr hWnd); 61 [DllImport ("Imm32.dll")] 62 private static extern IntPtr ImmAssociateContext (IntPtr hWnd, IntPtr hIMC); 63 [DllImport ("imm32.dll", CharSet = CharSet. auto)] 64 private static extern int ImmCreateContext (); 65 [DllImport ("imm32.dll", CharSet = CharSet. auto)] 66 private static extern bool ImmDestroyContext (int hImc); 67 [DllImport ("imm32.dll", CharSet = CharSet. auto)] 68 private static extern IntPtr SetFocus (IntPtr hWnd); 69 [DllImport ("Imm32.dll", CharSet = CharSet. unicode)] 70 private static extern int ImmGetCompositionStringW (IntPtr hIMC, int dwIndex, byte [] lpBuf, int dwBufLen); 71 # endregion 72}
View Code
Here we only need to call the constructor to pass in the control handle and then process the WndProc message.
Process the control construction method first.
1 public CodeEdit() 2 { 3 ImmManage = new ImmManage(this.Handle); 4 ImmManage.InputText += Input; 5 isInitialization = true; 6 }
View Code
Here I added an isInitialization variable to prevent the ImmManage class from calling methods in the object when it is not created. At the same time, the input event is registered.
Process Win messages
1 protected override void WndProc (ref Message m) 2 {3 base. wndProc (ref m); 4 if (isInitialization) 5 {6 this. immManage. immOperation (m); // Input Method 7} 8}
View Code
At this time, you only need to complete the Event Callback method to obtain the input text.
1 /// <summary> 2 // input event 3 /// </summary> 4 /// <param name = "ch"> </param> 5 private void inPut (char ch) 6 {7 Console. writeLine (ch. toString (); 8}
View Code
Now, we have received the text. In the next chapter, we have saved the text.