标签:
首先,这些功能支持RichEdit2.0 以上功能;
其次,用常规的方法是无法获得LineSpace 的;
也许RichEdit的作者有办法。
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/f67f6416-eab3-4aee-bfcc-85b9f26fde39/rich-edit-control-line-height
下面是richedit 作者的blog
https://blogs.msdn.microsoft.com/murrays/2010/01/12/richedit-versions-1-0-through-3-0/#comment-12105
下面是我的解决办法。
我发现了一个规律,当用EM_SETPARAFORMAT ,改变行间距的时候。整体文本区域的高度也改变了。
代码如下
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; static int LineSpace = 400; //--------------------------------------------------------------------------- __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)); } //---------------------------------------------------------------------------
由此我们得到启发,如果我们1开始,制定LinSpace 是1,然后逐渐累加。
当,文本区域的高度和默认文本高度相同时,是不是就得到,LineSpace 对应的值了呢?
问题1,如何得到文本区域的高度? 看我的另一个博客 http://www.cnblogs.com/songr/p/5485187.html
问题2,计算,这个超级简单;
下面是实现代码
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; static int LineSpace = 400; //--------------------------------------------------------------------------- __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)); } //--------------------------------------------------------------------------- int TForm1::getTextAreaHeight() { RichEdit1->Text = RichEdit1->Text.TrimRight(); int LogX,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); int totalHeight = formatrange.rc.bottom * LogY / 1440; RichEdit1->Perform(EM_FORMATRANGE,0,NULL); ReleaseDC(RichEdit1->Handle,richdc); return totalHeight; } void __fastcall TForm1::Button2Click(TObject *Sender) { //首先将RichEdit 设置成默认Single spacing RichEdit1->SelectAll(); PARAFORMAT2 Para; Para.cbSize = sizeof(Para); Para.dwMask = PFM_LINESPACING; Para.bLineSpacingRule = 0; SendMessage(RichEdit1->Handle, EM_SETPARAFORMAT, 0, LPARAM(&Para)); //得到默认情况下的文本区域高度 int defTextAreaHeight = getTextAreaHeight(); int i = 0; do { //不停的改变行间距,直到文本区域高度和默认情况下相同 i++; 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)); } //---------------------------------------------------------------------------
说明,当累加到 278 的时候,文本区域的高度相同了。也就是说,278 是,默认行高的基准线。
有了这个基准线之后。我就可以用这个278 作为,我们计算行高的基础值了。(例如,减少行高时,不能小于这个值。)
标签:
原文地址:http://www.cnblogs.com/songr/p/5633234.html