First of all, these functions support RichEdit2.0 above functions;
Secondly, the conventional method is unable to obtain the linespace;
- You won't get it using Em_getparaformat, you'll find that the value of dylinespacing is always 0.
- You first Em_setparaformat, specify Blinespacingrule = 4, and then use GetParaFormat to fetch dylinespacing is also 0
Maybe RichEdit's author has a way.
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/f67f6416-eab3-4aee-bfcc-85b9f26fde39/ Rich-edit-control-line-height
Below is the blog of the RichEdit author
https://blogs.msdn.microsoft.com/murrays/2010/01/12/richedit-versions-1-0-through-3-0/#comment-12105
Here is my solution.
I found a pattern when using Em_setparaformat to change the spacing between lines. The height of the whole text area also changed.
The code is as follows
//---------------------------------------------------------------------------#include<vcl.h>#pragmaHdrstop#include"Unit1.h"//---------------------------------------------------------------------------#pragmaPackage (Smart_init)#pragmaResource "*.DFM"TForm1*Form1;Static intLinespace = -;//---------------------------------------------------------------------------__fastcall Tform1::tform1 (tcomponent*owner): Tform (owner) {}//---------------------------------------------------------------------------void__fastcall Tform1::button1click (TObject *Sender) {RichEdit1-SelectAll (); PARAFORMAT2 Para; Para.cbsize=sizeof(Para); Para.dwmask=pfm_linespacing; Para.blinespacingrule=4; Para.dylinespacing=Linespace; SendMessage (RichEdit1->handle, Em_setparaformat,0, LPARAM (&Para));}//---------------------------------------------------------------------------
From this we get inspired, if we start at 1, the Linspace is 1, then gradually accumulate.
When the height of the text area and the default text height are the same, do you get the corresponding value of Linespace?
Question 1, how do I get the height of the text area? Look at one of my other blogs http://www.cnblogs.com/songr/p/5485187.html
Question 2, calculation, this super simple;
Here is the implementation code
//---------------------------------------------------------------------------#include<vcl.h>#pragmaHdrstop#include"Unit1.h"//---------------------------------------------------------------------------#pragmaPackage (Smart_init)#pragmaResource "*.DFM"TForm1*Form1;Static intLinespace = -;//---------------------------------------------------------------------------__fastcall Tform1::tform1 (tcomponent*owner): Tform (owner) {}//---------------------------------------------------------------------------void__fastcall Tform1::button1click (TObject *Sender) {RichEdit1-SelectAll (); PARAFORMAT2 Para; Para.cbsize=sizeof(Para); Para.dwmask=pfm_linespacing; Para.blinespacingrule=4; Para.dylinespacing=Linespace; SendMessage (RichEdit1->handle, Em_setparaformat,0, LPARAM (&Para));}//---------------------------------------------------------------------------inttform1::gettextareaheight () {RichEdit1->text = richedit1->text.trimright (); intlogx,logy; HDC RICHDC= GetDC (richedit1->Handle); LOGX=GetDeviceCaps (RICHDC, logpixelsx); Logy=GetDeviceCaps (RICHDC, logpixelsy); FormatRange FormatRange= {0}; FORMATRANGE.HDC=Richdc; Formatrange.hdctarget=Richdc; Formatrange.rc.left=0; Formatrange.rc.top=0; Formatrange.rc.right= Richedit1->clientwidth *1440/LOGX; Formatrange.rc.bottom= screen->height*1440/logy; Formatrange.rcpage=formatrange.rc; Formatrange.chrg.cpMin=0; Formatrange.chrg.cpMax= -1; RichEdit1->perform (Em_formatrange,0,(Long) &FormatRange); intTotalheight = Formatrange.rc.bottom * Logy/1440; RichEdit1->perform (Em_formatrange,0, NULL); ReleaseDC (RichEdit1-HANDLE,RICHDC); returntotalheight;}void__fastcall Tform1::button2click (TObject *Sender) {//first set the RichEdit as the default single spacingRichedit1->SelectAll (); PARAFORMAT2 Para; Para.cbsize=sizeof(Para); Para.dwmask=pfm_linespacing; Para.blinespacingrule=0; SendMessage (RichEdit1->handle, Em_setparaformat,0, LPARAM (&Para));//get the text area height by default intDeftextareaheight =gettextareaheight (); inti =0; Do {//Keep changing line spacing until the text area is the same height as by defaulti++; PARAFORMAT2 Para; Para.cbsize=sizeof(Para); Para.dwmask=pfm_linespacing; Para.blinespacingrule=4; Para.dylinespacing=i; SendMessage (RichEdit1->handle, Em_setparaformat,0, LPARAM (&Para)); } while(Deftextareaheight! =gettextareaheight ()); ShowMessage (IntToStr (i));//---------------------------------------------------------------------------
Note that the text area is the same height when it accumulates to 278. That is, 278 is the baseline of the default line height.
With this baseline. I can use this 278 as, we calculate the base value of the row height. (For example, when you reduce row height, you cannot be less than this value.) )
Get RichEdit default line spacing in BCB