The grammatical editor is a text editor that can edit the programming language. It performs a grammatical keyword for programming languages, such as annotation lines, strings, syntax, numbers, etc., so that the source program It is easier to read, VB, VC , Delphi and other development environments provide grammatical editing capabilities, and believe that developers will have some experience. This article describes how to design a grammar editor with VC , because the source program is longer, this article does not intend to list detailed code, but point out the design idea of the grammatical editor, in this article, finally accompany the download URL of the presentation. Interested friends can download trial.
Designing grammatical editors have multiple methods, and the author is ready to implement this function with the easiest way. We know that Windows offers Rich Edit control. This is a powerful editor. The WIDOWS's writing board is this control. From it, we can not only design a syntax editor, but also get its powerful editing capabilities. Rich Edit's support file is a riched20.dll This dynamic link library, its MFC interface is cricheditctrl, and its corresponding view class is CRICHEDITVIEW. CRICHEDITVIEW provides a setcharformat method that can be first considered when designing, how to use it for design, but according to the author's trial, this method is very efficient, especially for C , multi-line comments. We will change a thinking, in order to better understand the design ideas described in this article, let's make a trial:
To generate a single document project with VC , assume that it is named myProject, the final step of the Engineering Wizard, the sixth step, will change from CView from CView to crichEditView. After the project is generated, we add project wizard to the WM_PAINT message handler of the project, CMYPROJECTVIEW, but do not add any code in this function, that is, nothing. Now compilation, we see a editor that feels design, it is completely wrong, of course, this is natural, because we have not done anything in OnPain, so it does not redraw the window. We copy a piece of text from other places to paste it into this poor editor. Does it seem to have no response? Now drag the mouse, you will see that there is a black selection area, what is it? Yes, this seems to have text existence, although we don't know what text is! To make a further test, try to take the text in the window with cricheditctrl, you will get the text you can't see! Then we can draw these text in the same location, Rich Edit will restore its original appearance! If we analyze these texts, the specific text is drawn with a specific color. Is this not a grammatical editor in a dream?
In order to make the contents of the editor to be normal, the drawing of the text must be made exactly the default behavior of the editor, which requires the text to be drawn to the editor itself, otherwise the text misalignment will occur. This requires attention to two questions: First, because Rich Edit is exactly fine rolling, it may only display half of it, so you must calculate the top position of the first line; Second, Rich Edit TAB characters The problem, the width of the Tab does not constantly adjust, but constantly adjust according to the number of characters. Enter text in the WordPad, you can observe this situation. Solve a few questions said, a simple grammatical editor is very good!
This article link address: http://www.easysrc.com/ Design syntax editor with VC . RTF Source Code Download address: http://www.easysrc.com/synedit.rar website: http://www.easysrc.com