In Windows API, the string corresponds to the pchar (pansichar) of Delphi. In API, the string of Delphi is flexible.
Assign values first:
// Assignment Method 1: Give begin setwindowtext (handle, 'newtitle'); end;
// Assignment Method 2: define the type var P: pchar; begin P: = 'newtitle'; setwindowtext (handle, P); end;
// Value assignment method 3: convert to the desired var STR: string; begin STR: = 'newtitle'; setwindowtext (handle, pchar (STR); end; // Assignment Method 4: var arr: array [0 .. 255] of char; begin arr: = 'new title'; setwindowtext (handle, arr); end;
Value:
// Method 1: Use a character array (often called "buffer") var arr: array [0 .. 254] of char; begin getwindowtext (handle, arr, 255); showmessage (ARR); {form1} end;
// Method 2: Use getmem to allocate memory for pchar var P: pchar; begin getmem (p, 255); {memory allocation} getwindowtext (handle, P, 255 ); showmessage (p); {form1} freemem (p); {release memory} end;
// Method 3: Use globalalloc to allocate global memory (slower than getmem) var P: hglobal; begin P: = globalalloc (0,255 ); {0 or gmem_fixed indicates that fixed memory is allocated.} getwindowtext (handle, pchar (P), 255); showmessage (pchar (p )); {form1} globalfree (p); {release memory} end;
// Method 4: Use string directly; setlength is required first, and then the blank space is removed: var STR: string; begin setlength (STR, 255 ); {set the STR length first} getwindowtext (handle, pchar (STR), 255); {but the STR length is 255 at this time !} STR: = pchar (STR); {the actual length of the string} showmessage (STR); {form1} end;
The fixed-length string does not end with #0. It is not compatible with the API and is generally not used in the API.