Hi there,
I am writing a copybook-like editor with richview
// First of all, I build two text style: one for background text in gray and
// one for normal text (typing) in green
// then, I add the background text which apply the textstyle(0)
CopyBook.InsertText('This is a test text', true);
// then, I rewrite the keypress function where I fill each character
// with a specific color
procedure TTestForm.CopyBookKeyPress(Sender: TObject; var Key: Char);
var
ss :Integer;
ch :String;
begin
if (key<>#13) and (key>#30) then
begin
CopyBook.ApplyTextStyle(1); // textstyle(1) for typing character
CopyBook.InsertText(key, False);
key := #0;
end;
end;
Now, everything is all right expect that the charater where I type will be inserted to the current caret position rather than replace the character at current position. Any approach for that problem.
BTW, how can I capture when the Enter is pressed?
Thanks in advance.
help replacing a character
-
- Site Admin
- Posts: 17562
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
1) http://www.trichview.com/forums/viewtopic.php?t=171
2) OnKeyDown, Key=VK_RETURN
2) OnKeyDown, Key=VK_RETURN
Thanks, it works. However, I found it is very flickering while typing. Here is my code
procedure TTestForm.SEKeyPress(Sender: TObject; var Key: Char);
var
aword :string;
rve: TCustomRichViewEdit;
ItemNo, Offs: Integer;
begin
if key<>#13 then
begin
if (key>#30) then
begin
try
sent := string(background_text_in_gray[curPos]);
// if what you type exactly matches the text in background
if (sent=key) then
begin
Inc(curPos);
rve := SentenceEdit.TopLevelEditor;
if rve.SelectionExists then
exit;
ItemNo := rve.CurItemNo;
Offs := rve.OffsetInCurItem;
if (Offs>=rve.GetOffsAfterItem(ItemNo)) then
begin
if (ItemNo+1<rve.ItemCount) and not rve.IsFromNewLine(ItemNo+1) then
begin
inc(ItemNo);
Offs := rve.GetOffsBeforeItem(ItemNo);
end
else exit;
end;
rve.SetSelectionBounds(ItemNo, Offs, ItemNo, Offs+1);
rve.ApplyTextStyle(1); // show the typed character in green
rve.InsertText(key, False); // in order to control the style
rve.Invalidate;
end;
except
on E:Exception do begin {exception shown here} end;
end;
key := #0;
end
end;
end;
Any idea?
procedure TTestForm.SEKeyPress(Sender: TObject; var Key: Char);
var
aword :string;
rve: TCustomRichViewEdit;
ItemNo, Offs: Integer;
begin
if key<>#13 then
begin
if (key>#30) then
begin
try
sent := string(background_text_in_gray[curPos]);
// if what you type exactly matches the text in background
if (sent=key) then
begin
Inc(curPos);
rve := SentenceEdit.TopLevelEditor;
if rve.SelectionExists then
exit;
ItemNo := rve.CurItemNo;
Offs := rve.OffsetInCurItem;
if (Offs>=rve.GetOffsAfterItem(ItemNo)) then
begin
if (ItemNo+1<rve.ItemCount) and not rve.IsFromNewLine(ItemNo+1) then
begin
inc(ItemNo);
Offs := rve.GetOffsBeforeItem(ItemNo);
end
else exit;
end;
rve.SetSelectionBounds(ItemNo, Offs, ItemNo, Offs+1);
rve.ApplyTextStyle(1); // show the typed character in green
rve.InsertText(key, False); // in order to control the style
rve.Invalidate;
end;
except
on E:Exception do begin {exception shown here} end;
end;
key := #0;
end
end;
end;
Any idea?
-
- Site Admin
- Posts: 17562
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Thanks for your reply. Yes, it is little better after removing the invlidate, however, the problem still no good. When I type, the text after the current character is shakeing seriously.Sergey Tkachenko wrote:Try removing call of rve.Invalidate. It's not needed, and usually it's not required to redraw the whole window.
-
- Site Admin
- Posts: 17562
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Yes, it does cure the problem a little bit more.Sergey Tkachenko wrote:One more suggestion: change ApplyTextStyle(1) to CurTextStyleNo := 1;
I try to use TRichEdit instead, the problem even worse. However, when I type in TRichedit, only the text on the same line of the current charater is flickering. While I use TRichview, only the text after the current charater is shakeing, rather than flickering. !?
-
- Site Admin
- Posts: 17562
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
There are two operations here, deleting selection and inserting. We removed one unnecessary operation (applying style to the selection), but these 2 are necessary.
If the window is updated after deletion, the text after the insertion point may shake a bit.
Well, you can forbid redrawing while making this operation, but in this case you'll need to redraw the whole window after. There will be no flickering, but typing will be slower (usually TRichView does not redraw the whole window on editing operations)
If the window is updated after deletion, the text after the insertion point may shake a bit.
Well, you can forbid redrawing while making this operation, but in this case you'll need to redraw the whole window after. There will be no flickering, but typing will be slower (usually TRichView does not redraw the whole window on editing operations)