Saving a RichEdit to a Metafile

Source: Internet
Author: User

Introduction
Sometimes, you may want to store the contents of a rich edit control in a Metafile.

This article outlines an approach that stores this type of control's contents in enhanced metafiles, one page per Metafile.

Metafiles are useful when you want the content to be scalable at different resolutions

The following code demonstrates one method to dump the contents of a rich edit control into EMF files (one per page ):

 

The Delphi source code
Unit richeditemfprint; </P> <p> interface </P> <p> uses <br/> Windows, sysutils, RichEdit, commdlg, classes, messages, comctrls; </P> <p> procedure richedittometafile (acontrol: trichedit; afilename: string); </P> <p> Implementation </P> <p> // getprinterdc () <br/> // returns a printer DC-uses printer Common Dialog <br/> function getprinterdc: HDC; <br/> var <br/> pdlg: tprintdlg; <br/> begin <br/> fillchar (pdlg, sizeof (tprintdlg), 0); <br/> pdlg. lstructsize: = sizeof (tprintdlg); <br/> pdlg. flags: = pd_returndc; <br/> printdlg (pdlg); <br/> result: = pdlg. HDC; <br/> end; </P> <p> // get the length, in characters, of the text in the Control <br/> function getrtftextlength (hwndrtf: hwnd): integer; <br/> begin <br/> result: = sendmessage (hwndrtf, wm_gettextlength, 0, 0); <br/> end; </P> <p> // rtftoemf-tell the control to draw itself on the EMF <br/> // parameters: <br/> // hrefdc is used to create the EMF <br/> // pszmetafilename is the file name of the new EMF (can be nil) <br/> // prcmeta is the rect used to in createenhmetafile (), in 0.01 <br/> // units (shocould not be nil) <br/> // hwndrtf is the control of interest <br/> // nstart is the starting character location <br/> // pend is a integer which has es the position of <br/> // The next character to print after this page <br/> function rtftoemf (hrefdc: HDC; pszmetafilename: lpctstr; prcmeta: trect; <br/> hwndrtf: hwnd; nstart: integer; var pend: integer): henhmetafile; <br/> var <br/> hmetadc: HDC; <br/> fr: formatrange; <br/> ntextprinted: integer; <br/> begin <br/> // create the EMF <br/> hmetadc: = createenhmetafile (hrefdc, pszmetafilename, @ prcmeta, nil ); <br/> If (hmetadc = 0) Then <br/> begin <br/> result: = 0; <br/> exit; <br/> end; </P> <p> zeromemory (@ FR, sizeof (FR); <br/> // set up the page (convert 0.01 to twips) <br/> Fr. rcpage. top: = prcmeta. left * 1440 Div 2540; <br/> Fr. rcpage. left: = prcmeta. top * 1440 Div 2540; <br/> Fr. rcpage. right: = prcmeta. right * 1440 Div 2540; <br/> Fr. rcpage. bottom: = prcmeta. bottom * 1440 Div 2540; <br/> // set up no margins all around. <br/> Fr. RC: = Fr. rcpage; <br/> // set up the range of text to print as nstart to end of document <br/> Fr. chrg. CPMin: = nstart; <br/> Fr. chrg. cpmax: =-1; <br/> Fr. hdctarget: = hmetadc; <br/> Fr. HDC: = Fr. hdctarget; <br/> // tell the control to draw itself on our (Meta) DC <br/> ntextprinted: = sendmessage (hwndrtf, em_formatrange, 1, INTEGER (@ (FR); <br/> pend: = ntextprinted; <br/> result: = closeenhmetafile (hmetadc); <br/> end; </P> <p> // dumprtftopagedemfs-demonstrates using rtftoemf () to create an EMF <br/> // for each page in an RTF Control <br/> // parameters: <br/> // hwndrtfcontrol-the control <br/> // szemffiletitlebase-base filename for EMF files, number is appended <br/> procedure encryption (hwndrtfcontrol: hwnd; szemffiletitlebase: lptstr); <br/> var <br/> szmetaname: string; <br/> nrtftextlength, nstart, npage: integer; <br/> hrefdc: HDC; <br/> rcmeta: trect; <br/> hemf: henhmetafile; </P> <p> begin <br/> // first, determine how character chars are in the RTF <br/> nrtftextlength: = getrtftextlength (hwndrtfcontrol); <br/> // get a reference DC (based on a printer) <br/> hrefdc: = getprinterdc (); <br/> // set up the meta rect For 0.01mm units <br/> rcmeta: = classes. rect (0, 0, getdevicecaps (hrefdc, horzsize) * 100, <br/> getdevicecaps (hrefdc, vertsize) * 100); </P> <p> npage: = 0; <br/> nstart: = 0; <br/> while nstart <nrtftextlength DO <br/> // loop while we 've not reached the end of the text in the Control <br/> begin <br/>/ /construct a file name for this page <br/> szmetaname: = format ('% S % d. emf', [szemffiletitlebase, npage]); <br/> // call function above to draw this portion of the RTF on the EMF <br/> hemf: = rtftoemf (hrefdc, pchar (szmetaname), rcmeta, hwndrtfcontrol, <br/> nstart, nstart); <br/> // clean up <br/> deleteenhmetafile (hemf ); <br/> Inc (npage); <br/> If nstart = 0 then <br/> break; <br/> end; </P> <p> procedure richedittometafile (acontrol: trichedit; afilename: string); <br/> begin <br/> dumprtftopagedemfs (acontrol. handle, pchar (afilename); <br/> end; </P> <p> end. </P> <p>

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.