本文介绍了Richedit样式格式自行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(如果您能更好地理解和定义我的问题,请编辑标题.)

(Someone edit the title if you can understand and define my problem better.)

我遇到的问题是将RichEdit的样式格式还原"回默认的"nothing"(又名[]),然后再还原为我设置的粗体或斜体.

The problem which I am having is with style formatting of a RichEdit "reverting" back to the default "nothing" aka [] and then back to whatever I set it to, bold or italic for example.

有问题的是-我想,因为我不知道它是如何破坏事情的-是一个过程(REMainLinesCheck),该过程检查RichEdit中的行数并删除第一个行,直到达到特定点为止(一次最多显示14行),如下所示:

The thing that is at fault - I assume, since I have no idea how it is breaking things - is a procedure (REMainLinesCheck) that checks for amount of lines in the RichEdit and deletes the first one until a certain point is reached (to show a maximum of 14 lines at once) like so:

while REMain.Lines.Count > 14 do
  REMain.Lines.Delete(0);

在其他向RichEdit添加行的过程中,我有6次出现上述过程,但是没有一个改变RichEdit.SelAttributes.Style,只有一个,它只添加了一个粗体,如下所示:

I have 6 occurrences of the above procedure in other procedures that add lines to the RichEdit, but none of them change RichEdit.SelAttributes.Style but one, which was adding only one Bold line like so:

REMain.SelAttributes.Style := [fsBold];
REMain.Lines.Add('something');
REMainLinesCheck;

因此,我删除了所有出现的事件,除了一个并开始四处寻找,不久之后,它便开始正常运行,正常,粗体的行被正常添加,多余的行被删除,这没问题-没问题.但是,一旦我将REMainLinesCheck过程重新引入另一个过程中(为清楚起见,我们将其称为Proc3Lines,因为那样做:添加3行,然后检查多余的行),跟随此Proc3Lines的每一行都应加粗不是...从我在这里所经历的经验来看,REMainLinesCheck似乎在Proc3Lines中可以执行某些操作,因为没有它,一切都很好.

So I have removed all occurrences except that one and started poking around, it didn't take long to see that it was working in fact fine, regular and bold lines where being added normally and excess lines where being deleted - no problems. But as soon as I reintroduced REMainLinesCheck procedure into another procedure (for clarity purposes, lets call it Proc3Lines, because that's what it does: adds 3 lines and then calls the check for excess lines), every line that follows this Proc3Lines that should be Bold is not... From what I have experienced here it seems that REMainLinesCheck does something in Proc3Lines, since without it everything is fine.

很显然,这并不是一个互相调用的过程,但是代码的其他部分与此RichEdit没有关系,更不用说我不会在REMain的任何地方更改RichEdit.SelAttributes.Style,除了一个地方我已经显示,在同一单元中还有一个RichEdit,我确实可以像这样更改其行的样式,但是这不可能以任何方式关联...可以吗? (不,不是,我刚刚检查过.)

Obviously it's not a circle of procedures that call each other, but the other parts of the code have nothing to do with this RichEdit, not to mention that I don't change RichEdit.SelAttributes.Style anywhere for REMain except that one place that I have shown, there is another RichEdit in the same unit that I do change its line's style like that, but that cannot possibly be related in any way... could it? (No it does not, I just checked.)

基本上:Delphi到底是什么?没有比这更简单的方法了,而我仍在设法失败,有人可以解释和/或解决这个问题吗?提出问题,如果不清楚,我会尽力而为.

Basically: what the hell Delphi? It cannot get any simpler than this and I am still managing to fail, can someone explain and/or fix this? Ask questions, I'll elaborate as much as I can if something is not clear.

推荐答案

要将格式应用于新添加的行,请使用以下命令:

To apply a format to a new added line, use the following:

procedure TForm1.Button1Click(Sender: TObject);
var
  LineIndex: Integer;
begin
  LineIndex := RichEdit1.Lines.Add('Something');
  RichEdit1.SelStart := RichEdit1.Perform(EM_LINEINDEX, LineIndex, 0);
  RichEdit1.SelLength := RichEdit1.Perform(EM_LINELENGTH, RichEdit1.SelStart, 0);
  RichEdit1.SelAttributes.Style := [fsBold];
end;

这篇关于Richedit样式格式自行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:00