其他分享
首页 > 其他分享> > BCB 中测量Richedit 的文本总行高

BCB 中测量Richedit 的文本总行高

作者:互联网

BCB 中测量Richedit 的文本总行高

1,需要使用EM_FORMATRANGE 消息

2,实现的代码如下:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    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;
    ShowMessage("文本总高度"+IntToStr(formatrange.rc.bottom * LogY / 1440));
    RichEdit1->Perform(EM_FORMATRANGE,0,NULL);//free the cache

    if (totalHeight > (RichEdit1->ClientHeight+3))
    {
        ShowMessage("文字超出范围!");
    }

    ReleaseDC(RichEdit1->Handle,richdc);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    RichEdit1->Font->Size++;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    RichEdit1->Font->Size--;
}
//---------------------------------------------------------------------------

标签:formatrange,Richedit,TForm1,BCB,LogY,RichEdit1,richdc,rc,总行
来源: https://blog.csdn.net/huang714/article/details/88785936