Richedit-related operations [c #-oriented]

Source: Internet
Author: User

Http://www.codeproject.com/KB/edit/richtextboxplus.aspx

Http://hi.baidu.com/shaxunyeman/blog/item/e6f0d43f848603c27d1e71ee.html

Http://www.cnblogs.com/wuhuacong/archive/2010/07/20/1781378.html

"Create the font table using the RichTextBox's current font and append": This keyword can be found by inserting an image.

"\ Rtf1 \ ansi \ ansicpg1252 \ deff0 \ deflang1033" this keyword also contains some information.

It is mentioned that

Private const string RTF_HEADER = @ "{\ rtf1 \ ansi \ ansicpg1252 \ deff0 \ deflang1033 ";

If modified, Chinese characters are supported:

Private const string RTF_HEADER = @ "{\ rtf1 \ ansi \ ansicpg936 \ deff0 \ deflang1033 \ deflangfe2052 ";


// It cannot be displayed if it is too wide.
RichTextBox1.ScrollBars = RichTextBoxScrollBars. Both;
RichTextBox1.WordWrap = false; // The image cannot be displayed if it is too wide.

--------------------------------------------------

Description of RTF Files

RTF is a very popular file structure. Many text editors support it. VB and other development tools even provide Richtxtbox controls. Programmers who write common tools should consider adding the function of reading and writing RTF files to their own software. This requires a full understanding of the structure of the RTF. Now, the most important means of publishing information is WWW. The conversion from RTF to HTML in editing software is also something programmers should consider. Although this function already exists in WORD, you cannot say to your customers: "First save your program as RTF and then use WORD ......".

The Structure Analysis and Application of the RTF file are discussed below.

1. analyze the structure of the RTF File

The structure of the RTF is not complex, but the content is numerous. This article cannot be explained one by one. It can only be discussed in general (if you want to read the detailed RTF document, you can find or contact the author on the Internet .) Each RTF file is a text file, which is formatted by the RTF reader during display. At the beginning of the file is {\ rtf, Which is essential as a sign of the RTF file. The RTF reader determines whether a file is in the RTF format based on it. Then there is the file header and body. The file header includes several data structures, such as the font table, file table, and color table. The font and table style in the text are formatted Based on the file header information. Each table is enclosed in a pair of braces and contains many commands starting with the character. For example, a color table is as follows:

{\ Colortbl; \ red0 \ green0 \ blue0; \ red0 \ green0 \ blue255; \ red0 \ green255 \ blue255; \ red0 \ green255 \ blue0; \ red255 \ green0 \ blue255; \ red255 \ green0 \ blue0; \ red255 \ green255 \ blue0; \ red255 \ green255 \ blue255; \ red0 \ green0 \ blue128; \ red0 \ green128 \ blue128; \ red0 \ green128 \ blue0; \ red128 \ green0 \ blue128; \ red128 \ green0 \ blue0; \ red128 \ green128 \ blue0; \ red128 \ green128 \ blue128; \ red192 \ green192 \ blue192 ;}

At the beginning, use \ colortbl to indicate the color table in braces, followed by \ red0 \ green0 \ blue0, and register a color. The red, green, and blue components of this color are all 0. Other tables. The file header is followed by the body, which consists of layout formatting commands, text, and various special commands. Only special commands are enclosed in braces, while layout formatting commands and text are "open" to separate text from commands. There is a "}" at the end of the file, which corresponds to the first. In the entire file, "}" and "{" must correspond one by one. This format is the basis of the RTF reader and converter algorithm.

The RTF format also has a special feature, that is, some characters have special meanings in the command, so when they appear as text, you need to add a "\" before them. for example, "\" must be expressed as "\". In fact, this form is common in most programming languages.

Ii. Algorithm Analysis

Although the algorithms described in this section are for reading and writing of RTF, they are also common methods for file filters and are suitable for conversion between files of various formats. Specifically, various files are converted into an intermediate format, and then displayed or converted as required. One principle must be followed: the program must be able to filter out unrecognized formats. Various files have their own special formats, which are inevitable during the conversion process. This situation should be considered in the algorithm. For formatted text files such as RTF, the most important thing is to correctly display or convert the file size, color, Font, and other styles. Therefore, a data structure should be used in the program to store the information. This structure is the so-called intermediate format. The flowchart is as follows:

Iii. Difficulties

We encountered many problems during the development process, two of which were particularly interesting.

The first problem is the representation of Chinese characters. In RTF, Chinese characters are expressed as "\ 'Internal code" in the form of commands ". The inner code is the inner code of the Chinese character machine. However, please note: the RTF is a text file, and the internal code is stored in ASCII code. You must convert it into numbers before using it. For example, the form of "electronics and computers" in RTF is:

\ 'B5 \ 'e7 \ 'd7 \ 'd3 \ 'd3 \ 'EB \ 'b5 \ 'e7 \ 'c4 \ 'd4

The second is the image problem, which is also the focus of this article. In RTF, images exist in two ways: the first method is direct embedding, starting with {\ pict; the second method is embedding as an OLE object, starting with {\ object. When the RTF processor can directly use OLE, the RTF file provides OLE data; otherwise, the file directly provides image data, starting with {\ result. The most common image format in use is METAFILE containing dib bitmap. This format is not described in the SDK and is stored in RTF in the form of compression, therefore, there are some difficulties in conversion. We adopt a relatively new method: First read the meta file data and save it as a FILE, and then use the GetMetafile and PlayMetafile functions to print the graphics in the FILE on a memory DC, finally, save it as a BITMAP file by capturing images. The following are specific procedures:

Void WmfToBmp (int width, int height, char * metafile)

{

// Width indicates the image width, height indicates the image height, and * metafile indicates the object name.

Static HMETAFILE hmf;

Char num [4];

FILE * fh;

HDC hMemDC;

HBITMAP hbmp, hold;

BITMAPFILEHEADER hdr;

LPBITMAPINFOHEADER lpbmpih;

HGLOBAL hg;



BitmapCount ++;

If (BitmapCount = 3)

{

Length = 128;

Height = 132;

}

If (BitmapCount = 1)

{

Width = 80;

Height = 50;

}

ToString10 (BitmapCount, num );

// Function for converting a string into a number

Lstrcpy (BMP file, FilePath );

Lstrcat (BMP file, num );

Lstrcpy (gifFile, BMP file );

Lstrcat (bmp file, ". bmp ");

Lstrcat (gifFile, ". gif ");

Hmf = GetMetaFile (metafile );

HDisplayDC = CreateDC ("DISPLAY", NULL );

HMemDC = CreateCompatibleDC (hDisplayDC );

Hg = GlobalAlloc (GHND, sizeof (BITMAPINFOHEADER ));

Lpbmpih = (LPBITMAPINFOHEADER) GlobalLock (hg );

Lpbmpih-> biSize = sizeof (BITMAPINFOHEADER );

Lpbmpih-> biWidth = width;

Lpbmpih-> biHeight = height;

Lpbmpih-> biPlanes = 1;

Lpbmpih-> biBitCount = 8;

Lpbmpih-> biCompression = 0;

Hbmp = CreateCompatibleBitmap (hDisplayDC, width, height );

GlobalUnlock (hg );

GlobalFree (hg );

Hold = SelectObject (hMemDC, hbmp );

SetMapMode (hMemDC, MM_ANISOTROPIC );

Setdomainworgex (hMemDC, NULL );

SetViewportExtEx (hMemDC, width, height, NULL );

PlayMetaFile (hMemDC, hmf );

Hbmp = SelectObject (hMemDC, hold );

If (fh = fopen (BMP file, "w + B") = NULL)

{

Return;

}

Hdr. bfType = 0x4d42;

Hdr. bfSize = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD) + width * height;

Hdr. bfReserved1 = 0;

Hdr. bfReserved2 = 0;

Hdr. bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD );

Fwrite (LPSTR) & hdr, sizeof (BITMAPFILEHEADER), 1, fh );

Hg = GlobalAlloc (GHND, sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD ));

Lpbmpih = (LPBITMAPINFOHEADER) GlobalLock (hg );

Lpbmpih-> biSize = sizeof (BITMAPINFOHEADER );

Lpbmpih-> biWidth = width;

Lpbmpih-> biHeight = height;

Lpbmpih-> biPlanes = 1;

Lpbmpih-> biBitCount = 8;

Lpbmpih-> biCompression = 0;

Lpbmpih-> biSizeImage = width * height;

Lpbmpih-> biClrUsed = 256;

Lpbmpih-> biClrImportant = 0;

J = GetDIBits (hMemDC, hbmp, 0, height, NULL, (BITMAPINFO *) lpbmpih, DIB_RGB_COLORS );

Lpbmpih-> biSize = sizeof (BITMAPINFOHEADER );

Fwrite (lpbmpih, sizeof (BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD), 1, fh );

GetDIBits (hMemDC, hbmp, 0, height, (LPSTR) FileBuffer, (BITMAPINFO *) lpbmpih, DIB_RGB_COLS );

Fwrite (FileBuffer, width * height, 1, fh );

GlobalUnlock (hg );

GlobalFree (hg );

Fclose (fh );

DeleteDC (hMemDC );

DeleteDC (hDisplayDC );

DeleteObject (hbmp );

DeleteObject (hold );

DeleteMetaFile (hmf );

} // End function WmfToBmp ()

Another advantage of using this method is that when you start developing the RTF reader from the bottom layer, you only need to change the PlayMetaFile parameter from the memory DC to the screen DC to display the image.

Iv. Extensions of the RTF Format

Finally, we will discuss the extensions of the RTF format. The RTF format should be unified as a standard, but in some cases, expansion is necessary. The most obvious example is Microsoft's WORD, which has its own unique RTF command. If you want to make your software technically advantageous, you can also create new RTF commands. For example, if you want to support DHTML in the software, you can embed commands such as {\ dhtml or {\ java in RTF. Since the RTF reader provides the function of filtering commands that do not know each other, this will not affect the versatility of the RTF file.

Supplement:
RTF file format description:
1. \ ansi Character Set Flag Character Set Name
2. \ B Character Formatting Properties Toggle Character is bold
3. \ b0 Character Formatting Properties Toggle characters are not in bold
4. \ blueNNN Color Table Value identifies the blue rgb Value (0-255)
5. \ brdrb Paragraph Borders Flag section bottom boundary
6. \ brdrcfNNN Paragraph Borders Value section boundary color
7. \ brdrs Paragraph Borders Flag section boundary single line Border
8. \ brdrt Paragraph Borders Flag section boundary top
9. \ brdrth Paragraph Borders Flag section border double line Border
10. \ brdrwNNN Paragraph Borders Value width of the section boundary (unit: dike)
11. \ cfNNN Character Formatting Properties Value (0-255)
12. \ chpgn Special Characters Symbol current page number
13. \ colortbl Color Table Destination Color Table
14. \ deffNNN Font Table Value default Font (N is the Font number)
15. \ deftabNNN Document Formatting Properties the default tab width in the Document (unit: dike, default Value: 720 dike)
16. \ emdash Special Characters Symbol long description number -- (Special Symbol)
17. \ endash Special Characters Symbol short description-(Special Symbol)
18. font of \ fNNN Character Formatting Properties Value (N is the number in the font table)
19. \ fonttbl Font Table Destination Font Table
20. \ fonttype fontname font type, font name
21. \ footer Headers and Footers Destination footer of all pages.
22. \ footeryNNN Section Formatting Properties the distance between the footer and the bottom of the part (unit: dike, default: 720 dike)
23. \ fsNNN Character Formatting Properties the font size of the Value Character in the halfwidth state (24 by default)
24. \ greenNNN Color Table Value identifies the green rgb Value (0-255)
25. \ headeryNNN Section Formatting Properties the distance between the header and the top of the Value segment (unit: dike)
26. \ I Character Formatting Properties Toggle Character is Italic
27. \ i0 Character Formatting Properties Toggle characters are not italic characters
28. \ keepn Paragraph Formatting Properties
29. \ line Special Characters: line Break (not a paragraph separator)
30. \ li Paragraph Formatting Properties left indent of the Value section (0 by default)
31. \ margbNNN Document Formatting Properties the height of the white space at the bottom of the Document (unit: dike)
32. \ marglNNN Document Formatting Properties Value width of the left blank area of the Document (unit: dike)
33. \ margrNNN Document Formatting Properties the width of the right white space in the Document (unit: dike)
34. \ margtNNN Document Formatting Properties the height of the white space at the top of the Document (unit: dike)
35. \ 'special Characters sympost'
36. One of the \ page Special Characters Symbol separators: Paging character
37. \ par Special Characters Symbol new paragraph mark
38. \ pard Paragraph Formatting Properties the Flag section is used to separate special information such as footer, comment, header, and footer into a separate group. It is used to separately specify the format independent of the text content for each group.
39. \ plain Character Formatting Properties Flag restores the Character format to the default format specified by the application, and the relevant Character format to the default format specified by the application.
40. \ qc Paragraph Formatting Properties Flag section format Center
41. \ qj Paragraph Formatting Properties both ends of the Flag section are aligned
42. \ ql Paragraph Formatting Properties left-aligned section format
43. \ qr Paragraph Formatting Properties right-aligned section format
44. \ redNNN Color Table Value identifies the red rgb Value
45. \ riNNN Paragraph Formatting Properties Value right indent Value of the section format (default Value: 0)
46. \ rtf RTF Version Destination RTF Version
47. \ slmultNNN Paragraph Formatting Properties multi-times row spacing of the section of the Value 0 "minimum" or "strict" line spacing 1-multiple times row spacing (compared to single-times row spacing)
The specified line data of the 48. \ slN Paragraph Formatting Properties Value section can be adjusted or the maximum line height is the character height by default.
49. \ tab Special Characters Symbol TAB character
50. \ titlepg Section Formatting Properties the first page in special format in the Flag segment
51. \ tqr Tabs Flag fill the right part with a TAB
52. \ tx Tabs Value TAB position, starting from the blank area on the left (instead of starting from the edge of the Left page, unit: dike)
53. \ ul Character Formatting Properties Toggle characters have continuous underlines (\ ul0 turn off continuous underlines)
54. \ widowctrl Document Formatting Properties the lone row control of the Flag Document

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.