RICHEDIT Rich text controls can hold a variety of fonts, so what if we want to know how high the head of the text is?
For example, we want to judge whether the text content in RichEdit is beyond the scope of RichEdit, how to achieve it?
1, need to use Em_formatrange message http://msdn.microsoft.com/en-us/library/bb788020%28VS.85%29.aspx
2, the implementation of the code is as follows: (The following is the implementation of BCB, StackOverflow on the implementation of Delphi
Http://stackoverflow.com/questions/3244139/how-to-get-text-extent-of-richedit-in-delphi)
//---------------------------------------------------------------------------#include<vcl.h>#pragmaHdrstop#include"Unit1.h"//---------------------------------------------------------------------------#pragmaPackage (Smart_init)#pragmaResource "*.DFM"TForm1*Form1;//---------------------------------------------------------------------------__fastcall Tform1::tform1 (tcomponent*owner): Tform (owner) {}//---------------------------------------------------------------------------void__fastcall Tform1::button1click (TObject *Sender) {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; ShowMessage ("Total Text Height"+INTTOSTR (Formatrange.rc.bottom * logy/1440)); RichEdit1->perform (Em_formatrange,0, NULL);//Free the cache if(Totalheight > (richedit1->clientheight+3) {showmessage ("text out of range! "); }
ReleaseDC (RICHEDIT1->HANDLE,RICHDC);}//---------------------------------------------------------------------------void__fastcall Tform1::button2click (TObject *Sender) {RichEdit1->font->size++;}//---------------------------------------------------------------------------void__fastcall Tform1::button3click (TObject *Sender) {RichEdit1->font->size--;}//---------------------------------------------------------------------------
BCB The total height of the text measured in RichEdit