首页  编辑  

获取编辑框中显示出来的字符

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:获取编辑框中显示出来的字符

说明:Edit,Memo;比起屏幕取词差远了

设计:Zswang

日期:2002-02-20

支持:wjhu111@21cn.com

//*)

////////Begin Source

function EditVisibleText(mEdit: TEdit): string;

var

 X, Y, L: Integer;

 S: string;

begin

 Result := '';

 if not Assigned(mEdit) then Exit;

 with mEdit do try

   S := Text;

   L := Length(S);

   X := SendMessage(Handle, EM_CHARFROMPOS, 0, MAKELPARAM(2, 2));

   X := X and $0000FFFF;

   Y := SendMessage(Handle, EM_CHARFROMPOS, 0, MAKELPARAM(ClientWidth - 4, 2));

   Y := Y and $0000FFFF;

   for X := X to Y - 1 do if (Y >= 0) and (X < L) then

     Result := Result + S[X + 1];

 except

   Result := '';

 end;

end; { EditVisibleText }

function MemoVisibleText(mMemo: TMemo; mStrings: TStrings): Boolean;

var

 I, X, Y: Integer;

 L, H, W: Integer;

 S: string;

 T: string;

begin

 Result := False;

 if (not Assigned(mMemo)) or (not Assigned(mStrings)) then Exit;

 with TControlCanvas.Create do try

   Control := mMemo;

   H := TextHeight('|');

 finally

   Free;

 end;

 mStrings.Clear;

 with mMemo do try

   S := Text;

   L := Length(S);

   W := ClientWidth;

   for I := 0 to (ClientHeight div H) - 1 do begin

     X := SendMessage(Handle, EM_CHARFROMPOS, 0, MAKELPARAM(3, I * H + 2));

     X := X and $0000FFFF;

     Y := SendMessage(Handle, EM_CHARFROMPOS, 0, MAKELPARAM(5, I * H + 2));

     Y := Y and $0000FFFF;

     if Abs(Y - X) > 1 then Inc(X);

     if not ((X = 0) or ((X < L) and (S[X - 1] in [#13, #10]))) then Inc(X);

     Y := SendMessage(Handle, EM_CHARFROMPOS, 0, MAKELPARAM(W - 2, I * H + 2));

     Y := Y and $0000FFFF;

     T := '';

     for X := X to Y - 1 do if (Y >= 0) and (X < L) then

       T := T + S[X + 1];

     mStrings.Add(T);

   end;

 except

   Exit;

 end;

 Result := True;

end; { MemoVisibleText }

////////End Source

////////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

 MemoVisibleText(Memo1, Memo2.Lines);

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

 Caption := EditVisibleText(Edit1);

end;

////////End Demo