With the help of @Victorzanella and his response I developed the Project is not yet 100%, but already does what I wanted, when I finish I update:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
private
function GetCurrentLine(AMemo: TMemo): Integer;
function whiteSpaceCount(ASpaces: Integer): String;
function GetMaxCharacter(AMemo: TMemo): Integer;
procedure CleanWhiteSpace(var AMemo: TMemo);
procedure CreateWhiteSpace(var AMemo: TMemo);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.GetCurrentLine(AMemo: TMemo): Integer;
Var Coordinate: TPoint;
Begin
Coordinate := AMemo.CaretPos;
Result := Coordinate.Y + 1;
End;
function TForm1.GetMaxCharacter(AMemo: TMemo): Integer;
Var MaxChar: Integer;
Begin
MaxChar := (AMemo.Width div 7) - 1; //feito para font_name := "courier new"; size := "8"
Result := MaxChar;
End;
function TForm1.WhiteSpaceCount(ASpaces: Integer): String;
var i: Integer;
begin
Result := '';
for i := 1 to ASpaces do Result := Result + ' ';
end;
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
var ILine, WSCount, WSAdd: Integer;
begin
CleanWhiteSpace(Memo1);
if Key = #13 then
Begin
ILine := GetCurrentLine(Memo1) - 1;
WSCount := GetMaxCharacter(Memo1);
WSAdd := WSCount - Length(Memo1.Lines[ILine]);
Memo1.Lines[ILine] := Memo1.Lines[ILine] + WhiteSpaceCount(WSAdd);
End;
end;
procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
CreateWhiteSpace(Memo1);
end;
procedure TForm1.CreateWhiteSpace(AMemo: TMemo);
var ILine, ILine2, PColuna, WSAdd, WSCount: Integer;
Begin
ILine2 := GetCurrentLine(AMemo);
PColuna := Amemo.SelStart - Perform(EM_LINEINDEX, ILine2, 0);
ILine := GetCurrentLine(AMemo) - 1;
WSCount := GetMaxCharacter(AMemo);
WSAdd := WSCount - Length(AMemo.Lines[ILine]);
AMemo.Lines[ILine] := AMemo.Lines[ILine] + WhiteSpaceCount(WSAdd);
Amemo.SelStart := Perform(EM_LINEINDEX, ILine2, 0) + PColuna;
End;
procedure TForm1.CleanWhiteSpace(var AMemo: TMemo);
var ILine, PColuna: Integer;
SLine, CLine: String;
begin
ILine := GetCurrentLine(AMemo);
SLine := AMemo.Lines.Strings[ILine - 1];
SLine := Copy(SLine, GetMaxCharacter(AMemo)-2, 2);
if SLine = ' ' then
Begin
PColuna := AMemo.SelStart - Perform(EM_LINEINDEX, ILine, 0);
CLine := AMemo.lines[ILine - 1];
CLine := copy(CLine, 1, Length(CLine) - 1);
AMemo.lines[ILine - 1] := CLine;
AMemo.SelStart := Perform(EM_LINEINDEX, ILine, 0) + PColuna;
End;
end;
UPDATE 1
Key correction enter
, before he jumped lines or did not advance.
I believe the desired effect will have to be programmed. As a suggestion, when pressing the character
#40
(down arrow), you can also through the functionKeyBd_Event
press a specific number of characters#32
(space). The fact is that for the cursor position itself somewhere, there must be some character in the place.– Andrey
@Andrey because I understand what you’re saying, and thanks for the tip, but I wonder if there’s any other way to do it than this
– Tmc
What happens there, is that the Down in Memo , will pass the focus to another object. each line of Memo is a different Line.
– Victor Tadashi
@Victorzanella knows some way to avoid this other than by keydown?
– Tmc
@Victorzanella I didn’t understand what you meant... It was a bit confusing your comment. You can exemplify?
– Andrey
each line of Tmemo is a Tstring, I am putting together a possible solution.
– Victor Tadashi