RRUZ / vcl-styles-utils

Extend and improve the Delphi VCL Styles
https://theroadtodelphi.wordpress.com/
331 stars 114 forks source link

TSysControl.GetText will truncate text to 1K characters #225

Open jpluimers opened 6 years ago

jpluimers commented 6 years ago

TSysControl.GetText will truncate text to 1K characters. This is less worse than the VCL which - up to 10.1 Berlin - did this:

function GetSysWindowText(Window: HWND): string;
var
  Text: array[0..256] of Char;
begin
  SetString(Result, Text, Winapi.Windows.GetWindowText(Window, Text, Length(Text)));
end;

A solution like below is more flexible:

function GetSysWindowText_WMGetText(Window: HWND): string;
var
  RequiredTextLength: Integer;
  ObtainedTextLength: Integer;
begin
  RequiredTextLength := SendMessage(Window, WM_GETTEXTLENGTH, 0, 0);
  SetString(Result, PChar(nil), RequiredTextLength);
  if RequiredTextLength <> 0 then
  begin
    ObtainedTextLength := SendMessage(Window, WM_GETTEXT, WParam(RequiredTextLength + 1), LParam(PChar(Result)));
    if ObtainedTextLength < RequiredTextLength then
      SetLength(Result, ObtainedTextLength);
  end;
end;

Two notes: