Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1812
  • Last Modified:

SETTEXT, GETTEXT Question.

Hey,

Buffer : array[0..100] of Char;
SendMessage(hwndxyz, WM_GETTEXT, 100, integer(@Buffer));

hwndxyz is a textbox.
Buffer will now be the first 100 chars of that texbox.

Question 1: I need a way to get all the text from the textbox, and not just 100 chars.
Question 2: How do you get the last 100 chars from a textbox?
Question 3: How do you get the length of the textbox text.

Thnx!
0
TonyJiz
Asked:
TonyJiz
  • 5
  • 3
1 Solution
 
geobulCommented:
Hi,

Use this:

function ReadFromWindowText(h:HWND) : string;
var
 buf : string;
 len : integer;
begin
 try
   len := SendMessage(h,wm_getTextLength,0,0);
   SetLength(buf,len);
   SendMessage(h,wm_getText,len+1,Integer(buf));
   result := buf;
 except
   result := '';
 end;
end;

// usage:
var s: string;
begin
  // all the text
  s := ReadFromWindowText(hwndxyz);
  // last 100 chars
  s := Copy(s, Length(s) - 100, Length(s));
end;

Regards, Geo
0
 
TonyJizAuthor Commented:
Hey, then do also know why this won't do:

var xyz: string;
SendMessage(hwndxyz, WM_SETTEXT, length(xyz), integer(xyz));

If I don't set xyz as a string, but instead: xyz = array[0..100] of Char;
then it will do, but I dont know how long xyz is going to be and it could be way more than 100 chars so I want to use a string instead. There's probably another solution, do you know this, I will give you the points then.

I thought that with the previous question this would also be solved.

Thnx!
0
 
geobulCommented:
SendMessage(hwndxyz, WM_SETTEXT, 0, integer(xyz));
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TonyJizAuthor Commented:
Hey let me rephrase that.

The following lines don't do the job (xyz returns '')

var
i: Integer;
xyz: string;
begin
i := SendMessage(hwndxyz,wm_getTextLength,0,0);
SetLength(xyz,i);
SendMessage(hwndxyz,WM_GETTEXT,i,Integer(xyz));

The following lines do the job:

var
Buffer : array[0..255] of Char;
begin
SendMessage(hwndedit2,WM_GETTEXT,255,Integer(@buffer));
xyz := Buffer;

But the problem with the last one is the maxiumum of 255 chars.

Any ideas?
Thnx!
0
 
geobulCommented:
>SendMessage(hwndxyz,WM_GETTEXT,i,Integer(xyz));
should be
SendMessage(hwndxyz,WM_GETTEXT,i+1,Integer(xyz));
0
 
geobulCommented:
Why don't you simply use the function above?
0
 
geobulCommented:
BTW your code works fine here:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  xyz: string;
begin
  i := SendMessage(Memo1.Handle,wm_getTextLength,0,0);
  SetLength(xyz,i);
  SendMessage(Memo1.Handle,WM_GETTEXT,i+1,Integer(xyz));
  Memo2.Lines.Text := xyz;
end;

Memo1 contains more than 5000 chars.
0
 
TonyJizAuthor Commented:
Yup indeed. Thnx!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now