关于RichEdit翻页的问题 -- by 萧月禾(300分)

  • 主题发起人 萧月禾
  • 开始时间

萧月禾

Unregistered / Unconfirmed
GUEST, unregistred user!
问二个关于RichEdit的问题
1)用程序控制RichEdit中的Rtf文本翻页
RichEdit.Perform(EM_SCROLL, SB_PAGEDOWN ,0);
如何知道已经翻到最后?

2)将外部的Rtf文件读入RichEdit中
希望能用黑色作底色,文本用白色显示
如果能做到?

我查过以前的帖子,都不能实现之
 
下翻:RichEdit1.Perform(WM_VSCROLL,SB_PAGEDOWN,0);
上翻:RichEdit1.Perform(WM_VSCROLL,SB_PAGEUP,0);


procedure TEditForm.EditorKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
CurPos:TPoint;
begin
CurPos:=Editor.CaretPos;
Statusbar1.Panels[0].Text:=Format('Row:%5d Col:%3d',[CurPos.y,CurPos.x]);
if Key=VK_NEXT then
begin
if Editor.CaretPos.y>=Editor.Lines.Count then
begin
SendMessage(Editor.Handle, WM_VSCROLL, SB_top, 0);
Editor.SelStart:=0;
Editor.SelLength:=0;
Editor.SetFocus;
end
end



end;

改颜色
richedit1.color背景颜色
richedit1.font.color 字体颜色
 
这方法我在另一篇帖子已经看过了
实现不了

另外第二个问题也无法实现
因为是要对打开的文件进行颜色的更改
 
procedure TForm1.Button1Click(Sender: TObject);
var
ret: Integer;
begin
ret := SendMessage(RichEdit1.Handle, EM_SCROLL, SB_PAGEDOWN, 0);
if ret = 65536 then ShowMessage('已到最后');
end;
 
//改颜色
Richedit1.Color //背景颜色
Richedit1.Font.Color //字体颜色

procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=VK_PRIOR then RichEdit1.Perform(WM_VSCROLL,SB_PAGEUP,0);
if Key=VK_NEXT
then
begin
RichEdit1.Perform(WM_VSCROLL,SB_PAGEDOWN,0);
if (RichEdit1.CaretPos.y>=RichEdit1.Lines.Count )//到结尾后回到页首
then
begin
SendMessage(RichEdit1.Handle, WM_VSCROLL, SB_top, 0);
RichEdit1.SelStart:=0;
RichEdit1.SelLength:=0;
RichEdit1.SetFocus;
end;
end;
end;
 
第一个问题我刚刚解决了

iM := 65537; //赋初值
while iM > 65536 do
begin
iM := RichEdit.Perform(EM_SCROLL, SB_PAGEDOWN ,0);
Sleep(ATimes*1000);
end;


如果翻页成功,则返回为大于65536的值,即True
到最底时,则翻页不成功,返回65536,即False
如果是用TMemo控件,返回值则以0为界限,大于0为True

“独帅”用的也是这个方法
不过用SendMessage好象总返回0呀
 
另外,关于第二个问题不是那么简单的
因为其文本的内容是从外部文件读入
不管是静态或动态修改RichEdit的Font.Color属性
都不起作用,即使将文本全选再修改也不行
 
//不过用SendMessage好象总返回0呀
你试试就知道了[8D]
SendMessage的返回值与他发送的消息有关
对于EM_SCROLL,返回值的高字节总是0或1,表示函数是否成功,低字节表示函数作用(滚动)的行数
如果返回值是65536,换算成十六进制就是00010000,表示函数调用成功(高字节为0001),
但是滚动行数为0,当然是已经到了最后面了
如果返回值大于65536,则返回值与65536的差值就是滚动的行数
 
1 在pagedown 过程中判断 第一可视行是否变化即可!!
procedure TForm1.Button8Click(Sender: TObject);
var
n,m:integer;
begin
n:=-1;
while true do begin
RichEdit1.perform(WM_VSCROLL, SB_PAGEDOWN, 0);
m:=RichEdit1.perform(EM_GETFIRSTVISIBLELINE,0,0 );
if m=n then break; //已经到最后!!否则死循环 //就在这里知道!!!!!
n:=m;
end;
end;
//注:这一方法会导致最后一行是不可见的。

2 可能无法实现,因为无法改变选中文本的背景色。以下是
TTextAttributes.Color的说明:“选中文本的背景色是由RichEdit.color决定的”
Description
Use color to indicate the color of the actual characters. the background color is a property of the rich edit control that uses the TTextAttributes object, and should be set by the Color property of the rich edit control.

如果直接想移到头、尾,可用以下方法:
procedure TForm1.Button6Click(Sender: TObject);
begin //移到头上
RichEdit1.SetFocus ;
keybd_event(vk_Control,0,0,0);
keybd_event(VK_HOME,0,0,0);
keybd_event(VK_HOME,0,KEYEVENTF_KEYUP,0);
keybd_event(vk_control,0,KEYEVENTF_KEYUP,0);
end;

procedure TForm1.Button7Click(Sender: TObject);
begin //移到末尾
RichEdit1.SetFocus ;
keybd_event(vk_Control,0,0,0);
keybd_event(VK_END,0,0,0);
keybd_event(VK_END,0,KEYEVENTF_KEYUP,0);
keybd_event(vk_control,0,KEYEVENTF_KEYUP,0);

end;

 
第2个问题我有办法,用哪个RichEdit1.lines.se****属性可以修改字体颜色
 
richedit1.Lines.LoadFromFile('c:/Simple.rtf');
richedit1.DefAttributes.Color := clRed; // 修改全部
richedit1.SelAttributes.Color := clRed; // 修改选中部分
 
beta和张无忌的方法的确可以呀[:)]
不过还有一点小问题
rtf文件中的表格的线条不受控制,仍然是黑色呀
如果背景色是黑色,则看不到它了(因为是在LED上显示,所以用黑底白字)
 
不行了,richedit 就那么点本事:(

 
DefAttributes.color 改的是背景色吗????????????????//
 
对学过vc的人来说太简单了,不过实现起来太过麻烦。。多加点分我发源码给你(vc的)
 
VC我只是当年在学校时学过一天,发现学不会就放弃了
发给我也没用
 
我劝你还是用 HTM 好了,用户编辑也方便,有现成的 FrontPage 可以用。
你说 WebBrowser 载入慢,反正你显示一个要慢慢地显示很久,开一个线程
利用这个时间把其他的都载入吧。

 
// VC我只是当年在学校时学过一天
我也只是在机房学了几个小时而已。看来这是我们桂电的传统啊:)
我们科协一个 VC 高手,在我的“诱惑”之下也转学 Delphi 了:)

 
多人接受答案了。
 
顶部