Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi [nonVCL] Textlänge & GetWindowText, ExtTextOut Problem (https://www.delphipraxis.net/65397-%5Bnonvcl%5D-textlaenge-getwindowtext-exttextout-problem.html)

turboPASCAL 16. Mär 2006 08:14


[nonVCL] Textlänge & GetWindowText, ExtTextOut Problem
 
Moin,

Ich habe in einer nonVCL Anwendung folgendes Problem, im Code lese ich einen Text von einenm Item ein und möchte ihn wieder auf einen DC ausgeben.
Wenn ich die länge des Textes mit length(Text) oder sizeof(Text) bestimmen will werden alle Zeichen aus dem Puffer geschrieben. Gebe ich die Länge des Textes als Zahl direkt an ist alles ok.

Delphi-Quellcode:
var szText: Array [0..127] of Char;

//...

begin
  // Text holen
  GetWindowText(hwndItem, szText, sizeof(szText));

  // Text auf DC ausgeben
  ExtTextOut(lpdis.hDC,
      (lpdis.rcItem.Right div 2) - (ts.cx div 2),
      (lpdis.rcItem.Top div 2) + (ts.cy div 2),
      ETO_CLIPPED, @lpdis.rcItem, szText, length(szText), nil);// <---<<<
end;
Mit length(Text) oder sizeof(Text) bekomme ich immer die komplette länge des Arrays, also 128.

:gruebel:

xaromz 16. Mär 2006 08:25

Re: [nonVCL] Textlänge & GetWindowText, ExtTextOut Probl
 
Hallo,

ruf doch vorher Delphi-Referenz durchsuchenGetWindowTextLength auf, um die Länge des Textes zu bestimmen.

Gruß
xaromz

Luckie 16. Mär 2006 08:37

Re: [nonVCL] Textlänge & GetWindowText, ExtTextOut Probl
 
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer           : PChar;
  len              : Integer;
begin
  len := GetWindowTextLength(Handle);
  if len > 0 then
  begin
    try
      GetMem(Buffer, len);
      GetWindowText(Handle, Buffer, len);
      TextOut(Canvas.Handle, 10, 10, Buffer, len);
    finally
      FreeMem(Buffer);
    end;
  end
  else
    ShowMessage(SysErrorMessage(GetLastError));
end;
Aber beachte:
Zitat:

GetWindowTextLength cannot retrieve the length of the text of an edit control in another application.
Und:
Zitat:

Under certain conditions, the GetWindowTextLength function may return a value that is larger than the actual length of the text. [..] It can also occur when an application uses the ANSI version of GetWindowTextLength with a window whose window procedure is Unicode, or the Unicode version of GetWindowTextLength with a window whose window procedure is ANSI.
Zitat:

To obtain the exact length of the text, use the WM_GETTEXT, LB_GETTEXT, or CB_GETLBTEXT messages, or the GetWindowText function.
Und zu GetWindowText:
Zitat:

This function cannot retrieve the text of an edit control in another application.

turboPASCAL 16. Mär 2006 08:39

Re: [nonVCL] Textlänge & GetWindowText, ExtTextOut Probl
 
Stimmt, ist eine Möglichkeit.

Ich habe es jetzt mal mit PChar und (Windows.)lstrlen() gemacht.

Delphi-Quellcode:
var szText: PChar;

//...
begin
  GetMem(szText, 255);
  GetWindowText(hwndItem, szText, 255);

  // Text auf DC ausgeben
  ExtTextOut(lpdis.hDC,
      (lpdis.rcItem.Right div 2) - (ts.cx div 2),
      (lpdis.rcItem.Top div 2) + (ts.cy div 2),
      ETO_CLIPPED, @lpdis.rcItem, szText, lstrlen(szText), nil);// <---<<<
  //...
  FreeMem(szText, 255);
Klappt soweit. ;)

Luckie 16. Mär 2006 08:51

Re: [nonVCL] Textlänge & GetWindowText, ExtTextOut Probl
 
Und wenn der String länger als 255 Zeichen ist? Und warum mehr Speicher reservieren als nötig?

marabu 16. Mär 2006 09:00

Re: [nonVCL] Textlänge & GetWindowText, ExtTextOut Probl
 
Hallo Matti,

es geht auch so:
Delphi-Quellcode:
var
  buf: Array [0..MAX_PATH] of Char;
  bufSize: Integer;
begin
  bufSize := GetWindowText(hwndItem, buf, MAX_PATH);

  ExtTextOut(lpdis.hDC,
      (lpdis.rcItem.Right - ts.cx) div 2,
      (lpdis.rcItem.Top + ts.cy) div 2,
      ETO_CLIPPED, @lpdis.rcItem, szText, bufSize, nil
  );
end;
Wenn du den Speicher sowieso pauschal anforderst, dann kannst du ihn auch automatisch bereit stellen lassen. Wenn du die Textlänge später noch brauchst, dann solltest du sie zwischenspeichern. Die DIV Operation muss nicht viermal ausgeführt werden. Alles Kleinigkeiten.

Grüße vom marabu

turboPASCAL 16. Mär 2006 09:07

Re: [nonVCL] Textlänge & GetWindowText, ExtTextOut Probl
 
@Luckie, ist erst alles nur Experimentell. Hast aber Recht.

@marabu & @Luckie, ja :wall: GetWindowText gibt ja die Länge der kopierten Zeichen zurück. Warum habe ich das übersehen ?


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:44 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz