我有一个Delphi2007的TRichEdit,里面有几行代码。我想要垂直滚动richedit,使特定的行号大致居中于richedit的可见/显示区域。例如,我想在本例中为CenterLineInRichEdit编写代码:
procedure CenterLineInRichEdit(Edit: TRichEdit; LineNum: Integer);
begin
...
Edit.ScrollTo(...);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
REdit: TRichEdit;
i: Integer;
begin
REdit := TRichEdit.Create(Self);
REdit.Parent := Self;
Redit.ScrollBars := ssVertical;
REdit.SetBounds(10, 10, 200, 150);
for i := 1 to 25 do
REdit.Lines.Add('This is line number ' + IntToStr(i));
CenterLineInRichEdit(REdit, 13);
end;我研究了使用WM_VSCROLL消息,它允许上下滚动一行,等等,但不能滚动到特定行的中心。
发布于 2010-05-14 00:05:10
试一试;
procedure VertCenterLine(RichEdit: TRichEdit; LineNum: Integer);
// I don't know the reason but the RichEdit 2 control in VCL does not
// respond to the EM_SCROLLCARET in Richedit.h but it does so to the
// constant in WinUser.h
const
EM_SCROLLCARET = $00B7;
var
TextPos: lResult;
Pos: TSmallPoint;
begin
TextPos := SendMessage(RichEdit.Handle, EM_LINEINDEX, LineNum, 0);
if TextPos <> -1 then begin
// Go to top
SendMessage(RichEdit.Handle, EM_SETSEL, 0, 0);
SendMessage(RichEdit.Handle, EM_SCROLLCARET, 0, 0);
// Get the coordinates for the beginning of the line
Longint(Pos) := SendMessage(RichEdit.Handle, EM_POSFROMCHAR, TextPos, 0);
// Scroll from the top
SendMessage(RichEdit.Handle, WM_VSCROLL,
MakeWParam(SB_THUMBPOSITION, Pos.y - RichEdit.ClientHeight div 2), 0);
// Optionally set the caret to the beginning of the line
SendMessage(RichEdit.Handle, EM_SETSEL, TextPos, TextPos);
end;
end;下面的代码是另一种选择,因为它将字符串的第一次出现作为中心,而不是行号;
procedure VertCenterText(RichEdit: TRichEdit; Text: string);
const
EM_SCROLLCARET = $00B7;
var
FindText: TFindText;
TextPos: lResult;
Pos: TSmallPoint;
begin
FindText.chrg.cpMin := 0;
FindText.chrg.cpMax := -1;
FindText.lpstrText := PChar(Text);
TextPos := SendMessage(RichEdit.Handle, EM_FINDTEXT,
FR_DOWN or FR_WHOLEWORD, Longint(@FindText));
if TextPos <> -1 then begin
SendMessage(RichEdit.Handle, EM_SETSEL, 0, 0);
SendMessage(RichEdit.Handle, EM_SCROLLCARET, 0, 0);
Longint(Pos) := SendMessage(RichEdit.Handle, EM_POSFROMCHAR, TextPos, 0);
SendMessage(RichEdit.Handle, WM_VSCROLL,
MakeWParam(SB_THUMBPOSITION, Pos.y - RichEdit.ClientHeight div 2), 0);
SendMessage(RichEdit.Handle, EM_SETSEL, TextPos, TextPos);
end;
end;发布于 2010-05-13 04:28:11
向RichEdit发送EM_LINESCROLL消息:
SendMessage(REdit.Handle, EM_LINESCROLL, 0, NumberOfVerticalLinesToScroll);请参阅EM_LINESCROLL MSDN topic。
发布于 2010-05-15 12:30:28
基于这里的想法,我想出了一个解决方案。它假设richedit中的所有行都具有相同的高度,并且richedit的默认字体正确地报告了它的高度,但它可能对某些人有用:
type
TCustomEditHack = class(TCustomEdit);
procedure CenterLineInEdit(Edit: TCustomEdit; LineNum: Integer);
var
VisibleLines: Integer;
TopLine: Integer;
FirstLine: Integer;
begin
FirstLine := Edit.Perform(EM_GETFIRSTVISIBLELINE, 0, 0);
VisibleLines := Round(Edit.ClientHeight / Abs(TCustomEditHack(Edit).Font.Height));
if VisibleLines <= 1 then
TopLine := LineNum
else
TopLine := Max(LineNum - Round((VisibleLines/2)) + 1, 0);
if FirstLine <> TopLine then
Edit.Perform(EM_LINESCROLL, 0, TopLine - FirstLine);
end;我用TRichEdit测试了这一点,但它可能也适用于TMemo。
https://stackoverflow.com/questions/2822471
复制相似问题