gpt4 book ai didi

Delphi TRichEdit.Perform( EM_FORMATRANGE, 0,Longint(@fmtRange));

转载 作者:行者123 更新时间:2023-12-01 22:34:41 24 4
gpt4 key购买 nike

我想找到需要 TRichEdit 控件呈现自身而不显示垂直滚动条 的高度。我用这个代码

function RichEditHeight(var RE : TRichEdit; aForm : TForm) : integer;
var fmtRange: TFormatRange;
begin
FillChar(fmtRange, SizeOf(fmtRange), 0);
with fmtRange do begin
hDC := aForm.canvas.Handle;
hdcTarget := hDC;
rc.left := 0;
rc.right := (RE.ClientWidth * 1440) div screen.pixelsPerInch;
rc.top := 0;
rc.Bottom := MaxInt;
rcPage := rc;
chrg.cpMin := 0;
chrg.cpMax := -1;
end;
RE.Perform(EM_FORMATRANGE, 0, Longint(@fmtRange));
result := round(fmtRange.rc.Bottom*screen.pixelsPerInch/1440);
RE.Perform(EM_FORMATRANGE, 0, 0);
end;

当文档有几页(<15 A4 纵向)时,结果就足够了。但是随着页面的增多,rc.bottom 似乎被截断了,并且控件需要垂直滚动条。问题是:perform 的代码内部有一些限制吗?如果我手动(大约)增加 rc.bottom ,那么控件本身就可以正常显示,但这不是我想要的情况。

PS。如果重要的话,richedit 组件实际上是一个 TjvRichEdit 组件。

这是一个可以重现问题的最小程序(即使是 2 页)

unit Unit32;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, winAPI.richedit, JvExStdCtrls, JvRichEdit, Vcl.ExtCtrls;

type
TForm32 = class(TForm)
ScrollBox1: TScrollBox;
Button1: TButton;
RichEdit1: TJvRichEdit;
panel1: TPanel;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var Form32: TForm32;

implementation

{$R *.dfm}

function RichEditHeight(var RE : TjvRichEdit; aForm : TForm) : integer;
var fmtRange: TFormatRange;
begin
FillChar(fmtRange, SizeOf(fmtRange), 0);
with fmtRange do begin
hDC := aForm.canvas.Handle;
hdcTarget := hDC;
rc.left := 0;
rc.right := (RE.ClientWidth * 1440) div screen.pixelsPerInch;
rc.top := 0;
rc.Bottom := 500000;
rcPage := rc;
chrg.cpMin := 0;
chrg.cpMax := -1;
end;
RE.Perform(EM_FORMATRANGE, 0, Longint(@fmtRange));
result := round(fmtRange.rc.Bottom*screen.pixelsPerInch/1440);
RE.Perform(EM_FORMATRANGE, 0, 0);
end;

procedure TForm32.Button1Click(Sender: TObject);
begin
with OpenDialog1 do
if execute then begin
RichEdit1.lines.LoadFromFile(filename);
panel1.height := RichEditHeight(RichEdit1,Form32);
end;
end;

end.

表格

object Form32: TForm32
Left = 0
Top = 0
Caption = 'Form32'
ClientHeight = 615
ClientWidth = 874
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ScrollBox1: TScrollBox
Left = 0
Top = 0
Width = 874
Height = 615
Align = alClient
TabOrder = 0
ExplicitWidth = 885
ExplicitHeight = 583
object Button1: TButton
Left = 632
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object panel1: TPanel
Left = 0
Top = 0
Width = 457
Height = 561
Caption = 'panel1'
TabOrder = 1
object RichEdit1: TJvRichEdit
Left = 1
Top = 1
Width = 455
Height = 559
Align = alClient
Font.Charset = GREEK_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
ParentFont = False
ScrollBars = ssVertical
SelText = ''
TabOrder = 0
ExplicitHeight = 279
end
end
end
object OpenDialog1: TOpenDialog
Left = 656
Top = 88
end
end

以及 RTF 文档的结果(仅 2 页) the document is in RTF mode and as you can see the TjvRichEdit hasn't found its right height to render without its scrollbar

最佳答案

最后,解决方案非常简单!TRichedit 有 onResizeRequest 事件,其中控件的实际高度由 rect 参数给出

关于Delphi TRichEdit.Perform( EM_FORMATRANGE, 0,Longint(@fmtRange));,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395141/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com