Получить доступ к элементам списка с помощью API
Оформил: DeeCo
Автор: http://www.swissdelphicenter.ch
{
This code might be useful for nonVCL applications or to read Listbox
items from another application.
}
{
Die folgenden Funktionen konnen z.B fur nonVCL Projekte verwendet werden
oder auch zum Auslesen von Listboxen aus anderen Applikationen.
}
// Retrieve the number of items in a ListBox
// Anzahl Eintrage einer ListBox ermitteln
function LB_GetItemCount(hListBox: THandle): Integer;
begin
Result := SendMessage(hListBox, LB_GETCOUNT, 0, 0);
end;
// Delete a string in a ListBox
// Einen String in einer ListBox loschen
procedure LB_DeleteItem(hListBox: THandle; Index: Integer);
begin
SendMessage(hListBox, LB_DELETESTRING, Index, 0);
end;
// Retrieve the selected item from a ListBox
// Gibt den Text des markiertes Items einer ListBox zuruck
function LB_GetSelectedItem(hListBox: THandle): string;
var
Index, len: Integer;
s: string;
buffer: PChar;
begin
Index := SendMessage(hListBox, LB_GETCURSEL, 0, 0);
len := SendMessage(hListBox, LB_GETTEXTLEN, wParam(Index), 0);
GetMem(buffer, len + 1);
SendMessage(hListBox, LB_GETTEXT, wParam(Index), lParam(buffer));
SetString(s, buffer, len);
FreeMem(buffer);
Result := IntToStr(Index) + ' : ' + s;
end;
// Example, Beispiel:
procedure TForm1.Button1Click(Sender: TObject);
var
hListBox: THandle;
begin
hListBox := {...}; // listbox handle
ListBox1.Items.Text := LB_GetSelectedItem(hListBox);
end;
// Retrieve a string from a ListBox
// Gibt den Text eines bestimmten Items einer ListBox zuruck
function LB_GetListBoxItem(hWnd: Hwnd; LbItem: Integer): string;
var
l: Integer;
buffer: PChar;
begin
l := SendMessage(hWnd, LB_GETTEXTLEN, LbItem, 0);
GetMem(buffer, l + 1);
SendMessage(hWnd, LB_GETTEXT, LbItem, Integer(buffer));
Result := StrPas(buffer);
FreeMem(buffer);
end;
// Example, Beispiel:
procedure TForm1.Button2Click(Sender: TObject);
var
hListBox: THandle;
begin
hListBox := {...}; // listbox handle
ListBox1.Items.Text := LB_GetListBoxItem(hListBox, 2);
end;
// Gibt den gesamten Text einer ListBox zuruck
// Retrieve all listbox items
function LB_GetAllItems(hWnd: Hwnd; sl: TStrings): string;
var
RetBuffer: string;
i, x, y: Integer;
begin
x := SendMessage(hWnd, LB_GETCOUNT, 0, 0);
for i := 0 to x - 1 do
begin
y := SendMessage(hWnd, LB_GETTEXTLEN, i, 0);
SetLength(RetBuffer, y);
SendMessage(hWnd, LB_GETTEXT, i, lParam(PChar(RetBuffer)));
sl.Add(RetBuffer);
end;
end;
// Example, Beispiel:
procedure TForm1.Button3Click(Sender: TObject);
var
sl: TStringList;
ListBox_Handle: THandle;
begin
hListBox := {...}; // listbox handle
sl := TStringList.Create;
try
LB_GetAllItems(ListBox_Handle, sl);
finally
ListBox1.Items.Text := sl.Text;
sl.Free;
end;
end;
|