Solved

Text receiving window...

Posted on 2010-09-22
12
770 Views
Last Modified: 2013-11-23
I need a customized control such as TMemo, TRichEdit or equivalent ... This is used as a window that receives text from the chat- or teletype socket.
I want to scroll up the earlier received text, may select some words and copy it to the other controls.
The problem comes up when new text is received during the time when I read the text further up ... when new text is received, the focus will be moved to caret position and user made selection loses.
How would you avoid this "focus stealing" problem?
I'm using Delphi 7 & Windows 32 & 64 OS
0
Comment
Question by:ejla51
  • 5
  • 2
  • 2
  • +1
12 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 33739715
Hmmm not too sure but you could try the seltext, sellength functions, like this:
1. Remember where the cursor was and what was selected (seltext and sellength).
2. Append the new information at the end of the editor
3. Reset the cursor to what was the seltext and sellength etc
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 250 total points
ID: 33740082
I'm using a simple thread to simulate real time data entry. the idead here is to store the caret pos and restore it after insertion, Hope this helps you

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TAddProc = procedure of object;

  TMyThread = class(TThread)
  public
    FAddProc: TAddProc;
    constructor Create(AddProc: TAddProc);
    procedure Execute; override;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FThread: TMyThread;
    procedure DoAddProc;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


{ TMyThread }

constructor TMyThread.Create(AddProc: TAddProc);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FAddProc := AddProc;
  Resume;
end;

procedure TMyThread.Execute;
begin
  while not Terminated do
  begin
    Synchronize(FAddProc);
    Sleep(1000)
  end;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  FThread := TMyThread.Create(DoAddProc);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FThread.Terminate;
end;

procedure TForm1.DoAddProc;
var
  P: TPoint;
begin
  Memo1.Lines.BeginUpdate;
  P := Memo1.CaretPos;
  Memo1.Lines.Add(TimeToStr(Now));
  Memo1.CaretPos := P;
  Memo1.Lines.EndUpdate;
end;

end.
0
 
LVL 25

Assisted Solution

by:epasquier
epasquier earned 250 total points
ID: 33741702
You would have also to get the scroll position, otherwise it might be annoying to see the window moving
Use the GetScrollPos & SetScrollPos windows functions
procedure TForm1.DoAddProc;

var

  P: TPoint;

  SelectLen, ScrollPosVert , ScrollPosHoriz :Integer;

begin

  Memo1.Lines.BeginUpdate;

  P := Memo1.CaretPos;

  SelectLen:=Memo1.SelLength;

  ScrollPosVert :=GetScrollPos(Memo1.Handle, SB_VERT);

  ScrollPosHoriz :=GetScrollPos(Memo1.Handle, SB_HORZ);

  Memo1.Lines.Add(TimeToStr(Now));

  Memo1.CaretPos := P;

  Memo1.SelLength:=SelectLen;

  SetScrollPos(Memo1.Handle, SB_VERT,ScrollPosVert,True);

  SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True);

  Memo1.Lines.EndUpdate;

end;

Open in new window

0
 

Author Comment

by:ejla51
ID: 33743373
thanks for all the good comments, but I just found the remains undesirable features.
ewangoya: Caret position is OK, but the text selection will disappear when the new line has been added.
epsquier:  selection does not retain its original position when the new line added in the Memo1.
unit RxEditor;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TAddProc = procedure of object;



  TMyThread = class(TThread)

  public

    FAddProc: TAddProc;

    constructor Create(AddProc: TAddProc);

    procedure Execute; override;

  end;



  TForm1 = class(TForm)

    Memo1: TMemo;

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    FThread: TMyThread;

    procedure DoAddProc;

  public

    { Public declarations }

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



{ TMyThread }

// -----------------------------------------------------------------------------

constructor TMyThread.Create(AddProc: TAddProc);

begin

  inherited Create(True);

  FreeOnTerminate := True;

  FAddProc := AddProc;

  Resume;

end;

// -----------------------------------------------------------------------------

procedure TMyThread.Execute;

begin

  while not Terminated do

  begin

    Synchronize(FAddProc);

    Sleep(1000)

  end;

end;



{ TForm1 }

// -----------------------------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);

begin

  FThread := TMyThread.Create(DoAddProc);

end;

// -----------------------------------------------------------------------------

procedure TForm1.Button2Click(Sender: TObject);

begin

  FThread.Terminate;

end;

// -----------------------------------------------------------------------------

procedure TForm1.DoAddProc;

var

  P: TPoint;

  SelectLen, ScrollPosVert , ScrollPosHoriz :Integer;

begin

  Memo1.Lines.BeginUpdate;

  P := Memo1.CaretPos;

  SelectLen:=Memo1.SelLength;

  ScrollPosVert :=GetScrollPos(Memo1.Handle, SB_VERT);

  ScrollPosHoriz :=GetScrollPos(Memo1.Handle, SB_HORZ);

  Memo1.Lines.Add(TimeToStr(Now));

  Memo1.CaretPos := P;

  Memo1.SelLength:=SelectLen;

  SetScrollPos(Memo1.Handle, SB_VERT,ScrollPosVert,True);

  SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True);

  Memo1.Lines.EndUpdate;

end;



// -----------------------------------------------------------------------------

end.

Open in new window

0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 33743401
So that's why you use SelLength. Also, any new lines should be APPENDED at the end of the component.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ejla51
ID: 33743765
In reality, the text will enter one character at a time - as the chat terminal window ...
New series are fed only if received character is a carriage return chr (13).
0
 

Author Comment

by:ejla51
ID: 33744082
Added SelectStart variable to store start position and now the selection seems to "stay tuned".
The first step of my "OnRxChar" procedure is now completed.
I think it's fair play to share the points. I'm waiting for a while anyway for any possible comments.


  procedure TForm1.DoAddProc;

var

  P: TPoint;

  SelectStart, SelectLen, ScrollPosVert , ScrollPosHoriz :Integer;

begin

  Memo1.Lines.BeginUpdate;

  P := Memo1.CaretPos;

  SelectLen:=Memo1.SelLength;

  SelectStart := Memo1.SelStart;

  ScrollPosVert :=GetScrollPos(Memo1.Handle, SB_VERT);

  ScrollPosHoriz :=GetScrollPos(Memo1.Handle, SB_HORZ);

  Memo1.Lines.Add(TimeToStr(Now));

  Memo1.CaretPos := P;

  Memo1.SelStart := SelectStart;

  Memo1.SelLength:=SelectLen;

  SetScrollPos(Memo1.Handle, SB_VERT,ScrollPosVert,True);

  SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz-8 ,True);

  Memo1.Lines.EndUpdate;

end;

Open in new window

0
 

Author Comment

by:ejla51
ID: 33744169
Sorry typo... row 17 have unnecessary "-8" in testing purpose :(
must be:  
17: SetScrollPos(Memo1.Handle, SB_HORZ,ScrollPosHoriz ,True)
0
 
LVL 25

Expert Comment

by:epasquier
ID: 33744229
Yep, that seems complete to me :o) Great Team Work !!
0
 

Author Closing Comment

by:ejla51
ID: 33745073
Very nice concept but a little bit open
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now