The grammar editor is a text editor that can be used to edit the grammar of the programming language, which is used to lighten the syntax keyword of the programming language, such as annotation line, string, grammar key word, numeral, etc., which makes the source program easier to read, VB, VC + +, DELPHI and other development environment has provided the function of grammar editing, I believe that developers will have a personal experience.
This article will describe how to design a grammar editor with VC + +, because the source program is longer, this article does not intend to list detailed code, but to point out the syntax editor design ideas, at the end of this article with a demo program download URL, interested friends can download the trial.
There are several ways to design the grammar editor, and I am prepared to do it in the simplest way. We know that Windows offers rich edit control, which is a powerful editor, and Windows WordPad uses this control. From it, we can not only design the grammar editor, but also obtain its powerful editing capabilities. Rich Edit Support File is riched20.dll this dynamic link library, its MFC interface is CRichEditCtrl, its corresponding view class is CRichEditView. CRichEditView provides a Setcharformat method, which may be the first consideration in design is how to use it for design, but according to the author's experiment, this method is inefficient, especially for C + + such a multiline annotation language. We will change a way of thinking, in order to better understand the design ideas in this paper, we first to do a test:
First with VC + + to generate a single document project, assuming its name is MyProject, in the last step of the Engineering Wizard is the sixth step, the base class from CView to CRichEditView. After we build the project, we use the Engineering Wizard to add the WM_PAINT message processing function of the engineering view class Cmyprojectview, but do not add any code in the function, that is, do nothing. Now compile execution, we see a poorly designed editor, and its content window displays completely wrong, naturally, because we did nothing in the OnPaint, so it doesn't redraw the window. We copied a piece of text from somewhere else and pasted it into this poor editor, and it didn't seem to be responding? Now drag the mouse, you will see that there is a black selection area appears, what do you perceive? Yes, this clumsy editor seems to have text in it, although we don't know what the text is! To do further experiments, try to use the CRichEditCtrl Getline method to get the text in the window, you will receive the text that you just can't see! Then we can draw the text in the same position, and Rich edit will restore its original face! If we analyze the obtained text and draw the specific text in a particular color, isn't that the Dream grammar editor?
To make the contents of the editor appear normal, you must make the drawing of the text exactly the same as the default behavior of the editor, which requires that the drawn text be the same as the text position of the editor itself, otherwise there will be a text dislocation error. This requires a two-point note:
One, because rich edit is accurate and precise scrolling, it may only show half of the first line, so you must calculate the top position of the first row;
Two, Rich edit the tab character problem, the tab represents the width is not immutable, but according to the number of characters constantly adjusted. Enter the text in the WordPad and you can observe the situation.
This article supporting source code